Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Logging hierarchy vs. root logger?

Tags:

python

logging

Somewhere in the bowels of my code I have something like:

logger = logging.getLogger('debug0.x') 

The way I understand it, this should only respond when I have previously done something like:

logging.basicConfig(filename='10Nov2010a.txt',level=logging.DEBUG, name='debug0') 

note that name has been defined as debug0. However, I have discovered that if do

logging.basicConfig(filename='10Nov2010a.txt',level=logging.DEBUG) 

without the name keyword, then the debug0.x logger defined above reacts, and writes to the log file. I was thinking it would only react in the first case, when the logger had been named.

I'm confused.

like image 661
reckoner Avatar asked Nov 10 '10 23:11

reckoner


People also ask

What is the root logger?

The rootlogger is always the logger configured in the log4j. properties file, so every child logger used in the application inherits the configuration of the rootlogger . The logging levels are (from smaller to greater) : ALL, DEBUG, INFO, WARN, ERROR, FATAL, OFF .

What is root logger in Python?

On top of the hierarchy is the root logger, which can be accessed via logging. root. This logger is called when methods like logging. debug() is used. By default, the root log level is WARN, so every log with lower level (for example via logging.info("info") ) will be ignored.

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. Sounds like good advice.

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.


2 Answers

The Python logging module organizes loggers in a hierarchy. All loggers are descendants of the root logger. Each logger passes log messages on to its parent.

New loggers are created with the getLogger() function. The function call logging.getLogger('debug0.x') creates a logger x which is a child of debug0 which itself is a child of the root logger. When logging to this logger, it will pass on the message to its parent, and its parent will pass the message to the root logger. You configured the root logger to log to a file by the basicConfig() function, so your message will end up there.

like image 120
Sven Marnach Avatar answered Oct 23 '22 05:10

Sven Marnach


If you check out the code or the doc:

>>> print logging.basicConfig.__doc__      Do basic configuration for the logging system.      This function does nothing if the root logger already has handlers     configured. ...............     A number of optional keyword arguments may be specified, which can alter     the default behaviour.      filename  Specifies that a FileHandler be created, using the specified               filename, rather than a StreamHandler.     filemode  Specifies the mode to open the file, if filename is specified               (if filemode is unspecified, it defaults to 'a').     format    Use the specified format string for the handler.     datefmt   Use the specified date/time format.     level     Set the root logger level to the specified level.     stream    Use the specified stream to initialize the StreamHandler. Note               that this argument is incompatible with 'filename' - if both               are present, 'stream' is ignored. 

logging.basicConfig does not use name argument at all. It initializes the root logger. While getLogger takes a "name" argument

>>> print logging.getLogger.__doc__      Return a logger with the specified name, creating it if necessary.      If no name is specified, return the root logger. 
like image 29
pyfunc Avatar answered Oct 23 '22 06:10

pyfunc