Windows 10 Python 3.6
directory example
| -- main.py
|-- modules/
| |--_init__.py
| ` -- funcs.py
`-- log/
`-- root.log
If you want to log from multiple modules (from main.py and from funcs.py),
https://docs.python.jp/3/howto/logging.html#logging-from-multiple-modules
If you follow the instructions in , you can output the log without any problems.
However, if you want to customize the rotation method, visit
main.py.
https://docs.python.jp/3/howto/logging-cookbook.html#using-a-rotator-and-namer-to-customize-log-rotation-processing
After writing and giving the logger a handler, the log from main.py has been customized for rotation.
If I want to apply the same customization to logs from funcs.py, how should I write it on funcs.py (I want to print it to the same log file /log/root.log
)?
PermissionError: [WinError32]
if you describe it exactly like main.py.
+α
If possible, logging.config.fileConfig(...)
will make the program smoother, but if you want to apply rotation customization, is there no way to do it?
main.py
import logging
import modules.funcs
logger = logging.getLogger()
logger.setLevel(logging.DEBUG)
trh=logging.handlers.TimedRotatingFileHandler(...)
trh.rotator=rotator#rotator is created separately
logger.addHandler(trh)
logger.debug('rotate test')
funcs.py
import logging
logger = logging.getLogger()
# Do you want to set/describe any settings here?
logger.debug('rotate [email protected]')
funcs.py
should not have to do anything. If you configure something in funcs.py
, it will be a different logger object, so if you try to print it to the same log file, you will get PermissionError: WinError32
.
·Official document logging --- logging function for Python Logger object
Note that loggers cannot be instantiated directly and are always instantiated through the module function logging.getLogger(name).Calling getLogger() multiple times with the same name always returns a reference to the same logger object.
The name is a potentially hierarchical value, divided by periods (but may be just plain foo), such as foo.bar.baz.The lower loggers in the hierarchical list are children of the higher loggers.For example, when there is a logger named foo, all the loggers named foo.bar, foo.bar.baz, foo.bam are descendants of foo.The hierarchy of logger names is similar to the Python package hierarchy, and if you configure the logger on a per-module basis using the recommended build method logging.getLogger(__name__)
, it will be identical to the Python package hierarchy.This is because __name__
is the module name in the Python package namespace.
Does PermissionError mean you are running multiple processes?
In that case, Logging from multiple processes to a single file
introduces the following techniques:fileConfig
in logging.config
is easy to configure if you have a configuration file.
load
) makes the description easier to understand and more convenientRotatingFileHandler
or TimedRotatingFileHandler
) is easy to use, but handling customized handlers tends to be troublesomeThat's what it looks like
But first, I'll modularize the custom logging handler.
Why don't we start by import
and running each program (each process)?
© 2024 OneMinuteCode. All rights reserved.