How do I change the default format of log messages in python app engine?
I found this to be working for Python 3.6, it will set the logging level / format for all subsequent logging calls, even if logging
is called by previous imports.
logging_level = logging.INFO
logging_fmt = "%(levelname)s:%(name)s:%(message)s" # the default
try:
root_logger = logging.getLogger()
root_logger.setLevel(logging_level)
root_handler = root_logger.handlers[0]
root_handler.setFormatter(logging.Formatter(logging_fmt))
except IndexError:
logging.basicConfig(level=logging_level, format=logging_fmt)
I cooked this up by reading the logging
module's __init__.py
. I don't know if this is proper, but it seems to work:
import logging
logging.basicConfig(level=logging.DEBUG,
format='%(asctime)s %(name)-12s %(levelname)-8s %(message)s',
datefmt='%m-%d %H:%M',
)
logging.info('Danger Will Robinson!')
# 03-31 20:00 root INFO Danger Will Robinson!
root = logging.getLogger()
hdlr = root.handlers[0]
fmt = logging.Formatter('%(name)-12s: %(levelname)-8s %(message)s')
hdlr.setFormatter(fmt)
logging.info('Danger Will Robinson!')
# root : INFO Danger Will Robinson!