0

I'm using PTB library and have many handlers as:

def some_handler(update, context):  # Update is new data from user, context is all my data
    do_something()

And I want to notify user if error has occured, like:

def some_handler(update, context):
    try:
        do_something()
    except Exception as e:
    notify_user(text="Some error occured")
    logger.error(e)

To follow DRY and make code more nicely I wrote such decorator:

def bot_logger(text: str = ''):
    def decorator(function):
        @loguru_logger.catch
        @wraps(function)
        def wrapper(*args, **kwargs):
            try:
                return function(*args, **kwargs)
            except Exception as e:
                notify_user(text=f'Unknown error. {text} Please try againt latter')
                loguru_logger.error(e
        return wrapper
    return decorator

But in most cases I'm getting a pretty obscure log record like

2021-11-26 19:47:32.558 | ERROR | bot:wrapper:46 - There are no messages to send

Question: How I can make this message a more informative as a standard python error? What should I fix in bot_logger decorator?

Logger setup:

from loguru import logger as loguru_logger


loguru_logger.add(
    sink="log_error.txt",
    filter=lambda record: record["level"].name == "ERROR",
    backtrace=True,
    format="{time} {level} {function}:{line} {message}",
    level="ERROR",
    rotation="1 MB",
    compression="zip",
    enqueue=True,
    diagnose=True
)

P.S. I checked another similar questions

  1. Best practices for logging in python
  2. Python logging using a decorator

And the others but don't found an asnwer Also, I tried different logger formats and parameters but it's not change a log record a much

salius
  • 918
  • 1
  • 14
  • 30
  • some exceptions may have extra fields which you could use to get extra information - but these fileds may have different names and it would need to use many `except` for every exception which have extra field. You may also use module [trackback](https://docs.python.org/3/library/traceback.html) to get some extra information. ie, `print( traceback.format_exc() )` – furas Nov 27 '21 at 00:25

0 Answers0