Quantco / pytsql

Run mssql scripts from Python.
BSD 3-Clause "New" or "Revised" License
14 stars 3 forks source link

How to remove `#pytsql_prints` lines from logs #107

Open jonashaag opened 1 year ago

jonashaag commented 1 year ago

I wonder if there is something that we can implement in pytsql to optionally disable printing of the #pytsql_prints related lines to the SQLAlchemy logger.

jonashaag commented 1 year ago

My current solution

    def log_filter(record):
        msg = record.getMessage()
        return not msg.startswith(
            (
                "DELETE FROM #pytsql_prints",
                "SELECT * FROM #pytsql_prints",
                "CREATE TABLE #pytsql_prints",
                "DROP TABLE IF EXISTS #pytsql_prints",

                # non-pytsql noise
                "COMMIT using DBAPI",
                "[raw sql] ()",
                "SELECT CAST('test max support",
                "SELECT CAST(SERVERPROPERTY('ProductVersion')",
                "SELECT schema_name()",
                "SELECT [INFORMATION_SCHEMA].[TABLES].[TABLE_NAME]",
                "[generated in ",
                "[cached since",
            )
        )

    for handler in logging.getLogger().handlers:
        handler.addFilter(log_filter)
ivergara commented 1 year ago

This is the point of using a log filter. My only suggestion would be to split it in two one for pytsql and the other for the extra noise you want to filter out.