Skip to content

Instantly share code, notes, and snippets.

@karma-git
Last active January 1, 2022 10:34
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 karma-git/e711bf8918fff93a173e3ef33c530b79 to your computer and use it in GitHub Desktop.
Save karma-git/e711bf8918fff93a173e3ef33c530b79 to your computer and use it in GitHub Desktop.
Honestly, I hate python logging
import logging
# ref: https://realpython.com/python-logging/
# custom logger
logger = logging.getLogger(__name__)
# handler
c_handler = logging.StreamHandler()
# set handler and logger level
# CRITICAL 50
# ERROR 40
# WARNING 30
# INFO 20
# DEBUG 10
c_handler.setLevel(logging.DEBUG)
logger.setLevel(logging.DEBUG)
# create formatter and assign to handler
c_format = logging.Formatter(
"%(asctime)s - %(levelname)s - %(message)s", datefmt="%d-%b-%y %H:%M:%S"
)
c_handler.setFormatter(c_format)
# add handler to logger
logger.addHandler(c_handler)
var = "Hello there"
logger.debug(f"This is a debug message - {var}")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment