Topics

Configure Log LEVEL, Format etc

Welcome to a tutorial on Logging Basic configurations in Python. The basic config(**kwargs) method where **kwargs in the function definition means that the function takes variable length arguments, that are passed in the key-value form.

 

Parameters used in the basicConfig() function

  • level: This parameter helps the root logger to be set to the specified severity level such as the DEBUG, INFO, ERROR, and so on.
  • filename: This is mainly used to specify the file name if we want to store the logs in a file.
  • filemode: In this case, if the filename is given, then the file is opened in this mode. The default mode is a, which means append.
  • format: It is used to specify the format of the log message. E.g. if you want to add a timestamp with the logs, or the name of the python script, or maybe the function or class name in the logs, then we can specify the appropriate format for it.

In addition, in the basicConfig() function, the level parameter can either be provided with an appropriate value, filename, and filecode for printing logs in the file and the format parameter to specify the logs, or also, all the parameters can be specified. This is the uniqueness of the **kwargs, which just means the number of arguments that can be supplied to a function is not fixed.

 

Example of this method.

import logging

logging.basicConfig(level=logging.INFO)
logging.info('This is an info message.This will get logged.')

Output:

INFO:root: This is an info message. This will get logged.

From above, every event at or above INFO level will be logged.

 

Important points

  • Calling basicConfig() to configure the root logger works in the case only when the root logger has not been configured initially. However, this function can only be called once.
  • Also these functions; debug(), info(), warning(), error(), and critical(), internally call the basicConfig() method without any arguments automatically in case it has not been called initially.
  • Thus, it means that after the first time one of the above functions is called, one can no longer configure the root logger, because they would have called the basic config() function internally.

 

Store Logs in File

Also, the basicConfig() method can be used to store logs in a file. You will learn how to do this in the next tutorial.

 

Set Format of Logs

The logging module can be configured to print logs in any format. In python, there are standard formats that are used universally.

In addition, we have some basic components of logs that are already a part of the LogRecord and we can add them or remove them easily from the output format if necessary.

 

Examples of Logging basic configuration.

1. Add process ID to logs with loglevel and message

For this, if we want to include the process ID for the running Python script along with the log level and message, the code snippet is given below.

import logging

logging.basicConfig(format='%(process)d-%(levelname)s-%(message)s')
logging.warning('This message is a warning')

Output:

1396-WARNING-This message is a warning

From the above example, in the method basicConfig(), we set the format parameter with the format string as a value in which we have specified, and the components we want in our log, along with specifying its datatype, such as d with process to print the integer process id, and then s for loglevel which happens to be a string value and same for the message.

In addition, we can as well set the format using the LogRecord attributes set in any order in our example above.

 

2. Add Timestamp to logs with a log message

The date and time info (timestamp) can be added to your logs along with the log message. Check the example below:

import logging

logging.basicConfig(format='%(asctime)s - %(message)s', level=logging.INFO)
logging.info('This message is to indicate that Admin has just logged in')

Output:

2020-06-19 11:43:59,887 - This message is to indicate that Admin has just logged in

From the example above, the code %(asctime)s typically adds the time of the creation of the LogRecord. the code level=logging.INFO was used to configure the log level.

 

Use the datefmt attribute

Interestinly, when using the datefmt attribute, we can change the format, which uses the same formatting language as the formatting functions in the datetime module, like time.strftime(). Check out the example below.

import logging

logging.basicConfig(format='%(asctime)s - %(message)s', datefmt='%d-%b-%y %H:%M:%S')
logging.warning('The Admin just logged out')

Output:

19-Jun-20 11:50:28 - The Admin just logged out

From the example above, we saw that the log printed as the output of the above code has a date format DD-MM-YY. Alongside with the time.

Finally on this, by making use of the basicConfig(**kwargs) method, we can configure the logs format as we require.