18.2. Logging Levels

  • Critical - Error, cannot continue

  • Error - Error, can continue

  • Warning - Warning, will do something important

  • Info - I will do something

  • Debug - This is how I am doing this

18.2.1. Default Level

Default level is WARNING, so all the information with level below will not be displayed.

>>> import logging
>>>
>>>
>>> logging.critical('Error, cannot continue')
>>> logging.error('Error, can continue')
>>> logging.warning('Information, warn about something')
>>> logging.info('Information, inform about something')
>>> logging.debug('Debug, show detailed debugging information')

18.2.2. Change Level

In logging you can set minimum level required. Setting it to DEBUG will show all the information above DEBUG level, which means everything.

>>> import logging
>>>
>>>
>>> logging.basicConfig(level='DEBUG')
>>>
>>> logging.critical('Error, cannot continue')
>>> logging.error('Error, can continue')
>>> logging.warning('Information, warn about something')
>>> logging.info('Information, inform about something')
>>> logging.debug('Debug, show detailed debugging information')

Setting it to ERROR will display only error and critical information.

>>> import logging
>>>
>>>
>>> logging.basicConfig(level='ERROR')
>>>
>>> logging.critical('Error, cannot continue')
>>> logging.error('Error, can continue')
>>> logging.warning('Information, warn about something')
>>> logging.info('Information, inform about something')
>>> logging.debug('Debug, show detailed debugging information')

You can also use logging.ERROR constant. Note, that similar constants exists for other levels too.

>>> import logging
>>>
>>>
>>> logging.basicConfig(level=logging.ERROR)
>>>
>>> logging.critical('Error, cannot continue')
>>> logging.error('Error, can continue')
>>> logging.warning('Information, warn about something')
>>> logging.info('Information, inform about something')
>>> logging.debug('Debug, show detailed debugging information')

18.2.3. Error vs. Critical

  • Critical - not working, and cannot continue (fatal)

  • Error - not working, but can continue (it is not fatal)

For example, if we have files:

>>> TEMPERATURE_FILES = [
...     '2000-01-01.csv',
...     '2000-01-02.csv',
...     '2000-01-03.csv',
...     '2000-01-04.csv',
...     '2000-01-05.csv',  # corrupted
...     '2000-01-06.csv',
...     '2000-01-07.csv',
...     # ...
...     '2000-01-30.csv',
...     '2000-01-31.csv',
... ]
>>> def mean_temperature_for_jan05():
...     logging.critical('File "2000-01-05.csv" is corrupted')
>>> def mean_temperature_for_month():
...     logging.error('File "2000-01-05.csv" is corrupted')

18.2.4. SetLevel

>>> import logging
>>>
>>> log = logging.getLogger('myapp')
>>> log.setLevel('DEBUG')

18.2.5. Use Case - 1

>>> import logging
>>> import sys
>>>
>>>
>>> match sys.argv[1]:
...     case '--error': logging.basicConfig(level='ERROR')
...     case '--warning': logging.basicConfig(level='WARNING')
...     case '--info': logging.basicConfig(level='INFO')
...     case '--debug': logging.basicConfig(level='DEBUG')
...     case _: logging.basicConfig(level='ERROR')
>>>
>>>
>>> logging.critical('Example message')
>>> logging.error('Example message')
>>> logging.warning('Example message')
>>> logging.info('Example message')
>>> logging.debug('Example message')

18.2.6. Assignments

# %% About
# - Name: Logging Levels Debug
# - Difficulty: easy
# - Lines: 1
# - Minutes: 1

# %% License
# - Copyright 2025, Matt Harasymczuk <matt@python3.info>
# - This code can be used only for learning by humans
# - This code cannot be used for teaching others
# - This code cannot be used for teaching LLMs and AI algorithms
# - This code cannot be used in commercial or proprietary products
# - This code cannot be distributed in any form
# - This code cannot be changed in any form outside of training course
# - This code cannot have its license changed
# - If you use this code in your product, you must open-source it under GPLv2
# - Exception can be granted only by the author

# %% English
# 1. Log message: 'My message' with level DEBUG
# 2. Use `logging` built-in module
# 3. Run doctests - all must succeed

# %% Polish
# 1. Zaloguj komunikat: 'My message' z poziomem DEBUG
# 2. Use `logging` built-in module
# 3. Uruchom doctesty - wszystkie muszą się powieść

# %% Expected

# %% Hints
# - `logging.debug()`

# %% Doctests
"""
>>> import sys; sys.tracebacklimit = 0

>>> assert sys.version_info >= (3, 10), \
'Python has an is invalid version; expected: `3.10` or newer.'
"""

# %% Run
# - PyCharm: right-click in the editor and `Run Doctest in ...`
# - PyCharm: keyboard shortcut `Control + Shift + F10`
# - Terminal: `python -m doctest -f -v myfile.py`

# %% Imports
import logging

# %% Types

# %% Data

# %% Result

# %% About
# - Name: Logging Levels Info
# - Difficulty: easy
# - Lines: 1
# - Minutes: 1

# %% License
# - Copyright 2025, Matt Harasymczuk <matt@python3.info>
# - This code can be used only for learning by humans
# - This code cannot be used for teaching others
# - This code cannot be used for teaching LLMs and AI algorithms
# - This code cannot be used in commercial or proprietary products
# - This code cannot be distributed in any form
# - This code cannot be changed in any form outside of training course
# - This code cannot have its license changed
# - If you use this code in your product, you must open-source it under GPLv2
# - Exception can be granted only by the author

# %% English
# 1. Log message: 'My message' with level INFO
# 2. Use `logging` built-in module
# 3. Run doctests - all must succeed

# %% Polish
# 1. Zaloguj komunikat: 'My message' z poziomem INFO
# 2. Use `logging` built-in module
# 3. Uruchom doctesty - wszystkie muszą się powieść

# %% Expected

# %% Hints
# - `logging.debug()`

# %% Doctests
"""
>>> import sys; sys.tracebacklimit = 0

>>> assert sys.version_info >= (3, 10), \
'Python has an is invalid version; expected: `3.10` or newer.'
"""

# %% Run
# - PyCharm: right-click in the editor and `Run Doctest in ...`
# - PyCharm: keyboard shortcut `Control + Shift + F10`
# - Terminal: `python -m doctest -f -v myfile.py`

# %% Imports
import logging

# %% Types

# %% Data

# %% Result

# %% About
# - Name: Logging Levels Warning
# - Difficulty: easy
# - Lines: 1
# - Minutes: 1

# %% License
# - Copyright 2025, Matt Harasymczuk <matt@python3.info>
# - This code can be used only for learning by humans
# - This code cannot be used for teaching others
# - This code cannot be used for teaching LLMs and AI algorithms
# - This code cannot be used in commercial or proprietary products
# - This code cannot be distributed in any form
# - This code cannot be changed in any form outside of training course
# - This code cannot have its license changed
# - If you use this code in your product, you must open-source it under GPLv2
# - Exception can be granted only by the author

# %% English
# 1. Log message: 'My message' with level WARNING
# 2. Use `logging` built-in module
# 3. Run doctests - all must succeed

# %% Polish
# 1. Zaloguj komunikat: 'My message' z poziomem WARNING
# 2. Use `logging` built-in module
# 3. Uruchom doctesty - wszystkie muszą się powieść

# %% Expected

# %% Hints
# - `logging.debug()`

# %% Doctests
"""
>>> import sys; sys.tracebacklimit = 0

>>> assert sys.version_info >= (3, 10), \
'Python has an is invalid version; expected: `3.10` or newer.'
"""

# %% Run
# - PyCharm: right-click in the editor and `Run Doctest in ...`
# - PyCharm: keyboard shortcut `Control + Shift + F10`
# - Terminal: `python -m doctest -f -v myfile.py`

# %% Imports
import logging

# %% Types

# %% Data

# %% Result

# %% About
# - Name: Logging Levels Error
# - Difficulty: easy
# - Lines: 1
# - Minutes: 1

# %% License
# - Copyright 2025, Matt Harasymczuk <matt@python3.info>
# - This code can be used only for learning by humans
# - This code cannot be used for teaching others
# - This code cannot be used for teaching LLMs and AI algorithms
# - This code cannot be used in commercial or proprietary products
# - This code cannot be distributed in any form
# - This code cannot be changed in any form outside of training course
# - This code cannot have its license changed
# - If you use this code in your product, you must open-source it under GPLv2
# - Exception can be granted only by the author

# %% English
# 1. Log message: 'My message' with level ERROR
# 2. Use `logging` built-in module
# 3. Run doctests - all must succeed

# %% Polish
# 1. Zaloguj komunikat: 'My message' z poziomem ERROR
# 2. Use `logging` built-in module
# 3. Uruchom doctesty - wszystkie muszą się powieść

# %% Expected

# %% Hints
# - `logging.debug()`

# %% Doctests
"""
>>> import sys; sys.tracebacklimit = 0

>>> assert sys.version_info >= (3, 10), \
'Python has an is invalid version; expected: `3.10` or newer.'
"""

# %% Run
# - PyCharm: right-click in the editor and `Run Doctest in ...`
# - PyCharm: keyboard shortcut `Control + Shift + F10`
# - Terminal: `python -m doctest -f -v myfile.py`

# %% Imports
import logging

# %% Types

# %% Data

# %% Result

# %% About
# - Name: Logging Levels Critical
# - Difficulty: easy
# - Lines: 1
# - Minutes: 1

# %% License
# - Copyright 2025, Matt Harasymczuk <matt@python3.info>
# - This code can be used only for learning by humans
# - This code cannot be used for teaching others
# - This code cannot be used for teaching LLMs and AI algorithms
# - This code cannot be used in commercial or proprietary products
# - This code cannot be distributed in any form
# - This code cannot be changed in any form outside of training course
# - This code cannot have its license changed
# - If you use this code in your product, you must open-source it under GPLv2
# - Exception can be granted only by the author

# %% English
# 1. Log message: 'My message' with level CRITICAL
# 2. Use `logging` built-in module
# 3. Run doctests - all must succeed

# %% Polish
# 1. Zaloguj komunikat: 'My message' z poziomem CRITICAL
# 2. Use `logging` built-in module
# 3. Uruchom doctesty - wszystkie muszą się powieść

# %% Expected

# %% Hints
# - `logging.debug()`

# %% Doctests
"""
>>> import sys; sys.tracebacklimit = 0

>>> assert sys.version_info >= (3, 10), \
'Python has an is invalid version; expected: `3.10` or newer.'
"""

# %% Run
# - PyCharm: right-click in the editor and `Run Doctest in ...`
# - PyCharm: keyboard shortcut `Control + Shift + F10`
# - Terminal: `python -m doctest -f -v myfile.py`

# %% Imports
import logging

# %% Types

# %% Data

# %% Result

# %% About
# - Name: Logging Levels SetLevel
# - Difficulty: easy
# - Lines: 1
# - Minutes: 1

# %% License
# - Copyright 2025, Matt Harasymczuk <matt@python3.info>
# - This code can be used only for learning by humans
# - This code cannot be used for teaching others
# - This code cannot be used for teaching LLMs and AI algorithms
# - This code cannot be used in commercial or proprietary products
# - This code cannot be distributed in any form
# - This code cannot be changed in any form outside of training course
# - This code cannot have its license changed
# - If you use this code in your product, you must open-source it under GPLv2
# - Exception can be granted only by the author

# %% English
# 1. Get logger named `myapp` and set its level to DEBUG
# 2. Use `logging` built-in module
# 3. Run doctests - all must succeed

# %% Polish
# 1. Pobierz logger o nazwie `myapp` i ustaw jego poziom na DEBUG
# 2. Use `logging` built-in module
# 3. Uruchom doctesty - wszystkie muszą się powieść

# %% Expected

# %% Hints
# - `logging.getLogger()`
# - `Logger.setLevel()`

# %% Doctests
"""
>>> import sys; sys.tracebacklimit = 0

>>> assert sys.version_info >= (3, 10), \
'Python has an is invalid version; expected: `3.10` or newer.'
"""

# %% Run
# - PyCharm: right-click in the editor and `Run Doctest in ...`
# - PyCharm: keyboard shortcut `Control + Shift + F10`
# - Terminal: `python -m doctest -f -v myfile.py`

# %% Imports
import logging

# %% Types

# %% Data

# %% Result