'Only create logs' directory if logger has file handler
Python's logger library will create the log file if it doesn't exist but not any parent directories.
Our code creates the parent directories regardless of whether the logger is configured to use a FileHandler.
We're now running in a docker, using only console output, and want to avoid creating the directory if there is no file handler installed.
Is there a way to detect this configuration?
Solution 1:[1]
The dictConfig() documentation shows how you can configure handlers of a particular type, and that can be extended to your own handlers.
You could subclass FileHandler and override the emit() method like this:
def emit(self, record):
dn = os.path.dirname(self.baseFilename)
if not os.path.exists(dn):
os.makedirs(dn)
super().emit(record)
and then use this subclass in your logging configuration. However, make sure you instantiate the handler with delay=True and, if you override __init__() in your custom handler, call the parent class' __init__() from there. The delay=True doesn't try to open the file until there is an actual need to write to it; that's done by the FileHandler.emit() method, and by creating the directory in the overridden method before calling the base class' emit() method, you'll only create the directory when it's needed and doesn't already exist.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Vinay Sajip |
