Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Configuring child loggers

Tags:

python

logging

Every time I think I understand the logging module, gremlins come in and change the way it works. (Ok, I'll admit, that gremlin may be me changing my code.)

What am I doing wrong here?

> ipython
> import logging
> log = logging.Logger("base")
> log.addHandler(logging.StreamHandler())

> log.critical("Hi")
Hi

> log2 = log.getChild("ment")

> log2.critical("hi")
No handlers could be found for logger "base.ment"

I could have sworn that in the past, I was able to use child loggers without additional configuration...

like image 262
JS. Avatar asked Jul 27 '12 21:07

JS.


People also ask

How do I create a multiple logging level in Python?

You can set a different logging level for each logging handler but it seems you will have to set the logger's level to the "lowest". In the example below I set the logger to DEBUG, the stream handler to INFO and the TimedRotatingFileHandler to DEBUG. So the file has DEBUG entries and the stream outputs only INFO.

How do I enable logging in Python?

You can configure logging as shown above using the module and class functions or by creating a config file or a dictionary and loading it using fileConfig() or dictConfig() respectively. These are useful in case you want to change your logging configuration in a running application.

What is logging getLogger (__ Name __?

logger = logging. getLogger(__name__) This means that logger names track the package/module hierarchy, and it's intuitively obvious where events are logged just from the logger name. The root of the hierarchy of loggers is called the root logger.


2 Answers

If you change

log = logging.Logger('base')

to

log = logging.getLogger('base')

then it works:

import logging

log = logging.getLogger('base')
log.addHandler(logging.StreamHandler())
log.critical('Hi')
log2 = log.getChild('ment')
log2.critical('hi')

yields

Hi
hi
like image 173
unutbu Avatar answered Oct 05 '22 18:10

unutbu


More detail: You are using the module wrong. :) From looking at the module code, it looks like they don't expect you to ever create a logging.Logger() directly. Many of the functions available directly on the module (ex getLogger()) and the methods on logging.Logger() (ex getChild()) actually proxy through an instance of logging.Manager that the module creates on import. When you create a Logger with logging.Logger() directly, you are actually creating a Logger instance outside of the Manager. When you subsequently call log.getChild(), the module is actually creating the new logger inside the Manager, but with the name of the Manager-external logger appended to the front of the logger name. So your handler added to log is not in the Manager with the spawned child, and thus the handler doesn't work. I am a little confused still though on why adding a handler to log before or after creating log2 causes logging against log2 to behave differently. I don't see what's causing that...

like image 33
Silas Ray Avatar answered Oct 05 '22 18:10

Silas Ray