Suppressing logger messages below a certain level in Python
Question: I worked on one system with a repo that imported lots of libraries. These used
the Python logger facility. I would only receive messages for events
logger.error and higher.

I moved to a different system and cloned the same repos (and libraries). Now,
I get logger.info() and higher. I've done a bit of searching and I've been
unable to track down where a central configuration for logger for a user is
kept. I'm not sure what changed from the first system to the second, but I
wish to have the functionality of the first back.

To clarify: the logger 'filter' applied to messages generated by not only the
software, but also the libraries it used.

Any ideas?


Answer: For Python's standard `logging` module, there's no system-wide central config
for logging, IIRC; you have to set it programmatically — and you can trigger
that from your own config file, the arguments in `sys.argv`, from Consul,
etc., if you want; sky's the limit here. Depending on exactly what program
you're using, and how it works, it may have a particular config file or method
for setting the logging level. If this is just your own codebase, then read
on.

To set the logging level, you need a logger; once you have a logger, you just
need to call
[`setLevel`](https://docs.python.org/2/library/logging.html#logging.Logger.setLevel)
on it. You can do this for the root logger, which will likely affect all
logging:

    
    
    root_logger = logging.getLogger()
    root_logger.setLevel(logging.WARNING)  # I'd recommend not removing WARNINGs.
    

Applying a log level to the root logger is a bit of a sledgehammer solution;
some well-behaved libraries (and your own code can do this, too) create their
own loggers, with something like,

    
    
    MY_LIBRARYS_LOGGER = logging.getLogger('my_library')
    
    def some_public_function(…):
        MY_LIBRARYS_LOGGER.info('Hi there logs.')
    

You can then silence _just those messages_ with,

    
    
    logging.getLogger('my_library').setLevel(logging.WARNING)
    

(or, if they export their logger as a constant, as my example does,
`MY_LIBRARYS_LOGGER.setLevel(…)`) I find it very useful to redirect entire
subsystems through a logger, so that you can raise/lower the levels as needed
when hunting bugs.

As an example of a library that does this, [this
answer](http://stackoverflow.com/a/26460395/101999) shows how to apply this to
the [requests library](http://docs.python-requests.org/en/latest/).

One last note: the loggers form a hierarchy: `foo.bar` is a child logger to
`foo`. See [the logging module's
documentation](https://docs.python.org/2/library/logging.html), of course.

One _last_ opinion: if this _is_ your code-base, and you're seeing too many
log lines, consider moving some of them to `debug`; I find `info` a good place
to log things that are indicative of the health of the system, such as "this
request to this other server took n seconds" (a real metrics system is of
course a better choice, if you have one, but sometimes what you have are
logs); this makes for an easy grep to see if that request is slow, and still
allows you to grep them _out_ mostly reliably if it is too noisy (by grepping
out info lines); `debug` is more "I did this thing that I always would do."

