4👍
✅
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!
3👍
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.
Source:stackexchange.com