Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Customize logging for external/third-party libs

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

like image 637
guettli Avatar asked Aug 11 '16 13:08

guettli


People also ask

Is logging part of Python standard library?

Python provides a logging system as a part of its standard library, so you can quickly add logging to your application.

How do you create a logging file in Python?

To do this, add the following line after the basicConfig() call: logging. debug("Debug logging test...") Then, run the program and see that a new file called log.

What is logging library in Python?

Python comes with a logging module in the standard library that can provide a flexible framework for emitting log messages from Python programs. This module is widely used by libraries and is often the first go-to point for most developers when it comes to logging.


2 Answers

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!
like image 190
Jeremy Avatar answered Oct 24 '22 16:10

Jeremy


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.

like image 45
Wayne Werner Avatar answered Oct 24 '22 17:10

Wayne Werner