Search code examples
pythonloggingkeyerror

python logger KeyError 'messages'


Below code is my logger settings

# generate log
logger = logging.getLogger()

# log level
logger.setLevel(logging.INFO)

#log formatter
formatter = logging.Formatter('%(asctime)s - %(levelname)s - %(messages)s')

# log to console
stream_handler = logging.StreamHandler()
stream_handler.setFormatter(formatter)
logger.addHandler(stream_handler)

#log to file
file_handler = logging.FileHandler('my.log')
file_handler.setFormatter(formatter)
logger.addHandler(file_handler)

and when I try to print log, I got below error

logger.info('hi')

KeyError: 'messages' During handling of the above exception, another exception occurred: ValueError: Formatting field not found in record: 'messages' Message: 'hi' Arguments: ()

Does this error occurs because I am trying to output logs in jupyter notebook? If not, please help me to print normal logs..


Solution

  • Thx to @wkl

    Reference to @wkl 's comment

    You have a typo in your format - logging.Formatter('%(asctime)s - %(levelname)s - %(messages)s') should be logging.Formatter('%(asctime)s - %(levelname)s - %(message)s'). Notice it's called message and not messages