MFB MFB - 1 year ago 116
Python Question

Python global logging

How do I make a Logger global so that I can use it in every module I make?

Something like this in moduleA:

import logging
import moduleB

log = logging.getLogger('')

result = moduleB.goFigure(5)'Answer was', result)

With this in moduleB:

def goFigure(integer):
if not isinstance(integer, int):
log.critical('not an integer')
return integer + 1

Currently, I will get an error because moduleB does not know what
is. How do I get around that?

Answer Source

You could make your own logging "module" which instantiates the logger, than have all of your code import that instead. Think:

import logging
log = logging.getLogger('')

from logger import log'whatever')

from logger import log
log.warn('some other thing')