Windows 10 Python 3.6
If you want to log from multiple modules (from main.py and from funcs.py),
If you follow the instructions in , you can output the log without any problems.
However, if you want to customize the rotation method, visit
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
PermissionError: [WinError32] if you describe it exactly like main.py.
logging.config.fileConfig(...) will make the program smoother, but if you want to apply rotation customization, is there no way to do it?
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
·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 fileintroduces the following techniques:
logging.config is easy to configure if you have a configuration file.
load) makes the description easier to understand and more convenient
TimedRotatingFileHandler) is easy to use, but handling customized handlers tends to be troublesome
That'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)?
© 2022 OneMinuteCode. All rights reserved.