Skip to content

Instantly share code, notes, and snippets.

@osulli
Last active August 22, 2021 10:16
Show Gist options
  • Select an option

  • Save osulli/0ed2c5512c9c3193bf8ed4d39f2065f6 to your computer and use it in GitHub Desktop.

Select an option

Save osulli/0ed2c5512c9c3193bf8ed4d39f2065f6 to your computer and use it in GitHub Desktop.
Logging

Q: "How do you handle logs? I am looking for tools to better format my logs and maybe process and filter them"

Gotchas

  • The call to basicConfig() should come before any calls to debug(), info() etc. As it’s intended as a one-off simple configuration facility, only the first call will actually do anything: subsequent calls are effectively no-ops.

Interesting Notes

On error handling:

The logging package is designed to swallow exceptions which occur while logging in production. 
This is so that errors which occur while handling logging events - such as logging misconfiguration, network or
other similar errors - do not cause the application using logging to terminate prematurely

Source: https://docs.python.org/3/howto/logging.html#exceptions-raised-during-logging

If exc_info is not set to True, the output of the above program would not tell us anything about the exception,
which, in a real-world scenario, might not be as simple as a ZeroDivisionError.

Source: https://realpython.com/python-logging/#capturing-stack-traces

Sauces

import logging
logging.basicConfig(level=logging.DEBUG)
logging.info("This will use the default format")
"""
INFO:root:This will use the default format
"""
import logging
logging.basicConfig(format='%(levelname)s | %(asctime)s | %(name)s | %(message)s', level=logging.DEBUG)
logging.info("This will use a simple and reasonable format")
"""
INFO | 2021-08-22 09:59:58,148 | root | This will use a simple and reasonable format
"""
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment