Python3 has the argument stack_info
for logging.info()
:
https://docs.python.org/dev/library/logging.html#logging.Logger.debug
How to get this in Python2?
The following module wrap the logging module with support to stack_info keyword argument. you can import this module and call the getLogger method similar to how you work with logging module.
import logging
import logging.handlers
from functools import partial
logger = logging.getLogger()
old_critical = logger.critical
old_error = logger.error
old_warning = logger.warning
old_info = logger.info
old_debug = logger.debug
import traceback
def custom_log(old_log, message, *args, **kwargs):
if kwargs.get("stack_info"):
message = message + reduce(lambda x,y: x+y, traceback.format_stack(), "")
if kwargs.get("stack_info") is not None:
del kwargs["stack_info"]
old_log(message, *args, **kwargs)
logger.critical = partial(custom_log, old_critical)
logger.error = partial(custom_log, old_error)
logger.warning = partial(custom_log, old_warning)
logger.info = partial(custom_log, old_info)
logger.debug = partial(custom_log, old_debug)
def new_getLogger(logger_name="root"):
return logger
from logging import *
getLogger = new_getLogger
PS - One downside to this approach is it will make one extra function call and that also will be visible in stack trace
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.