Skip to content

Commit

Permalink
Simplify logging in pytsql.py (#64)
Browse files Browse the repository at this point in the history
**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.
  • Loading branch information
svengiegerich authored Jan 31, 2023
1 parent 6fdf204 commit 566c9b5
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 9 deletions.
2 changes: 1 addition & 1 deletion environment.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ dependencies:
- sphinx
- sphinx_rtd_theme
- sphinxcontrib-apidoc
- sqlalchemy
- sqlalchemy>=1.4, <2.0
- pyodbc
- speedy-antlr-tool==1.4.1
- antlr4-python3-runtime==4.11.1
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ dynamic = ["version"]
requires-python = ">=3.7.0"

dependencies = [
"sqlalchemy >=1.4",
"sqlalchemy >=1.4, <2.0",
"antlr4-python3-runtime ==4.11",
"pyodbc >=4.0.30"
]
Expand Down
10 changes: 6 additions & 4 deletions src/pytsql/tsql.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
_GO = "GO"
_PRINTS_TABLE = "#pytsql_prints"

logger = logging.getLogger("pytsql")


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


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

logging.info("Started SQL script parsing")
logger.debug("Started SQL script parsing")

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

logging.info("SQL script parsed successfully.")
logger.debug("SQL script parsed successfully.")

return batches


def _fetch_and_clear_prints(conn: Connection):
prints = conn.execute(f"SELECT * FROM {_PRINTS_TABLE};")
for row in prints.all():
logging.info(f"Captured PRINT statement: {row[0]}")
logger.info(f"SQL PRINT: {row[0]}")
conn.execute(f"DELETE FROM {_PRINTS_TABLE};")


Expand Down
16 changes: 16 additions & 0 deletions tests/integration/test_logging.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import logging

from pytsql import executes


def test_deactivation_of_logger(engine, caplog):
logger = logging.getLogger("pytsql")
logger.disabled = True
logger.setLevel(logging.DEBUG)

seed = "PRINT 'test'"
executes(seed, engine)

assert len(caplog.records) == 0

logger.disabled = False
8 changes: 5 additions & 3 deletions tests/integration/test_prints.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

from pytsql import executes

SQL_PRINT_LOG_TEXT = "SQL PRINT"


def test_evaluations(engine, caplog):
caplog.set_level(logging.INFO)
Expand All @@ -17,7 +19,7 @@ def test_evaluations(engine, caplog):
assert "basic text" in caplog.text
assert "20" in caplog.text
assert "two plus two equals 4" in caplog.text
assert caplog.text.count("Captured PRINT") == 3
assert caplog.text.count(SQL_PRINT_LOG_TEXT) == 3


def test_evaluation_in_cf(engine, caplog):
Expand All @@ -36,7 +38,7 @@ def test_evaluation_in_cf(engine, caplog):
assert "text1" in caplog.text
assert "text2" in caplog.text
assert "text3" in caplog.text
assert caplog.text.count("Captured PRINT") == 3
assert caplog.text.count(SQL_PRINT_LOG_TEXT) == 3


def test_evaluation_in_context(engine, caplog):
Expand All @@ -55,7 +57,7 @@ def test_evaluation_in_context(engine, caplog):

for i in range(10):
assert str(i) in caplog.text
assert caplog.text.count("Captured PRINT") == 10
assert caplog.text.count(SQL_PRINT_LOG_TEXT) == 10


def test_print_unicode(engine, caplog):
Expand Down

0 comments on commit 566c9b5

Please sign in to comment.