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
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 shows how to apply this to the requests library.
One last note: the loggers form a hierarchy: foo.bar
is a child logger to foo
. See the logging module's documentation, 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."