Set up logging better to play nicely with other libraries
Submitted by Barry Warsaw
Assigned to Simon McVittie
Description
I've noticed some problems with the way dbus-python does logging, so I want to open this issue for discussion. Once we agree on something, I'll try to propose a patch/branch/merge.
The basic problem I think is that dbus-python expects to log through the root logger, and formats its messages making certain assumptions about how the root logger is configured. Here's an example from proxies.py:
def _introspect_error_handler(self, error):
logging.basicConfig()
_logger.error("Introspect error on %s:%s: %s.%s: %s",
self._named_service, self.__dbus_object_path__,
error.__class__.__module__, error.__class__.__name__,
error)
The problem with this is that other libraries or applications may already have initialized the root logger, and in fact may have set style='{' which will essentially break all dbus logging. I noticed this in my system-image application.
One minor comment here is that you don't need to call logging.basicConfig(). The logging infrastructure will call that automatically if needed, and if it's not needed, it's a no-op. So you can probably remove all the basicConfig() calls.
The other thing is that you probably want to set up your own loggers to have the format and style that the library wants, independent of whatever other settings an application using dbus-python has made. Or in other words: don't rely on the root logger. I wish Python's logging infrastructure was more amenable to composition of distinct libraries, and use by applications. While inconvenient and requiring a bunch of extra code, it is possibly to set up library-specific loggers that won't be affected by any other logging system settings elsewhere.
Even better, it would be cool if a client of dbus-python could direct log messages anywhere they wanted, e.g. syslog, or a specific debug file. I can provide some examples of the kinds of things I've done on request. I'm not saying I have great solutions, but I think they're better given what we have to work with.