I followed the advice of the django docs, and use logging like this:
import logging logger = logging.getLogger(__name__) def today(...): logger.info('Sun is shining, the weather is sweet')
With my current configuration, the output looks like this:
2016-08-11 14:54:06 mylib.foo.today: INFO Sun is shining, the weather is sweet
Unfortunately some libraries which I can't modify use logging like this:
import logging def third_party(...): logging.info('Make you want to move your dancing feet')
The output unfortunately looks like this:
2016-08-09 08:28:04 root.third_party: INFO Make you want to move your dancing feet
I want to see this:
2016-08-09 08:28:04 other_lib.some_file.third_party: INFO Make you want to move your dancing feet
Difference:
root.third_party ==> other_lib.some_file.third_party
I want to see the long version (not root
) if code uses logging.info()
instead of logger.info()
Update
This is not a duplicate of Elegant setup of Python logging in Django, since the solution of it is:
Start of quote
In each module, I define a logger using
logger = logging.getLogger(__name__)
End of quote.
No, I won't modify third-party-code which uses logging.info()
instead of logger.info()
.
Follow Up Question
Avoid logger=logging.getLogger(__name__)
without loosing way to filter logs
2 Answers
Answers 1
As Wayne Werner suggested, I would use the Log Record format options. Here's an example.
File 1: external_module
import logging def third_party(): logging.basicConfig(level=logging.DEBUG) logger = logging.getLogger() logger.info("Hello from %s!"%__name__)
File 2: main
import external_module import logging logging.basicConfig(level=logging.DEBUG, format='%(asctime)s %(module)s.%(funcName)s: %(levelname)s %(message)s') logger = logging.getLogger(__name__) def cmd(): logger.info("Hello from %s!"%__name__) external_module.third_party() cmd()
Output:
2016-08-11 09:18:17,993 main.cmd: INFO Hello from __main__! 2016-08-11 09:18:17,993 external_module.third_party(): INFO Hello from external_module!
Answers 2
That's because they're using the root logger (which is what you get by default when you just do
import logging logging.info("Hi! I'm the root logger!")
If you want to do something different you have two (or three) options. The best would be to use the Log Record format options. Alternatively, you could monkey patch the libraries that you're using, e.g.
import logging import mod_with_lazy_logging mod_with_lazy_logging.logger = logging.getLogger(mod_with_lazy_logging.__name__)
Or you could do something gnarly with parsing the ast and rewriting their bits of logging code. But, don't do that.
0 comments:
Post a Comment