TypeError: _log() got an unexpected keyword argument 'stacklevel'

Viewed 4935

Using stacklevel with log.error produces this error. Ex:

logger = logging.getLogger('tcpserver')
logger.error("Connection problem", stack_info=sys.exc_info(), stacklevel=1)

While according to documentation: There are four keyword arguments in kwargs which are inspected: exc_info, stack_info, stacklevel and extra.

See https://docs.python.org/3/library/logging.html#logging.Logger.debug

1 Answers

The documentation you're looking at and the version of your interpreter do not match. Also it's, well, documented: https://docs.python.org/3.8/library/logging.html#logging.Logger.debug

Changed in version 3.8: The stacklevel parameter was added.

Either upgrade your Interpreter, or use matching docs e.g.: https://docs.python.org/3.7/library/logging.html#logging.Logger.debug (and generally it's useful to not forget to check API changes notes at the bottom, also for compatibility with other possible users and their environment).

Related