Last active
February 12, 2020 01:55
-
-
Save AlmightyOatmeal/56ebae2e5a0680a559eb4cb30ecf6c19 to your computer and use it in GitHub Desktop.
Python logging example from dictionary (or loaded JSON) featuring formatting that includes the local timezone, custom log format, logging to both console AND file, and automatically rotating log files.
This file contains hidden or 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.config | |
import time | |
TZ_NAME = time.tzname[0] if not time.localtime().tm_isdst else time.tzname[1] | |
# Alternatively, convert the localtime to UTC/GMT but if you use this then update | |
# the logging formatter because this example is using the local timezone. | |
# logging_handler.formatter.converter = time.gmtime | |
# This is kept separate as it can be used to apply a default log level to your | |
# loggers otherwise you can use a method to change this to debug and apply the | |
# logging config dictionary but I will create another gist to show an example | |
# of that. | |
LOGGER_DEFAULT = { | |
'level': 'INFO' | |
} | |
# List concatenation makes long strings easier to digest so I include each of | |
# my log format components here and concatenate later. | |
FORMATTER_PARTS = [ | |
'%(asctime)s', | |
'%(levelname)s', | |
'PID:%(process)d - PROC:%(processName)s - THREAD:%(thread)s:', | |
'%(name)s.%(funcName)s', | |
'%(message)s' | |
] | |
# Now for the nitty-gritty. | |
LOG_CONFIG = { | |
'version': 1, | |
'disable_existing_loggers': False, | |
'formatters': { | |
'default': { | |
'format': ' | '.join(FORMATTER_PARTS), | |
'datefmt': '%Y-%m-%d %H:%M:%S {}'.format(TZ_NAME) | |
} | |
}, | |
'handlers': { | |
'console': { | |
'class': 'logging.StreamHandler', | |
'formatter': 'default' | |
}, | |
'file': { | |
'backupCount': 10, | |
'class': 'logging.handlers.RotatingFileHandler', | |
'encoding': 'utf-8', | |
'formatter': 'default', | |
'filename': 'recentmetadata.log', | |
'maxBytes': 10485760 # 10MB | |
} | |
}, | |
'loggers': { | |
'main': LOGGER_DEFAULT, | |
'elasticsearch': LOGGER_DEFAULT | |
}, | |
'root': { | |
'handlers': ['console', 'file'] | |
} | |
} | |
# Finally apply the logging config dictionary. | |
logging.config.dictConfig(LOG_CONFIG) | |
# The only thing left to do is setup your loggers and enjoy! | |
logger = logging.getLogger('main') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment