0

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?

  • It sounds like you need to read more code. I don't think anyone is going to be able to help without knowing the system etc. Why don't you redirect the reference from the logger to something else temporarily while you want this behavior? Otherwise I would do a `git blame` on the logger file and go speak with that person. – Matt Jul 30 '15 at 02:32

1 Answers1

0

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."

Community
  • 1
  • 1
Thanatos
  • 37,926
  • 14
  • 76
  • 136
  • @Thantos - Thanks for all the info. I have been setting the logger in the main file (root logger?) to log ERROR and above, but its imported libraries still log INFO. Looking at what you've written I've also tried setting `logging.getLogger('library').setLevel(logging.ERROR)`, but that didn't stop `library.otherthing` from logging INFO. It looks like I have to do it once for all my imports? – HH- Apologize to Carole Baskin Jul 30 '15 at 12:24