Basic Configurations in Logging

Basic Configurations

The logging module offers a function called ‘basicConfig’ which allows us to configure how we log our code. This function has quite a few parameters which we can alter to modify the default behavior of our logs.

We saw the debug(), info(), warning(), error(), and critical() function in the previous blog, but what is interesting is that these functions also call the basicConfig() function but without modifying any of the parameters, or in other words they use the default arguments. The basicConfig() function must be called before calling any of the severity level functions.

The basicConfig() however can only be configured if the root logger has not been modified before. Or in other words, we cannot use this function more than once in a program

Let’s look at a basic example of a code that uses ‘basicConfig()’:

import logging

    level=logging.DEBUG,  # minimum severity level
    filename="log_file.txt",  # name of a file to write our log on to
    filemode="w",  # the mode of opening the file
    format=f'%(name)s - %(levelname)s - %(message)s'  # format the output


Don’t be confused in the way we used the basicConfig() function. It’s just another way of writing the following code:

logging.basicConfig(level=logging.DEBUG, filename="log_file.txt", filemode="w", format=f'%(name)s - %(levelname)s - %(message)s')

However, the first way of calling the basicConfig() function is more readable and also allows us to write comments on our code. 

The “level” parameter allows us to specify the minimum level of severity so that all log messages will include messages from this severity level onwards. Based on our code every event that is of level ‘DEBUG’ or more will be logged.

The “filename” parameter stores the name of the file where we want to store our log and like file handling, we must also mention the extension such as (.txt). Depending on the “filemode” we provide, it can create a file, modify, append or write to a file.
Since we used “w” (like the regular file handling mode) it will check if such a file exists and if not, it will create it and write to it. Since it is in write mode, all the logs will be overwritten every time the program is executed.

Thus a file called “log_file.txt” will be created and the log will be written in it.

The “format” parameter allows us to modify how the log is displayed. As seen in the previous blog the output which was separated by ‘:’, first had the severity level then the name, and finally the message. Here we have changed it to be separated by ‘ – ’, with the name coming first and the severity level.

“%(variable-name)s” is a way to insert values into a string.

Some of the other things we can call in the format parameter are:

  • asctime – Gives us a human-readable time of when the log was created.
  • funcName – Gives us the name of the function containing the log.
  • levelno – Gives us the numeric severity level of the log.
  • lineno – Gives us the line number from the source where the log was called.
  • pathname – Gives us the full name of the source file where the log was called.
  • threadName – Gives us the name of the Thread if any.


What have we learned?

  • What is the use of the basicConfig() function?
  • When and how many times can the basicConfig() function be called?
  • What is the use of the level parameter in the basicConfig() function?
  • What are the use and default behavior of the format parameter?
  • What are some things we can display in the format parameter?
Notify of
Inline Feedbacks
View all comments