In Python, the logging
module is used to set up a flexible and powerful logging system for your application. It allows you to record messages during the execution of your program, which can be useful for debugging, monitoring, and analysis. To configure the logging system, you need to define various parameters like loggers, handlers, formatter, and log levels.
Here’s an example of a logging configuration file in INI format:
[loggers]
keys=root, my_logger
[handlers]
keys=console_handler, file_handler
[formatters]
keys=default_formatter
[logger_root]
level=DEBUG
handlers=console_handler
[logger_my_logger]
level=DEBUG
handlers=file_handler
qualname=my_logger
[handler_console_handler]
class=StreamHandler
level=DEBUG
formatter=default_formatter
args=(sys.stdout,)
[handler_file_handler]
class=FileHandler
level=INFO
formatter=default_formatter
args=('app.log',)
[formatter_default_formatter]
format=%(asctime)s - %(name)s - %(levelname)s - %(message)s
datefmt=%Y-%m-%d %H:%M:%S
Explanation:
- The
[loggers]
section lists the logger names, and thekeys
parameter specifies which loggers to configure. In this example, we have two loggers: the root logger and a custom logger called “my_logger.” - The
[handlers]
section lists the handler names, and thekeys
parameter specifies which handlers to configure. Here, we have aconsole_handler
and afile_handler
. - The
[formatters]
section lists the formatter names, and thekeys
parameter specifies which formatters to configure. We have adefault_formatter
defined. - Each logger, handler, and formatter is defined in its respective section (
logger_
,handler_
,formatter_
). Thelevel
parameter sets the log level, and thehandlers
parameter associates the logger with the specified handlers. - The
class
parameter for handlers specifies the handler class to use (e.g.,StreamHandler
for console output andFileHandler
for file output). - The
formatter
parameter for handlers specifies the formatter to use for the associated log messages. - The
args
parameter for handlers specifies any additional arguments to pass when creating the handler instance.
To load and apply this configuration, you can use the logging.config
module:
import logging
import logging.config
logging.config.fileConfig('logging_config.ini')
logger = logging.getLogger('my_logger')
logger.debug('This is a debug message')
logger.info('This is an info message')
logger.warning('This is a warning message')
logger.error('This is an error message')
logger.critical('This is a critical message')
Ensure that the logging_config.ini
file is present in the same directory as your Python script or provides the correct path to the configuration file in the fileConfig
function.
Python logging config example
Let’s try one last approach that should work regardless of the environment. Instead of using logging.config.dictConfig
, we’ll use logging.basicConfig
, which provides a simpler way to configure logging:
import logging
import sys
# Create a custom formatter
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s', datefmt='%Y-%m-%d %H:%M:%S')
# Create a console handler with the custom formatter and set the log level
console_handler = logging.StreamHandler(sys.stdout)
console_handler.setFormatter(formatter)
console_handler.setLevel(logging.DEBUG)
# Create a file handler with the custom formatter and set the log level
file_handler = logging.FileHandler('app.log')
file_handler.setFormatter(formatter)
file_handler.setLevel(logging.INFO)
# Create a custom logger and set the log level
logger = logging.getLogger('my_logger')
logger.setLevel(logging.DEBUG)
# Add the handlers to the logger
logger.addHandler(console_handler)
logger.addHandler(file_handler)
# Now, you can use the logger to record messages
logger.debug('This is a debug message')
logger.info('This is an info message')
logger.warning('This is a warning message')
logger.error('This is an error message')
logger.critical('This is a critical message')
Output:
In this approach, we directly create and configure the handlers, formatter, and logger using the logging
module’s standard functions. This method should work without relying on the logging.config
module or any other external configuration files.
Note: IDE: PyCharm 2021.3.3 (Community Edition)
Windows 10
Python 3.10.1
All Python Examples are in Python 3, so Maybe its different from python 2 or upgraded versions.