Skip to content

Python logging level

  • by

The python logging level is related to the “importance” of the log. For example, an “error” log is a top priority and should be considered more urgent than a “warn” log. A “debug” log is usually only useful when the application is being debugged.

Python has six log levels with each one assigned a specific integer indicating the severity of the log:

  • NOTSET=0
  • DEBUG=10
  • INFO=20
  • WARN=30
  • ERROR=40
  • CRITICAL=50

Python logging level example

Simple example code. The logging module has simple methods that can be used right away without any configuration.

import logging

logging.debug('This is a debug message')
logging.info('This is an info message')
logging.warning('This is a warning message')
logging.error('This is an error message')
logging.critical('This is a critical message')

Output:

Python logging level example

Note: that a root logger is used and only three messages were written. This is because, by default, only messages with level warnings and up are written.

Logging into a file

import logging
logging.basicConfig(filename='example.log', encoding='utf-8', level=logging.DEBUG)
logging.debug('This message should go to the log file')
logging.info('So should this')
logging.warning('And this, too')
logging.error('And non-ASCII stuff, too, like Øresund and Malmö')

The standard levels and their applicability are described below (in increasing order of severity):

LevelWhen it’s used
DEBUGDetailed information, is typical of interest only when diagnosing problems.
INFOConfirmation that things are working as expected.
WARNINGAn indication that something unexpected happened, or indicative of some problem in the near future (e.g. ‘disk space low’). The software is still working as expected.
ERRORDue to a more serious problem, the software has not been able to perform some functions.
CRITICALA serious error indicates that the program itself may be unable to continue running.

Source: https://docs.python.org/3/howto/logging.html

Do comment if you have any doubts or suggestions on this Python log topic.

Note: IDE: PyCharm 2021.3.3 (Community Edition)

Windows 10

Python 3.10.1

All Python Examples are in Python 3, so Maybe its different from python 2 or upgraded versions.

Leave a Reply

Discover more from Tutorial

Subscribe now to keep reading and get access to the full archive.

Continue reading