Skip to content

Instantly share code, notes, and snippets.

@cesarferradas
Created April 8, 2021 09:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cesarferradas/17b43b3d0ebb3bf2a58cbbec255ab989 to your computer and use it in GitHub Desktop.
Save cesarferradas/17b43b3d0ebb3bf2a58cbbec255ab989 to your computer and use it in GitHub Desktop.
Python framework agnostic logging setup
import logging
import os
import sys
import time
from app import settings
def setup_logging():
"""
Configure the root logger which is the default when you import the logging lib.
Call this function once then you can:
import logging
...
logging.info("foo")
and it will respect the config defined here
"""
logging.basicConfig(
level=os.getenv("LOG_LEVEL", "INFO"),
format="[%(asctime)s][%(levelname)s][%(name)s][%(filename)s:%(lineno)s] %(message)s",
handlers=[logging.StreamHandler(sys.stdout)],
)
logging.info(f"Logger set up with handlers {logging.getLogger().handlers}")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment