Log to a File in Python

Log to file in Python
Logging messages to a file in Python. Image design by Elias Ervast

There is a built-in module called log to create a log file in Python.

To log a message to a separate text file by configuring the root logger of the logging module and logging a debugging message:

import logging logging.basicConfig(filename=”log.txt”, level=logging.DEBUG) logging.debug(“Debug logging test…” )

As a result, you will see a file called log.txt in the same folder with your program. This file should have a message similar to this:

DEBUG:root:Test debug log…

In Python, you can log information about problems in a separate file. This file can later help you diagnose problems. It leaves a trail of breadcrumbs that leads to the root of the problem.

In this comprehensive guide, you’ll learn how to work with the logging module and how to set up a logger.

Login to Python: A Detailed Tour

Let’s take a more detailed tour of logging and log files in Python.

Logging means tracking the events and state of your application as it runs. It’s a crucial method of developing, debugging, and running software.

If you don’t keep track of your program’s logs, you may not be able to diagnose problems correctly. This is because without logging there is no trail leading to the potential root cause of a problem.

With the right logging set up, you can follow the breadcrumb trail to the source of a problem.

Why not print

While printing is a great strategy for debugging code, it may not always be the best option. This is especially true when the program is more complex.

The problem with printing is that the prints are not stored anywhere. If your application throws an error, there is no trace of that error, unless the error message is logged somewhere.

Therefore, you may want to log your program’s data in a separate log file.

In Python, there is a built-in library called logging for this purpose.

Getting started with logging messages in Python

Getting started with logging messages in a file, you need to know how to configure a logger.

To do this, you need to

  1. Import the logging module
  2. Configure the logger using the basicConfig() method. The rest of the steps describe how.
  3. Specify the file to which log messages are sent.
  4. Set the “severity” level of log messages.
  5. Format the log messages.
  6. Add or overwrite the old log messages in the file.
See Also:  How to create a website and purchase a url

Let’s start building a logger while going through these six steps in detail.

1. Import Python’s built-in logging module

To use Python’s built-in logging feature, start by importing the logging module.

import logging

2. Call basicConfig() to start configuring the logger.

To start logging messages to a file in Python, you need to configure the logging module.

To configure the logging module , call logging .basicConfig():

import logging logging.basicConfig()

An empty basicConfig() call does nothing useful. In the following steps, you’ll add parameters to this call to actually configure the logger.

3. Register Target File in Python

The basicConfig() method accepts a parameter called filename. This is a string that specifies the destination file for log messages.

For example, let’s start logging messages to a file called log.txt.

import logging logging.basicConfig(filename =”log .txt”)

Now the logger knows the target file and can start logging messages to it.

But before logging our first message, we need to specify the “severity” level ” of messages we want to trace.

Let’s take a look at the levels of log messages in Python.

4. Log Message Levels

There are five main levels of debugging messages at login. These levels describe the “severity” of the problem.

The message levels are:

  1. DEBUG. Used to give detailed information. This level is used primarily to diagnose problems in code.
  2. INFO. Confirms that the program works as expected.
  3. WARNING. An indication that an unexpected event has occurred or may occur.
  4. ERROR. Serious problem. Indicates that a program was unable to perform some action because of an error.
  5. CRITICAL. A serious mistake. Indicates that the program may not be able to continue running.

Logging levels are integers in the background. Here’s a table

Logging levels by logging module.

When you specify a logging level, the program logs messages from that level.

For example, if you use logging.WARNING, only WARNING, ERROR, CRITICAL level messages are logged.

To log a message at a specific level, use the built-in logging methods that correspond to the levels specified above.

See Also:  How to Create an App Like Swiggy?

Here are the logging methods:


  • logging.debug(message). Logs a message at a DEBUG level.
  • logging.info(message). Logs a message at an INFO level.
  • logging.warning(message). Logs a message at a WARNING level.
  • logging.error(message). Logs a message at an ERROR level.
  • logging.critical(message). Logs a message at a CRITICAL level.
  • Let’s reconfigure our logger. Let’s specify the message level as DEBUG to log all possible messages to the file:

    import logging logging.basicConfig(filename=”log.txt”, level=logging.DEBUG)

    Now you can log your first message in the log.txt file. To do this, add the following line after the basicConfig() call:

    logging.debug(“Debug logging test…”)

    Then run the program and see a new file appear named log.txt appears in your project folder. Open the file and you should see this message in the file:

    DEBUG:root:Debug logging test…

    And there you have it. Now that your logging message level is set to DEBUG, you can use all the other logging methods to log more “fatal” errors. This is what the whole program would look like now:

    import logging logging.basicConfig(filename=”log.txt”, level=logging.DEBUG) logging.debug(“Debug logging test…”) logging. info(“The program works as expected”) logging.warning(“Warning, the program may not work correctly”) logging.error(“The program encountered an error”) logging.critical(“The program failed”)

    Running this snippet logs all the different levels of messages to the log.txt file, which looks like this:

    DEBUG:root:Test debug log… DEBUG:root:Test log debug… INFO: root:The program works as expected WARNING:root:Warning, the program may not work correctly ERROR:root:The program encountered an error CRITICAL:root:The program failed

    5. How to format log messages

    Now all messages are formatted like this:



    • The first element is the message level. For example DEBUG.
    • The second element is the logger object. In this guide, we use the root logger, so it returns the root.
    • The third element is the logged message.

    But the format could include something more informative , such as time and message.

    To get rid of the default format, you can customize the format by specifying the format key in the basicConfig() call.

    For example, format messages so that only the time and message are displayed. Let’s modify the basicConfig() call:

    logging.basicConfig(filename=”log.txt”, level=logging.DEBUG, format=”%(asctime)s %(message)s”)

    See Also:  How to create a website that you upload images

    Now you can run the whole program again:

    import logging logging.basicConfig(filename=”log.txt”, level=logging.DEBUG, format=”%(asctime)s %(message)s”) logging.debug( “Test logging…”) logging.info(“The program is working as expected”) logging.warning(“The program may not be working correctly”) logging.error(“The program encountered an error”) logging.critical ( “The program failed”)

    Now the output is formatted with the timestamp just as we wanted:

    2021-09-28 19:42:54,461 Log test… 2021-09-28 19 :42:54,461 The program works as expected 2021-09-28 19:42:54,461 The program may not work correctly 2021-09-28 19:42:54,461 The program encountered an error 2021-09-28 19: 42:54,461 Program crashed

    (The integer 461 is the number of milliseconds this operation took ration).

    Note that this is not the only information you can include in the logged message. Here’s a complete list of all the information you can display about the message.

    6. Add or overwrite the log file in Python

    Try running the sample logging program several times. If you open the file, you can see the errors that are added to the file. The logs from the previous runs are still there.

    This is because, by default, the logger is configured to add messages to the log file.

    You can also change this by specifying the file mode in the basicConfig() call.

    If you want to overwrite the logs instead of adding them, specify the file mode as “w”:

    logging.basicConfig(filename=” log.txt “, level=logging.DEBUG, format=”%(asctime)s %(message)s”, filemode=”w”)

    If you now run the program, the new logs will overwrite the old ones.

    One last time here is the complete program:

    import logging logging.basicConfig(filename=”log.txt”, level=logging.DEBUG, format=”%(asctime)s %(message) s “, filemode=”w”) logging.debug(“Test logging…”) logging.info(“The program works as expected”) logging.warning(“The program may not work correctly”) logging.error( “The program encountered an error”) loggi ng.critical(“The program failed”)


    Today you learned how to create a log file in Python and how to log messages from different levels.

    A log file it can be useful for tracking down bugs and issues in your code.A properly configured log file leads to the source of an error quite easily.

    Thanks for reading. I hope you find it useful.

    Happy coding!

    Further reading

    Python Interview Questions and Answers

    Advanced Features useful Python


    Leave a Reply

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