Now that we have covered all of the primary features of the logging module, let’s cover another method that allows for the configuration of some components in one easy step.

The basicConfig() method allows for the basic configuration of the logger object by configuring the log level, any handlers, log message formatting options, and more in one line of code. Let’s say we want to simplify our calculator code by using the basicConfig() method. We can use this method in the following way:

logging.basicConfig(filename='calculator.log', level=logging.DEBUG, format='[%(asctime)s] %(levelname)s - %(message)s')

This code automatically adds a FileHandler object that will write to the filename calculator.log, set the log level to DEBUG, and format all logged messages to have a timestamp, log level, and message information.

Suppose we provide no arguments to the basicConfig() method. In that case, a StreamHandler object will automatically be created for the logger with default formatting, and it will log to the default sys.stderr.

After the configuration is set, the logger object is still retrieved using the getLogger(name) method.

If you want to dig more into the basicConfig() method, feel free to peruse The Python documentation here, which covers all the arguments that basicConfig() supports.

Let’s practice using basicConfig() to simplify our logger configurations.



Create a logger that writes to a file called basic_config.log, sets a log level of INFO, and formats the logs with the following format:

[%(asctime)s] %(levelname)s - %(message)s

using basicConfig().

Run the program by typing python3 script.py in the terminal and clicking Enter. Click Check Work once the program is complete.


Execute the calculator code with the following inputs:

  • Dividend = 0
  • Divisor = 0

View the basic_config.log file and verify that the formatting of the logs matches the formatting set previously. Also, verify that there are no DEBUG log messages found within the log file.

Click Check Work once you are ready to move onto the final exercise.

Sign up to start coding

Mini Info Outline Icon
By signing up for Codecademy, you agree to Codecademy's Terms of Service & Privacy Policy.

Or sign up using:

Already have an account?