How To Refer To A Standard Library In A Logging Configuration File?
Solution 1:
As documented in this section of the docs, the values are evaluated in the logging
package's namespace. Hence, you can do something like this:
import logging
import socket
# The next line allows 'socket' in the logging package's namespace to pick up# the stdlib socket module
logging.socket = socket
...
# when the config file is processed, it should work as expected
logging.config.fileConfig(...)
# remove the mapping from the logging package, as not needed any more# (optional)del logging.socket
Solution 2:
First solution (should work but doesn't)
Well here there is a partial answer: https://docs.python.org/3.4/library/logging.config.html#access-to-external-objects
So I tried this:
args=('/dev/log','mathmaker','ext://socket.SOCK_STREAM')
But it does not work:
Traceback (most recent call last):
File "/usr/lib/python3.4/logging/__init__.py", line 1878, in shutdown
h.close()
File "/usr/lib/python3.4/logging/handlers.py", line 857, incloseself.socket.close()
AttributeError: 'SysLogHandler' object has no attribute 'socket'
It's like python expects the 'external' object to be an attribute of the class declared in the handler section (e.g. here: class=logging.handlers.SysLogHandler
).
Second solution (but requires to turn the config file into yaml):
So, as the mechanism that seems dedicated to solve this problem does not work, I have tried with a configuration file written in yaml, and now it works. It requires to add a dependency (python-yaml or python3-yaml for ubuntu users...) and to load the configuration file as a dictionary:
with open(settings.logging_conf_file) as f:
logging.config.dictConfig(yaml.load(f))
and this way, it works.
Here is the same configuration file turned into working yaml (and notice that: 1. the import of socket
is not required in the main script, looks like python will by itself 'magically' deal with the import; and 2. apart from the fact that yaml is easier to read than the old plain text config file, it also allows to define the keywords in a more readable way):
version:1formatters:rawMessageFormatter:format:'%(message)s'datefmt:''defaultFormatter:format:'%(asctime)s.%(msecs)d %(filename)s: %(funcName)s: %(message)s'datefmt:'%Y/%m/%d %H:%M:%S'handlers:nullHandler:class:logging.NullHandlermainHandler:class:logging.handlers.SysLogHandlerlevel:INFOformatter:defaultFormatteraddress:'/dev/log'facility:'myapp'socktype:ext://socket.SOCK_DGRAMloggers:root:level:INFOhandlers: [nullHandler]
mainLogger:level:DEBUGhandlers: [mainHandler]
qualname:mainLogger
Post a Comment for "How To Refer To A Standard Library In A Logging Configuration File?"