I understand that Python loggers cannot be instantiated directly, as the documentation suggests:
Note that Loggers are never instantiated directly, but always through the module-level function
logging.getLogger(name)
.. which is reasonable, as you are expected not to create logger objects for every class/module for there is a better alternative.
However, there are cases where I want to create a logger object and attach a file to it exclusively for logging some app-specific output to that file; and then close the log file.
For instance, I have a program that builds all packages in PyPI. So basically assume there is a for
loop going over every package. Inside the loop, I want to "create" a logger, attach a file handler (eg: /var/logs/pypi/django/20090302_1324.build.log and send the output of python setup.py build
(along with other things) to this log file. Once that is done, I want to close/destroy the logger and continue
building other packages in similar fashion.
So you see .. the normal Pythonic way of calling logging.getLogger
does not apply here. One needs to create temporary logger objects.
Currently, I achieve this by passing the file name itself as the logger name:
>>> packagelog = logging.getLogger('/var/..../..34.log')
>>> # attach handler, etc..
I want to ask .. is there a better way to do this?