Logging for Mechaphlowers
Logger inside
Mechaphlowers has a logger named mechaphlowers that is used by the library. It can be accessed from any module of the library.
We propose to use it like this in a mechaphlowers module:
| import logging
logger = logging.getLogger(__name__)
# important to define the logger in this way, to avoid circular imports
# also in this way, the output can export the module name as well
# Output example:
# 2025-03-28 21:33:47,634 - mechaphlowers.entities.arrays - DEBUG - Section Array initialized.
# later in the module code
logger.info("This is an info message")
|
Logger outside
The mechaphlowers logger can be used from outside of the library.
We propose to use it like this in an app using the library:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 | # In the main file, before importing mechaphlowers
import logging
Log = logging.getLogger("MyApp")
Log.setLevel(logging.DEBUG)
logging.basicConfig(filename='example.log', filemode="w", level=logging.DEBUG, format='%(asctime)s - %(name)s - %(levelname)s - %(message)s')
Log.debug("This is a debug message")
# In the example.log file:
# 2025-03-28 21:33:41,771 - MyApp - DEBUG - This is a debug message
from mechaphlowers import SectionArray
# In the example.log file:
# 2025-03-28 21:33:42,437 - mechaphlowers - INFO - Mechaphlowers package initialized.
# 2025-03-28 21:33:42,437 - mechaphlowers - INFO - Mechaphlowers version: ...
|
Logger in notebook
1
2
3
4
5
6
7
8
9
10
11
12 | import logging
import sys
logger = logging.getLogger()
logger.setLevel(logging.DEBUG)
logging.basicConfig(format='%(asctime)s | %(levelname)s : %(message)s', level=logging.DEBUG, stream=sys.stdout)
logger.info('Logging is set up.')
# In the stdout:
# 2025-10-27 12:29:17,750 | INFO : Logging is set up.
# 2025-10-27 12:29:18,876 | INFO : Mechaphlowers package initialized.
# 2025-10-27 12:29:18,876 | INFO : Mechaphlowers version: 0.4.0b0
|
Loger helper for debug uses
A helper is available to easily create a logger with the name of the current module for debug purposes:
add_stderr_logger
Helper for quickly adding a StreamHandler to the logger. Useful for
debugging. Inspired by the urllib3 library.
Parameters:
| Name |
Type |
Description |
Default |
level
|
int
|
The logging level to set for the handler. Default is DEBUG.
|
DEBUG
|
Returns:
Examples:
| >>> from mechaphlowers import add_stderr_logger
>>> add_stderr_logger(logging.DEBUG)
>>> # In the example.log file:
>>> # 2025-03-28 21:33:42,437 - mechaphlowers - INFO - Added a stderr logging handler to logger: mechaphlowers
|
Source code in src/mechaphlowers/utils.py
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142 | def add_stderr_logger(
level: int = logging.DEBUG,
) -> logging.StreamHandler:
"""Helper for quickly adding a StreamHandler to the logger. Useful for
debugging. Inspired by the urllib3 library.
Args:
level (int): The logging level to set for the handler. Default is DEBUG.
Returns:
(logging.StreamHandler): the handler after adding it.
Examples:
>>> from mechaphlowers import add_stderr_logger
>>> add_stderr_logger(logging.DEBUG)
>>> # In the example.log file:
>>> # 2025-03-28 21:33:42,437 - mechaphlowers - INFO - Added a stderr logging handler to logger: mechaphlowers
"""
# This method needs to be in this __init__.py to get the __name__ correct
# even if mechaphlowers is vendored within another package.
logger = logging.getLogger("mechaphlowers")
handler = logging.StreamHandler()
handler.setFormatter(
logging.Formatter(
"%(asctime)s - %(name)s - %(levelname)s - %(message)s"
)
)
logger.addHandler(handler)
logger.setLevel(level)
logger.debug("Added a stderr logging handler to logger: %s", __name__)
return handler
|