The basicConfig() function of the Python logging
module is used to configure the logging system. It sets the basic configuration of the logging system, including the level of messages to be logged, the format of the log messages, and the destination of the log messages.
This method takes a set of keyword arguments (also called named arguments) that are used to configure the logging system. The keyword arguments include:
level
: This parameter is used to specify the level of messages that should be logged. The default value is logging.WARNING
.format
: This parameter is used to specify the format of the log messages. It uses string format
codes that are replaced with the corresponding values when a log message is emitted.datefmt
: This parameter is used to specify the format of the timestamp in log messages.filename
: This parameter is used to specify the name of the file to which log messages should be written.filemode
: This parameter is used to specify the mode in which the log file should be opened. ‘w’ for overwriting and ‘a’ for appending.stream
: This parameter is used to specify the stream to which log messages should be written. The default value is sys.stderr
.handlers
: This parameter is used to specify the handlers that should be used to output log messages.The basicConfig()
method is usually only called once in the beginning of the application to set the basic configuration of the logging system. Once the basic configuration is set, you can use the logging methods (debug(), info(), warning(), error(), critical()) to log messages.
It’s also possible to configure different formatters and handlers for different loggers, which can provide more granular control over the logging output.
import logging
logging.basicConfig(level=logging.DEBUG,
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
filename='app.log',
filemode='w')
In this example, the basicConfig()
function is used to set the logging level to logging.DEBUG
, the format of the log messages to '%(asctime)s - %(name)s - %(levelname)s - %(message)s'
, and the destination of the log messages to a file named ‘app.log’ with mode ‘w’.
To store logs in a file using the Python logging
module, you can use the basicConfig()
function and set the filename
parameter to the name of the file where you want to store the logs.
The logging module allows you to customize the format of log messages in a variety of ways. The basicConfig() method of the logging module provides a format parameter that can be used to specify the format of log messages.
Here are a few examples of commonly used format strings:
You can also add your own custom fields to logrecord and include them in the format.
logging.basicConfig(level=logging.DEBUG,
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s - %(custom_field)s')
The basicConfig() method can also be used to set the date format, which is used to format the timestamp of the log messages.
logging.basicConfig(level=logging.DEBUG,
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
datefmt='%Y-%m-%d %H:%M:%S')
You can also use the Formatter class to format the log records, where you can set the format, datefmt and other attributes of the formatter.
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
file_handler = logging.FileHandler('app.log')
file_handler.setFormatter(formatter)
logger = logging.getLogger()
logger.addHandler(file_handler)
The format of the logs can be customized as per the requirement. The above examples are just a few commonly used formats, you can experiment with different formats as per your requirement.
Note: also read about Logging Module in Python
Please follow me to read my latest post on programming and technology if you like my post.
https://www.instagram.com/coderz.py/
https://www.facebook.com/coderz.py
Staying up to the mark is what defines me. Hi all! I’m Rabecca Fatima a keen learner, great enthusiast, ready to take new challenges as stepping stones towards flying colors.
You are given a stream of elements that is too large to fit into memory.…
The formula for the area of a circle is given by πr². Use the Monte…
Given an integer k and a string s, write a function to determine the length…
There is a staircase with N steps, and you can ascend either 1 step or…
Build an autocomplete system that, given a query string s and a set of possible…
Design a job scheduler that accepts a function f and an integer n. The scheduler…