Python Module logging Concepts
Internal
Overview
The standard Python library module to provide logging support is logging
.
import logging
logging.basicConfig(level=logging.DEBUG)
some_logger = logging.getLogger('some-logger')
logging.debug('debugging')
some_logger.debug('debugging via some_logger')
The output is:
DEBUG:root:debugging
DEBUG:some-logger:debugging via some_logger
Logging Levels
The logging system can be configured to filter out log messages whose logging level is under a certain threshold. The logging levels, and the corresponding logging functions are, listed from the most important to the least important, are provided below. By default, the logging system is configured to allow log messages of level WARNING and above, INFO and below are discarded. A different logging threshold can be configured.
Standard Logging Levels
CRITICAL
Accessible as logging.CRITICAL
. Has numeric value 50. Generated with logging.critical()
.
ERROR
Accessible as logging.ERROR
. Has numeric value 40. Generated with logging.error()
.
WARNING
Accessible as logging.WARNING
. Has numeric value 30. Generated with logging.warning()
.
WARN seems to be deprecated. warn()
is deprecated.
INFO
Accessible as logging.INFO
. Has numeric value 20. Generated with logging.info()
.
DEBUG
Accessible as logging.DEBUG
. Has numeric value 10. Generated with logging.debug()
.
NOTSET
Accessible as logging.NOTSET
. Has numeric value 0.
Setting Logging Level
import logging
logging.basicConfig(level=logging.DEBUG)
Logger
The logger is the programmatic way to generate logging messages. By default, logging.debug()|info()|...
syntax implies the usage of the root logger, but custom loggers can be created.
Logger Hierarchy
If the logger name contains dot characters, they separate levels of a hierarchy. Closer to the left, the higher the logger in the logger hierarchy: a
has a higher level than a.b
. Each level in the hierarchy can be given different properties. At the top of the hierarchy there's a root logger.
The root Logger
At the top of the logger hierarchy, there is a special root logger, called ' '.
Custom Loggers
Custom loggers can be created with logging.getLogger()
function. Each logger has a name, and the presence of dots in the name place the logger in the logger hierarchy.
import logging
some_logger = logging.getLogger('some.logger')
some_other_logger = logging.getLogger('some.other.logger')
Handler
The handlers direct logging messages to their destinations (terminal, file, database, etc.)
Formatter
A formatter transforms the log message according to a certain pattern.
Format
The default format, without any customization is <LOGGING_LEVEL>:<logger_name>:<message>
.