Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

logging setLevel, how it works

Tags:

python

logging

In the logging howto documentation there is this example:

import logging  # create logger logger = logging.getLogger('simple_example') logger.setLevel(logging.DEBUG)  # create console handler and set level to debug ch = logging.StreamHandler() ch.setLevel(logging.DEBUG)  # create formatter formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')  # add formatter to ch ch.setFormatter(formatter)  # add ch to logger logger.addHandler(ch) 

Why I should set the level to logging.DEBUG twice, for Logger, and for the StreamHandler?

I understand ch.setLevel(logging.DEBUG) will set the debug level for the stream handler. But what the effect is of setting the level to logger? Where this level is reflected?

I get the same console output if I change the level to, for example, INFO either to the Logger or to the StreamHandler.

That is:

........... logger.setLevel(logging.INFO) ............ ch.setLevel(logging.DEBUG) 

gives the same output in console than

........... logger.setLevel(logging.DEBUG) ............ ch.setLevel(logging.INFO) 
like image 832
joaquin Avatar asked Jul 07 '11 16:07

joaquin


People also ask

What is logger setLevel?

setLevel() method of a Logger class used to set the log level to describe which message levels will be logged by this logger. The level we want to set is passed as a parameter.

How does Python logger work?

Standard Library Logging Module Python comes with a logging module in the standard library that provides a flexible framework for emitting log messages from Python programs. This module is widely used by libraries and is the first go-to point for most developers when it comes to logging.

What is the use of logging getLogger?

The getLogger() function accepts a single argument - the logger's name. It returns a reference to a logger instance with the specified name if provided, or root if not. Multiple calls to getLogger() with the same name will return a reference to the same logger object.

What does logger getLogger do Python?

Python logging getLogger The getLogger returns a logger with the specified name. If no name is specified, it returns the root logger. It is a common practice to put the module name there with __name__ . All calls to this function with a given name return the same logger instance.


2 Answers

It's there for fine-tuning (you can have multiple handlers, and each could have different levels set) — you can safely not set level on the handler, which will cause it to process all messages (a.k.a. NOTSET level), and leave level filtering to the logger.

Logger is also the first to filter the message based on a level — if you set the logger to INFO, and all handlers to DEBUG, you still won't receive DEBUG messages on handlers — they'll be rejected by the logger itself. If you set logger to DEBUG, but all handlers to INFO, you won't receive any DEBUG messages either — because while the logger says "ok, process this", the handlers reject it (DEBUG < INFO).

like image 167
Cat Plus Plus Avatar answered Sep 25 '22 06:09

Cat Plus Plus


Why I should set the level to logging.DEBUG twice, for logger, and for the streamhandler. I understand ch.setLevel(logging.DEBUG) will set the debug level for the stream handler. But what the effect is of setting the level to logger?. Where this level is reflected?.

This is indicated in the documentation:

"The setLevel() method, just as in logger objects, specifies the lowest severity that will be dispatched to the appropriate destination. Why are there two setLevel() methods? The level set in the logger determines which severity of messages it will pass to its handlers. The level set in each handler determines which messages that handler will send on."

Check under Handlers: http://docs.python.org/2.7/howto/logging.html#logging-advanced-tutorial

like image 39
Ankur Agarwal Avatar answered Sep 22 '22 06:09

Ankur Agarwal