Created
April 8, 2021 09:10
-
-
Save cesarferradas/17b43b3d0ebb3bf2a58cbbec255ab989 to your computer and use it in GitHub Desktop.
Python framework agnostic logging setup
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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