Python logging

We can track events in a software application, this is known as logging. Let’s start with a simple example, we will log a warning message.

As opposed to just printing the errors, logging can be configured to disable output or save to a file. This is a big advantage to simple printing the errors.

Related course
Complete Python Bootcamp: Go from zero to hero in Python

Logging example

import logging

# print a log message to the console.
logging.warning('This is a warning!')

This will output:

WARNING:root:This is a warning!

We can easily output to a file:

import logging

logging.warning('An example message.')
logging.warning('Another message')

The importance of a log message depends on the severity.

Level of severity

The logger module has several levels of severity. We set the level of severity using this line of code:


These are the levels of severity:

The default logging level is warning, which implies that other messages are ignored. If you want to print debug or info log messages you have to change the logging level like so:

Type Description
DEBUG Information only for problem diagnostics
INFO The program is running as expected
WARNING Indicate something went wrong
ERROR The software will no longer be able to function
CRITICAL Very serious error
import logging

logging.debug('Debug message')

Time in log

You can enable time for logging using this line of code:

logging.basicConfig(format='%(asctime)s %(message)s')

An example below:

import logging

logging.basicConfig(format='%(asctime)s %(message)s', level=logging.DEBUG)
logging.info('Logging app started')
logging.warning('An example logging message.')
logging.warning('Another log message')


2015-06-25 23:24:01,153 Logging app started
2015-06-25 23:24:01,153 An example message.
2015-06-25 23:24:01,153 Another message


9 thoughts on “Logging

  1. Gloomshroud
    - October 16, 2015

    I’m doing this on a Linux machine (Linux Mint 17.2, specifically) and you have to run this with superuser privileges in order for it to work. 🙂 Most will know that…but I’m a Linux noob so I thought I would point it out just in case.

    So, to run it:

    sudo python your_file_name.py

    1. Frank
      - October 17, 2015


  2. Dubravko
    - July 12, 2015

    I’m using Python 3.4 and do not get any program.log file inside of my .py folder nor anywhere else in the user folder. Can you tell me why?

    Also, I would like to know is there a logging level where you can set to pick all logged messages except INFO or something like that?

    1. Frank
      - July 13, 2015

      Hi, this seems to be something platform specific. I tried the code on Python 3.4 and it outputed the file. Which operating system / platform do you use? You could try hardcoding the path to the file. Does logging to the console work?

      If you want to ignore info you could simply raise the logging level. The warning, error and critical level will ignore the info and debug messages.

  3. Tim
    - July 4, 2015

    logging.basicConfig(filename=’program.log’,level=logging.DEBUG) #I don’t understand this line .where can I find the file’program.log’?

    1. Frank
      - July 4, 2015

      Hi Tim, this file is created when running the program. You will find it in the same directory of your python program after running.

  4. Christophe Razafimahatratra
    - June 27, 2015

    Got this error ‘module’ object has no attribute ‘warning’

    1. Frank
      - June 27, 2015

      Hi, which code and python version are you using?

      1. Christophe Razafimahatratra
        - June 28, 2015

        Hi Frank,
        I use 2.7.9. I found it. It’s due to my filename “logging.py’
        So if I understand, It’s not good to give the same name as a library to my own file.
        I got the same problem with example on random numbers.

        Thank you so much.

Leave a Reply

Login disabled