简体   繁体   中英

How to set custom logging handler in dictConfig file using python logging module

I'm newbee in development and faced with next problem.
I wrote a custom logging handler and save it into separate module. I try to set this handler as handler's class in dictConfig, and it works correctly on localhost, but when I run it on Heroku it raises ValueError with message " Unable to configure handler 'send_to_telegram_handler': 'NoneType' object is not iterable "

Here is the code:

# logging_handlers.py

import os
import logging
import telegram


class SendToTelegramHandler(logging.Handler):
    def __init__(self):
        super().__init__()
        self.bot = telegram.Bot(token=os.getenv('TELEGRAM_BOT_TOKEN'))

    def emit(self, record):
        log_entry = self.format(record)
        self.bot.send_message(chat_id=os.getenv('TELEGRAM_CHAT_ID'), text=log_entry)

# settings.py

import logging_handlers

LOGGING = {
    'version': 1,
    'disable_existing_loggers': True,
    'formatters': {
        'standard': {
            'format': '%(asctime)s [%(levelname)s] %(name)s: %(message)s',
            'datefmt': '%Y-%m-%d %H:%M:%S',
        },
    },
    'handlers': {
        'send_to_telegram_handler': {
            'level': 'WARNING',
            'formatter': 'standard',
            'class': 'logging_handlers.SendToTelegramHandler',
        },
    },
    'loggers': {
        'telegram_bot': {
            'handlers': ['send_to_telegram_handler'],
            'level': 'WARNING'
        },
        'dialogflow': {
            'handlers': ['send_to_telegram_handler'],
            'level': 'WARNING'
        },
    }
}

I guess the problem is with using custom class for logging handler, but don't understand why it works on localhost but not on Heroku

There was a mistake in TELEGRAM_BOT_TOKEN keyname in config-vars on Heroku. So, the problem has solved.

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM