Python logging how to get message arguments

Question:

I want to catch the errormessage arguments and save them to the logfile. How do I do that?

enter image description here

It works fine for the message, but the Arguments are a mystery.

enter image description here

enter image description here

import logging
import sys

logger = logging.getLogger(__name__)
logger.setLevel(logging.DEBUG)
#formatter = logging.Formatter("%(asctime)s %(name)-10s %(levelname)-8s %(message)s")
formatter = logging.Formatter("%(asctime)s %(levelname)-8s %(message)s")

stdout_handler = logging.StreamHandler(sys.stdout)
stdout_handler.setLevel(logging.DEBUG) #DEBUG, INFO, WARNING, ERROR, CRITICAL wordt geprint in console
stdout_handler.setFormatter(formatter)

file_handler = logging.FileHandler("MontFin.log", mode='a')

file_handler.setLevel((logging.INFO)) #INFO, WARNING, ERROR + CRITICAL wordt geprint in logfile
file_handler.setFormatter(formatter)

logger.addHandler(file_handler)
logger.addHandler(stdout_handler)

# DEBUG
# INFO
# WARNING
# ERROR
# CRITICAL
Asked By: Tralala

||

Answers:

The arguments tuple could be added like this:

helper.logger.error("Oops ... %s, args=%s", e, e.args)
Answered By: wim
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.