Skip to content

Instantly share code, notes, and snippets.

@William-Hill
Created February 9, 2018 20:12
Show Gist options
  • Save William-Hill/a61bb65a575f890878900c1388e2cf14 to your computer and use it in GitHub Desktop.
Save William-Hill/a61bb65a575f890878900c1388e2cf14 to your computer and use it in GitHub Desktop.
Python logging setup
'''My preferred logging setup to log the filename, line number, function name, and time in messages'''
import logging
logger = logging.getLogger('metrics_logger')
logger.setLevel(logging.DEBUG)
# create formatter
formatter = logging.Formatter("%(levelname)s - %(filename)s - %(lineno)s - %(funcName)s - %(asctime)s - %(message)s", datefmt='%m/%d/%Y %I:%M:%S %p')
# create console handler with a higher log level
console_handler = logging.StreamHandler()
console_handler.setFormatter(formatter)
logger.addHandler(console_handler)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment