Skip to content

Instantly share code, notes, and snippets.

@elnygren
Created September 26, 2016 10:09
Show Gist options
  • Save elnygren/d58d6f5af23e6126eb52bd2156438a21 to your computer and use it in GitHub Desktop.
Save elnygren/d58d6f5af23e6126eb52bd2156438a21 to your computer and use it in GitHub Desktop.
Sane Python logging
# Setting up python logging is a pain in the but
# ...until you found this gist.
import logging
from logging.config import dictConfig as LOGGING_CONFIG
LOG_LEVEL = logging.INFO
LOGGING_CONFIG({
'version': 1,
'formatters': {
'f': {'format': '%(asctime)s - %(name)s - %(levelname)s - %(message)s'}
},
'handlers': {
'h': {'class': 'logging.StreamHandler',
'formatter': 'f',
'level': LOG_LEVEL}
},
'root': {
'handlers': ['h'],
'level': LOG_LEVEL,
},
})
# anywhere in your code
# import logger
logger = logging.getLogger()
logger.info('we are logging to stdout with a nice format like we should be')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment