Last Updated: February 25, 2016
·
796
· Harshniket Seta

Python Logging

Just sharing a little bit of knowledge about the in built logging module in Python that I gained in the past few days.

So here are the main players:
1. logger
2. log-records
2. handler
3. filter
4. levels

A logger is a object which is used by the developer to create log-records.Each log that you create in your code is a log-record.They have a format which you can specify and these are then given to the handler. A handler is the interface to the system which is responsible for taking the log-record and putting in your desired output stream.

loggers and handlers both have a level and accept filters.

There are 6 default levels:
NOTSET = 0
DEBUG = 10
INFO = 20
WARN/WARNING = 30
ERROR = 40
CRITICAL/FATAL = 50

A logger can create a log-record with any of theses levels.
for e.g:

logger.debug("this is a debug log")

and

logger.error("this is a error log")

Similarly, handler also accepts a level. But this works more like a filter. When a handler is set on a level, it will only allow log-records of that level or more.Thereby reducing the log-records dumped in the output stream.

loggers and handlers both accept filters. These basically accept a keyword and if your message has the keywork the log-records will be transferred ahead.This is another good way of reducing the log-records that go to the output streams. When applied at the logger level the records don't even reach the handler thereby reducing computation time.

Please tell me if there is something that you want me to clarify more or if the above post needs any correction.