How to print current logging configuration used by the python logging module?

From Simeon’s comment, the logging_tree package lets you print out the details of the current logging configuration. >>> import logging >>> logging.getLogger(‘a’) >>> logging.getLogger(‘a.b’).setLevel(logging.DEBUG) >>> logging.getLogger(‘x.c’) >>> from logging_tree import printout >>> printout() <–“” Level WARNING | o<–“a” | Level NOTSET so inherits level WARNING | | | o<–“a.b” | Level DEBUG | o<–[x] | … Read more

python logging to database

I recently managed to write my own database logger in Python. Since I couldn’t find any example I thought I post mine here. Works with MS SQL. Database table could look like this: CREATE TABLE [db_name].[log]( [id] [bigint] IDENTITY(1,1) NOT NULL, [log_level] [int] NULL, [log_levelname] [char](32) NULL, [log] [char](2048) NOT NULL, [created_at] [datetime2](7) NOT NULL, … Read more

Redirect Python ‘print’ output to Logger

You have two options: Open a logfile and replace sys.stdout with it, not a function: log = open(“myprog.log”, “a”) sys.stdout = log >>> print(“Hello”) >>> # nothing is printed because it goes to the log file instead. Replace print with your log function: # If you’re using python 2.x, uncomment the next line #from __future__ … Read more

Python logging before you run logging.basicConfig?

You can remove the default handlers and reconfigure logging like this: # if someone tried to log something before basicConfig is called, Python creates a default handler that # goes to the console and will ignore further basicConfig calls. Remove the handler if there is one. root = logging.getLogger() if root.handlers: for handler in root.handlers: … Read more

Replace default handler of Python logger

Perhaps the following example will help. Basically you can either remove the handlers of the logger you’d like to disable, or don’t propagate with the logger you are logging from. $ cat testlog.py import logging logging.basicConfig(filename=”foo”, level=logging.DEBUG) root_logger = logging.getLogger() root_logger.debug(‘bar’) my_logger = logging.getLogger(‘my_logger’) FORMAT = “%(process)s %(thread)s: %(message)s” formatter = logging.Formatter(fmt=FORMAT) handler = logging.StreamHandler() … Read more

logging with filters

Just implement a subclass of logging.Filter: http://docs.python.org/library/logging.html#filter-objects. It will have one method, filter(record), that examines the log record and returns True to log it or False to discard it. Then you can install the filter on either a Logger or a Handler by calling its addFilter(filter) method. Example: class NoParsingFilter(logging.Filter): def filter(self, record): return not … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)