11.1. Logging About
Do not print
Always use logger
Logs can be displayed on console
Logs can be redirected to file
Logs can be redirected to database
Logs can be redirected to log-server
Logs can be silenced (certain level)
Logs can be rotated
Logs can change format
>>> import logging
>>>
>>>
>>> def run():
... logging.warning('Program start')
... for number in range(0,3):
... logging.info(f'Current number: {number}')
... logging.warning('Program end')
>>>
>>>
>>> run()
WARNING:root:Program start
WARNING:root:Program end
11.1.1. SetUp
>>> import logging
11.1.2. Global Logger
>>> logging.info('My message')
11.1.3. Logger Instance
logging.getLogger(name)- Get logger instance by namemyapp- Name of the loggermyapp
>>> log = logging.getLogger('myapp')
>>> log.info('My message')
11.1.4. Dynamically Named Logger
__name__- Name of the current module
>>> log = logging.getLogger(__name__)
>>> log.info('My message')
11.1.5. Further Reading
11.1.6. Assignments
# %% About
# - Name: Logging About GetLogger
# - 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`
# 2. Define variable `result` with the solution
# 3. Use `logging` built-in module
# 4. Run doctests - all must succeed
# %% Polish
# 1. Pobierz logger o nazwie `myapp`
# 2. Zdefiniuj zmienną `result` z rozwiązaniem
# 3. Use `logging` built-in module
# 4. Uruchom doctesty - wszystkie muszą się powieść
# %% Expected
# %% Hints
# - `logging.getLogger()`
# %% Doctests
"""
>>> import sys; sys.tracebacklimit = 0
>>> assert sys.version_info >= (3, 10), \
'Python has an is invalid version; expected: `3.10` or newer.'
>>> assert 'result' in globals(), \
'Variable `result` is not defined; assign result of your program to it.'
>>> assert result is not Ellipsis, \
'Variable `result` has an invalid value; assign result of your program to it.'
>>> assert isinstance(result, logging.Logger), \
'Variable `result` has an invalid type; expected: `logging.Logger`.'
"""
# %% 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
result: logging.Logger
# %% Data
# %% Result
result = ...
# %% About
# - Name: Logging About GetLogger
# - 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 after current module
# 2. Define variable `result` with the solution
# 3. Use `logging` built-in module
# 4. Run doctests - all must succeed
# %% Polish
# 1. Pobierz logger o nazwie aktualnego modułu
# 2. Zdefiniuj zmienną `result` z rozwiązaniem
# 3. Use `logging` built-in module
# 4. Uruchom doctesty - wszystkie muszą się powieść
# %% Expected
# %% Hints
# - `logging.getLogger()`
# - `__name__`
# %% Doctests
"""
>>> import sys; sys.tracebacklimit = 0
>>> assert sys.version_info >= (3, 10), \
'Python has an is invalid version; expected: `3.10` or newer.'
>>> assert 'result' in globals(), \
'Variable `result` is not defined; assign result of your program to it.'
>>> assert result is not Ellipsis, \
'Variable `result` has an invalid value; assign result of your program to it.'
>>> assert isinstance(result, logging.Logger), \
'Variable `result` has an invalid type; expected: `logging.Logger`.'
"""
# %% 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
result: logging.Logger
# %% Data
# %% Result
result = ...