Python Logging: Effective Error Tracking and Debugging

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:
Logger: Provides the interface that applications directly use. Handler: Sends (logger-created) log records to appropriate output destinations. Filter: Provides fine-grained control over which log records are output. Formatter: Determines the final output format of log records.
**Console and File Output Example:**
import logging # Create a logger instance app_logger = logging.getLogger('application') # Create console handler with WARNING level console_handler = logging.StreamHandler() console_handler.setLevel(logging.WARNING) # Create file handler with INFO level file_handler = logging.FileHandler('application_events.log') file_handler.setLevel(logging.INFO) # Create formatter and add it to handlers log_formatter = logging.Formatter('%(asctime)s %(name)s- %(levelname)s - %(message)s') console_handler.setFormatter(log_formatter) file_handler.setFormatter(log_formatter) # Add handlers to logger app_logger.addHandler(console_handler) app_logger.addHandler(file_handler) # Application logging app_logger.debug('debug message') app_logger.info('informational message') app_logger.warning('warning message') app_logger.error('error message') app_logger.critical('critical message')
Console output:
2023-05-15 14:30:22,123 application- WARNING - warning message 2023-05-15 14:30:22,123 application- ERROR - error message 2023-05-15 14:30:22,123 application- CRITICAL - critical message
Content of application_events.log file:

Tags: python logging error tracking debugging programming

Posted on Fri, 08 May 2026 11:49:05 +0000 by airdee