3

I am looking for a way to have logging.info, logging.warning, logging.error, etc. set exc_info=True by default. Refactoring the project to include this parameter isn't feasible at this point, so I'm looking for a way to set this for the logging module at a project-wide scope.

I am specifically looking for a way to set this parameter. Workarounds to retrieve the exception like sys.exc_info or traceback.format_exc() are not what I'm looking for.

Additionally, please let me know if this just isn't possible.

SkippyNBS
  • 687
  • 1
  • 5
  • 21
  • It's possible, but doesn't sound useful at all. The exc info will be `NoneType` and `None` unless you're logging from within an except block. Why on earth would you want that true / on by default? – wim Oct 13 '20 at 22:12
  • Because of project requirements outside of my control... Is this possible from a config or do I have to overload the functions themselves? I've been reading through the logging documentation and haven't found a relevant config. – SkippyNBS Oct 13 '20 at 22:17
  • There is a requirement outside of your control for log files to be littered with useless `NoneType: None` everywhere? – wim Oct 13 '20 at 22:19
  • Yes; is there something in ```logging.Config``` that can achieve this behavior or should I be looking elsewhere? – SkippyNBS Oct 13 '20 at 22:22
  • @wim seems like you want to be helpful, and understand, thus you ask for an example use case => Personnaly, I use sentry to log error, but when using python's logging to explicitely throw an error, the stacktrace is not available by default in sentry; I need to set exc_info=True. Makes sense ? – nicolas.leblanc Dec 22 '22 at 15:47

1 Answers1

2

Posting this answer in case anyone has a similar problem - taken from this question.

The easiest way I've found is to use functools.partial at the start of your project. This allows you to change the default parameter value, while still being able to set it to False for specific function calls if you want to!

import logging
from functools import *

logging.info = partial(logging.info, exc_info=True)
SkippyNBS
  • 687
  • 1
  • 5
  • 21