Many applications require logging functionality to capture various types of information including regular access logs, errors, warnings, and other output messages. Python's logging module provides a standardized interface for storing logs in various formats. The module supports different log levels organized hierarchically: critical > error > warning > info > debug
Each log level serves a specific purpose in application monitoring and troubleshooting:
**Basic logging to console:**
>>> import logging
>>> logging.debug('debug message')
>>> logging.info('informational message')
>>> logging.warning('warning message')
WARNING:root:warning message
>>> logging.error('error message')
ERROR:root:error message
>>> logging.critical('critical message')
CRITICAL:root:critical message
>>>
By default, only messages with WARNING level or higher are displayed.
**Configuring log level, format, and output destination:**
import logging
logging.basicConfig(filename='application.log', level=logging.DEBUG,
format='%(asctime)s %(filename)s[line:%(lineno)d] %(message)s',
datefmt='%Y-%m-%d')
logging.info('informational message')
logging.debug('debug message')
logging.warning('warning message')
logging.error('error message')
logging.critical('critical message')
This creates an application.log file in the current directory with the following content:
The logging.basicConfig() function allows customization of the default logging behavior through several parameters:
filename: Creates a FileHandler with the specified filename, storing logs in that file.
filemode: File opening mode when filename is specified, defaults to "a" (append), can also be "w" (write).
format: Specifies the log display format for handlers.
datefmt: Defines the date and time format.
level: Sets the logging level for the root logger.
stream: Creates a StreamHandler with the specified stream. Can direct output to sys.stderr, sys.stdout, or a file. Defaults to sys.stderr. When both filename and stream are specified, stream is ignored.
The format parameter supports various placeholders:
%(name)s: Logger's name
%(levelno)s: Numeric log level
%(levelname)s: Textual log level
%(pathname)s: Full path of the module calling the logging function, may be unavailable
%(filename)s: Filename of the module calling the logging function
%(module)s: Module name calling the logging function
%(funcName)s: Function name calling the logging function
%(lineno)d: Line number where the logging function was called
%(created)f: Current time in UNIX timestamp format (float)
%(relativeCreated)d: Milliseconds since the logger was created
%(asctime)s: Current time as string, default format "2003-07-08 16:49:45,896" (milliseconds after comma)
%(thread)d: Thread ID, may be unavailable
%(threadName)s: Thread name, may be unavailable
%(process)d: Process ID, may be unavailable
%(message)s: User-provided message
For more flexible logging control, it's essential to understand the core components: