Created
October 24, 2017 14:45
-
-
Save TopherGopher/03775b646aa0528f09cbb6cb8fe90242 to your computer and use it in GitHub Desktop.
Logging in Python
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
class CustomJsonFormatter(jsonlogger.JsonFormatter): | |
""" | |
Class that helps redefine the log message format | |
""" | |
def add_fields(self, log_record, record, message_dict): | |
super(CustomJsonFormatter, self).add_fields(log_record, record, message_dict) | |
if not log_record.get('timestamp'): | |
# this doesn't use record.created, so it is slightly off | |
now = datetime.utcnow().strftime('%Y-%m-%dT%H:%M:%S.%fZ') | |
log_record['timestamp'] = now | |
if log_record.get('level'): | |
log_record['level'] = log_record['level'].lower() | |
else: | |
log_record['level'] = record.levelname | |
if log_record.get('message') is None: | |
log_record.pop('message') | |
# Initialize a logger for readable/debuggable STDOUT | |
logger = logging.getLogger(__name__) | |
FORMAT = "[%(filename)s:%(lineno)s - %(funcName)20s() ] %(message)s" | |
logging.basicConfig(format=FORMAT) | |
# Add a rotating file handler | |
fh = RotatingFileHandler('/var/log/flask.log', | |
maxBytes=10000, | |
backupCount=1) | |
# And have him log in JSON (for consumption by splunk) | |
fh.setFormatter(CustomJsonFormatter()) | |
logger.addHandler(fh) | |
logger.setLevel(logging.DEBUG) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This is just my preferred logging setup. You will need the
jsonlogger
pypi package installed to use it, but if you aren't using the file handler portion, don't worry about it and just leave the class definition out.