Skip to content

Commit 566c9b5

Browse files
Simplify logging in pytsql.py (#64)
**Motivation**: When parsing multiple files, the package produces quite some logging. These entries are not telling much and create overhead. **Changes**: Remove two logging entries - which are more helpful for debugging (in my view). Besides, I propose to formulate the source of another one more explicitly.
1 parent 6fdf204 commit 566c9b5

File tree

5 files changed

+29
-9
lines changed

5 files changed

+29
-9
lines changed

environment.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ dependencies:
1313
- sphinx
1414
- sphinx_rtd_theme
1515
- sphinxcontrib-apidoc
16-
- sqlalchemy
16+
- sqlalchemy>=1.4, <2.0
1717
- pyodbc
1818
- speedy-antlr-tool==1.4.1
1919
- antlr4-python3-runtime==4.11.1

pyproject.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ dynamic = ["version"]
3636
requires-python = ">=3.7.0"
3737

3838
dependencies = [
39-
"sqlalchemy >=1.4",
39+
"sqlalchemy >=1.4, <2.0",
4040
"antlr4-python3-runtime ==4.11",
4141
"pyodbc >=4.0.30"
4242
]

src/pytsql/tsql.py

+6-4
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818
_GO = "GO"
1919
_PRINTS_TABLE = "#pytsql_prints"
2020

21+
logger = logging.getLogger("pytsql")
22+
2123

2224
def _code(path: Union[str, Path], encoding: str) -> str:
2325
with Path(path).open(encoding=encoding) as fh:
@@ -142,7 +144,7 @@ def syntaxError(
142144
error_message = f"Line {line}:{column} {msg}."
143145
if offendingSymbol is not None:
144146
error_message += f" Problem near token '{offendingSymbol.text}'."
145-
logging.error(error_message)
147+
logger.error(error_message)
146148
raise ValueError(f"Error parsing SQL script: {error_message}")
147149

148150

@@ -154,7 +156,7 @@ def _split(code: str, isolate_top_level_statements: bool = True) -> List[str]:
154156
" and is preventing the use of the faster C++ parser."
155157
)
156158

157-
logging.info("Started SQL script parsing")
159+
logger.debug("Started SQL script parsing")
158160

159161
# The default error listener only prints to the console without raising exceptions.
160162
error_listener = _RaisingErrorListener()
@@ -182,15 +184,15 @@ def _split(code: str, isolate_top_level_statements: bool = True) -> List[str]:
182184
else:
183185
batches.append("\n".join(clauses))
184186

185-
logging.info("SQL script parsed successfully.")
187+
logger.debug("SQL script parsed successfully.")
186188

187189
return batches
188190

189191

190192
def _fetch_and_clear_prints(conn: Connection):
191193
prints = conn.execute(f"SELECT * FROM {_PRINTS_TABLE};")
192194
for row in prints.all():
193-
logging.info(f"Captured PRINT statement: {row[0]}")
195+
logger.info(f"SQL PRINT: {row[0]}")
194196
conn.execute(f"DELETE FROM {_PRINTS_TABLE};")
195197

196198

tests/integration/test_logging.py

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import logging
2+
3+
from pytsql import executes
4+
5+
6+
def test_deactivation_of_logger(engine, caplog):
7+
logger = logging.getLogger("pytsql")
8+
logger.disabled = True
9+
logger.setLevel(logging.DEBUG)
10+
11+
seed = "PRINT 'test'"
12+
executes(seed, engine)
13+
14+
assert len(caplog.records) == 0
15+
16+
logger.disabled = False

tests/integration/test_prints.py

+5-3
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
from pytsql import executes
44

5+
SQL_PRINT_LOG_TEXT = "SQL PRINT"
6+
57

68
def test_evaluations(engine, caplog):
79
caplog.set_level(logging.INFO)
@@ -17,7 +19,7 @@ def test_evaluations(engine, caplog):
1719
assert "basic text" in caplog.text
1820
assert "20" in caplog.text
1921
assert "two plus two equals 4" in caplog.text
20-
assert caplog.text.count("Captured PRINT") == 3
22+
assert caplog.text.count(SQL_PRINT_LOG_TEXT) == 3
2123

2224

2325
def test_evaluation_in_cf(engine, caplog):
@@ -36,7 +38,7 @@ def test_evaluation_in_cf(engine, caplog):
3638
assert "text1" in caplog.text
3739
assert "text2" in caplog.text
3840
assert "text3" in caplog.text
39-
assert caplog.text.count("Captured PRINT") == 3
41+
assert caplog.text.count(SQL_PRINT_LOG_TEXT) == 3
4042

4143

4244
def test_evaluation_in_context(engine, caplog):
@@ -55,7 +57,7 @@ def test_evaluation_in_context(engine, caplog):
5557

5658
for i in range(10):
5759
assert str(i) in caplog.text
58-
assert caplog.text.count("Captured PRINT") == 10
60+
assert caplog.text.count(SQL_PRINT_LOG_TEXT) == 10
5961

6062

6163
def test_print_unicode(engine, caplog):

0 commit comments

Comments
 (0)