coderz.py

Keep Coding Keep Cheering!

Logging basicConfig()function

JOIN Clause

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.

Example:
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’.

Store Logs in File:

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:

  1. %(asctime)s – %(levelname)s – %(message)s: This format string includes the timestamp (%(asctime)s), the log level (%(levelname)s), and the log message (%(message)s).
  2. %(asctime)s – %(levelname)s – %(module)s – %(message)s: This format string includes the timestamp (%(asctime)s), the log level (%(levelname)s), the module name (%(module)s), and the log message (%(message)s).
  3. %(asctime)s – %(levelname)s – %(module)s – %(lineno)d – %(message)s: This format string includes the timestamp (%(asctime)s), the log level (%(levelname)s), the module name (%(module)s), the line number (%(lineno)d), and the log message (%(message)s).

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

Follow Me

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

Leave a Comment

Your email address will not be published. Required fields are marked *

Advertisement