Skip to content

Commit 56634ce

Browse files
committed
minor changes
1 parent 388a3c3 commit 56634ce

File tree

4 files changed

+25
-7
lines changed

4 files changed

+25
-7
lines changed

main.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,16 @@
11
from mssql_python import connect
2-
from mssql_python import setup_logging
2+
from mssql_python import setup_logging, get_logger
33
import os
44
import decimal
55

66
setup_logging('stdout')
77

8+
logger = get_logger()
9+
if logger:
10+
logger.info("This should print")
11+
else:
12+
print("Logger is None - logging not enabled!")
13+
814
conn_str = os.getenv("DB_CONNECTION_STRING")
915
conn = connect(conn_str)
1016

mssql_python/connection.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
from mssql_python.helpers import add_driver_to_connection_str, sanitize_connection_string
1919
from mssql_python import ddbc_bindings
2020
from mssql_python.pooling import PoolingManager
21-
from mssql_python.exceptions import DatabaseError, InterfaceError
21+
from mssql_python.exceptions import InterfaceError
2222

2323
logger = get_logger()
2424

@@ -281,5 +281,7 @@ def __del__(self):
281281
try:
282282
self.close()
283283
except Exception as e:
284-
if logger:
285-
logger.error(f"Error during connection cleanup in __del__: {e}")
284+
raise InterfaceError(
285+
driver_error=str(e),
286+
ddbc_error="Error during python connection cleanup"
287+
)

mssql_python/cursor.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
from mssql_python.helpers import check_error
1818
from mssql_python.logging_config import get_logger
1919
from mssql_python import ddbc_bindings
20+
from mssql_python.exceptions import InterfaceError
2021
from .row import Row
2122

2223
logger = get_logger()
@@ -739,5 +740,7 @@ def __del__(self):
739740
try:
740741
self.close()
741742
except Exception as e:
742-
if logger:
743-
logger.error(f"Error closing cursor: {e}")
743+
raise InterfaceError(
744+
driver_error=str(e),
745+
ddbc_error="Error during python connection cleanup"
746+
)

mssql_python/logging_config.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,7 @@ def get_logger(self):
132132
logging.Logger: The logger instance, or None if logging is not enabled.
133133
"""
134134
if not self.enabled:
135+
# If logging is not enabled, return None
135136
return None
136137
return self._logger
137138

@@ -160,4 +161,10 @@ def get_logger():
160161
Returns:
161162
logging.Logger: The logger instance.
162163
"""
163-
return _manager.get_logger()
164+
# Always return a logger instance, even if setup hasn't been called yet
165+
# This ensures modules can get a logger reference that will work once setup is called
166+
if _manager._logger is None:
167+
# Return the logger that will be configured when setup() is called
168+
# This logger won't output anything until handlers are added in setup()
169+
return logging.getLogger("mssql_python")
170+
return _manager._logger

0 commit comments

Comments
 (0)