Python – Is it a good practice to use logging inside classes/functions in Python

python

So far, I've been using logging only in main() mostly. I do not log anything in my functions or classes but sometimes I feel like it would be helpful.

What I would do now:

def init_logger():

    # set up logging to file
    logging.basicConfig(filename=LOG,
                    format='%(asctime)s - %(name)s - %(levelname)s - %(message)s')

    logger = logging.getLogger('testlog')
    h = logging.handlers.SysLogHandler()
    h.setLevel(logging.ERROR)

    logger.addHandler(h)
    return logger

def x():
    return 5+5

def main():
    logger = init_logger()
    logger.info('starting function x')
    x()
    logger.info('x returned successfully')
 

But sometimes I want/need to do something like:

def x():
    logger.info('starting x')
    ...
    logger.info('running x')
    ...
    logger.info('finished x')
    return value

But this means that I would need to set up logging at the module level, right? E.g.

#!/bin/python
import logging
logging.basicConfig(filename='example.log', encoding='utf-8', level=logging.DEBUG)

And now I can use logging anywhere in my script.

Also, doing so raises other points of consideration such as making sure logging is set up properly if somebody imports my module etc.

So basically, my main question is:

Is it Pythonic to use logging inside classes/methods/functions or should it be used in outer functions such as main() – as I do now?

And my subquestion would be:

Should logging be initialized at the module level or inside a function as I've done in my example in init_logger()?

Best Answer

Since the module is the unit of Python software, a good convention to use when naming loggers is to use a module-level logger, in each module which uses logging, add

logger = logging.getLogger(__name__)

on top of each module right after import statements. This is a singleton so we don't pass it around functions. And of course you can use logger.info or debug/error/warn inside any function/class then.

Configuring logging has several possible ways, Programmers can configure logging in three ways:

Creating loggers, handlers, and formatters explicitly using Python code that calls the configuration methods listed above.

Creating a logging config file and reading it using the fileConfig() function.

Creating a dictionary of configuration information and passing it to the dictConfig() function.

Refer to Configuring Logging section in Python doc for more examples.

Related Topic