11.2. Logging About

  • Always use logger

  • Always name your logger

11.2.1. SetUp

>>> import logging

11.2.2. Root Logger

>>> logging.info('My message')

11.2.3. Logger Instance

  • logging.getLogger() - Get logger instance

>>> log = logging.getLogger()
>>> log.info('My message')

11.2.4. Named Logger

  • logging.getLogger(name) - Get logger instance by name

  • myapp - Name of the logger myapp

>>> log = logging.getLogger('myapp')
>>> log.info('My message')

11.2.5. Dynamically Named Logger

  • __name__ - Name of the current module

>>> log = logging.getLogger(__name__)
>>> log.info('My message')

11.2.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 = ...