Skip to content

Commit 2a44f59

Browse files
Add Logging functionality in ibm_db_sa (#194)
* Add configurable logging support to ibm_db_sa using ibmdbsa_log URL parameter Signed-off-by: Balram Choudhary <bchoudhary@rocketsoftware.com> * Added encoding for file logs Signed-off-by: Balram Choudhary <bchoudhary@rocketsoftware.com> --------- Signed-off-by: Balram Choudhary <bchoudhary@rocketsoftware.com>
1 parent 95e6158 commit 2a44f59

6 files changed

Lines changed: 2875 additions & 1298 deletions

File tree

README.md

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,109 @@ For a local socket connection, exclude the "host" and "port" portions::
8383
e = create_engine("ibm_db_sa://user:pass@/database")
8484
```
8585

86+
## Logging in ibm_db_sa
87+
The `ibm_db_sa` module provides built-in logging support to help debug SQLAlchemy dialect operations, connection handling, reflection, and SQL execution.
88+
Logging can be enabled using the `ibmdbsa_log` query parameter in the database connection URL.
89+
90+
### Logging Configuration
91+
Use the `ibmdbsa_log` parameter at the end of the connection URL.
92+
Possible values:
93+
```
94+
True -> Enable logging to the console
95+
"filename" -> Enable logging to a file (file is overwritten on each run)
96+
False -> Disable logging
97+
```
98+
99+
---
100+
### Examples
101+
#### 1. Enable Logging to a File (Current Directory)
102+
Logs will be written to a file in the current working directory.
103+
```python
104+
from sqlalchemy import create_engine
105+
engine = create_engine(
106+
"ibm_db_sa://userID:Password@host:port/database?ibmdbsa_log=log_ibmdbsa.txt"
107+
)
108+
```
109+
This will create the file:
110+
```
111+
log_ibmdbsa.txt
112+
```
113+
---
114+
#### 2. Enable Logging to a File (Specific Directory)
115+
You can provide an absolute file path.
116+
#### Windows example
117+
```python
118+
from sqlalchemy import create_engine
119+
engine = create_engine(
120+
"ibm_db_sa://userID:Password@host:port/database?ibmdbsa_log=C:\\Users\\Logs\\log_ibmdbsa.txt"
121+
)
122+
```
123+
#### Linux / macOS example
124+
```python
125+
from sqlalchemy import create_engine
126+
engine = create_engine(
127+
"ibm_db_sa://userID:Password@host:port/database?ibmdbsa_log=/var/log/log_ibmdbsa.txt"
128+
)
129+
```
130+
---
131+
#### 3. Enable Console Logging
132+
Logs will be printed to the terminal or console.
133+
```python
134+
from sqlalchemy import create_engine
135+
engine = create_engine(
136+
"ibm_db_sa://userID:Password@host:port/database?ibmdbsa_log=True"
137+
)
138+
```
139+
---
140+
### Using SQLAlchemy URL Object
141+
Logging can also be configured using SQLAlchemy's `URL` object.
142+
#### Log to File
143+
```python
144+
from sqlalchemy import create_engine
145+
from sqlalchemy.engine import URL
146+
url_object = URL.create(
147+
drivername="ibm_db_sa",
148+
username="userID",
149+
password="Password",
150+
host="host",
151+
port=port,
152+
database="database",
153+
query={"ibmdbsa_log": "log_ibmdbsa.txt"},
154+
)
155+
engine = create_engine(url_object)
156+
```
157+
---
158+
#### Log to Console
159+
```python
160+
from sqlalchemy import create_engine
161+
from sqlalchemy.engine import URL
162+
url_object = URL.create(
163+
drivername="ibm_db_sa",
164+
username="userID",
165+
password="Password",
166+
host="host",
167+
port=port,
168+
database="database",
169+
query={"ibmdbsa_log": "True"},
170+
)
171+
engine = create_engine(url_object)
172+
```
173+
---
174+
### Notes
175+
- Logging configuration is automatically detected when the SQLAlchemy engine is created.
176+
- The `ibmdbsa_log` parameter is removed internally before the connection parameters are passed to the DBAPI driver.
177+
- If logging is not specified, logging remains disabled by default.
178+
---
179+
#### Typical Use Cases
180+
Logging can help diagnose:
181+
- Connection issues
182+
- SQL execution problems
183+
- Reflection metadata queries
184+
- Dialect initialization
185+
- Performance troubleshooting
186+
187+
188+
86189
Supported Databases
87190
-------------------
88191
- IBM DB2 Database for Linux/Unix/Windows versions 11.5 onwards

0 commit comments

Comments
 (0)