In my program I define a logger at the beginning that looks similar to this:
def start_logger():
fh = logging.handlers.RotatingFileHandler('logger.log',
maxBytes=1000000,
backupCount=100)
fh.setLevel(logging.DEBUG)
ch = logging.StreamHandler(sys.stdout)
ch.setLevel(logging.DEBUG)
fh_fmt = '%(asctime)s %(levelname)8s %(message)s [%(filename)s:%(lineno)d]'
#fh_fmt = '%(asctime)s - %(funcName)s - %(levelname)s - %(message)s'
ch_fmt = '%(asctime)s %(levelname)8s %(message)s [%(filename)s:%(lineno)d]'
#ch_fmt = '%(funcName)s - %(levelname)s - %(message)s'
fh.setFormatter(logging.Formatter(fh_fmt))
ch.setFormatter(logging.Formatter(ch_fmt))
root = logging.getLogger()
root.addHandler(fh)
root.addHandler(ch)
I then have multiple files that are called from my main program. In order for them to work correctly I need to do the following:
import logging
log = logging.getLogger(__name__)
log.setLevel(logging.DEBUG)
log.debug("This debug message is ounly output when I set the level again to debug for this file. Otherwise the the log level for this file is WARNING.")
Why is the default level for all modules that I import set to warning. Why do I have to set the level again to DEBUG
for each of them when they import my root logger with log = logging.getLogger(__name__)
? Is this the best way to create a logging module accross a package with different modules or is there a better solution?
root.setLevel(logging.DEBUG)
and removed all the setLevel from the files, they do not produce any output. What could be the reason for that? Which one is defined as the 'root logger'? Simply the first instantiation of the logger? – Fractious