skip to Main Content

I have a logger function from logging package that after I call it, I can send the message through logging level.

I would like to send this message also to another function, which is a Telegram function called SendTelegramMsg().

How can I get the message after I call the funcion setup_logger send a message through logger.info("Start") for example, and then send this exatcly same message to SendTelegramMsg() function which is inside setup_logger function?

My currently setup_logger function:

# Define the logging level and the file name
def setup_logger(telegram_integration=False):
    """To setup as many loggers as you want"""

    filename = os.path.join(os.path.sep, pathlib.Path(__file__).parent.resolve(), 'logs', str(dt.date.today()) + '.log')
    formatter = logging.Formatter('%(levelname)s: %(asctime)s: %(message)s', datefmt='%m/%d/%Y %H:%M:%S')
    level = logging.DEBUG

    handler = logging.FileHandler(filename, 'a')    
    handler.setFormatter(formatter)

    consolehandler = logging.StreamHandler()
    consolehandler.setFormatter(formatter)

    logger = logging.getLogger('logs')
    if logger.hasHandlers():
        # Logger is already configured, remove all handlers
        logger.handlers = []
    else:
        logger.setLevel(level)
        logger.addHandler(handler)        
        logger.addHandler(consolehandler)

    #if telegram_integration == True:
        #SendTelegramMsg(message goes here)

    return logger

After I call the function setup_logger():

logger = setup_logger()
logger.info("Start")

The output:

INFO: 01/06/2022 11:07:12: Start

How am I able to get this message and send to SendTelegramMsg() if I enable the integration to True?

3

Answers


  1. Picking up the idea suggested by @gold_cy: You implement a custom logging.Handler. Some hints for that:

    • for the handler to be able to send message via a bot, you may want to pass the bot to the handlers __init__ so that you have it available later
    • emit must be implemented by you. Here you’ll want to call format which gives you a formatted version of the log record. You can then use that message to send it via the bot
    • Maybe having a look at the implementation of StreamHandler and FileHandler is helpful as well
    Login or Signup to reply.
  2. #Defining a global flag
    tlInt=False
    # Define the logging level and the file name
    def setup_logger(telegram_integration=False):
        """To setup as many loggers as you want"""
    
        filename = os.path.join(os.path.sep, pathlib.Path(__file__).parent.resolve(), 'logs', str(dt.date.today()) + '.log')
        formatter = logging.Formatter('%(levelname)s: %(asctime)s: %(message)s', datefmt='%m/%d/%Y %H:%M:%S')
        level = logging.DEBUG
    
        handler = logging.FileHandler(filename, 'a')    
        handler.setFormatter(formatter)
    
        consolehandler = logging.StreamHandler()
        consolehandler.setFormatter(formatter)
    
        logger = logging.getLogger('logs')
        if logger.hasHandlers():
            # Logger is already configured, remove all handlers
            logger.handlers = []
        else:
            logger.setLevel(level)
            logger.addHandler(handler)        
            logger.addHandler(consolehandler)
    
        if telegram_integration == True:
            global tlInt
            tlInt=True
    
        return logger
    
    #Logic : If telegram integration is true, it will call SendTelegramMsg to send the message to the app based on the level; and if it is false, it will save the message in local file based on the level
    def GenerateLog(logger,levelFlag,data):
        global tlInt
        if tlInt == True:
           SendTelegramMsg(levelFlag,data)
        else:
           if levelFlag == "warning":
               logger.warning(data)
           elif levelFlag == "error":
               logger.error(data)
           elif levelFlag == "debug":
               logger.debug(data)
           elif levelFlag == "critical":
               logger.critical(data)
           else:
               logger.info(data)
    
    
    #You can used the same logic in SendTelegramMsg which used in GenerateLog for deciding the level 
    def SendTelegramMsg(levelFlag,data):
        #your code goes here
    
    logger=setup_logger()
    GenerateLog(logger,'warning','Start')
    
    
    
    Login or Signup to reply.
  3. Implement a custom logging.Handler:

    class TelegramHandler(logging.Handler):
    
        def emit(self, record):
            message = self.format(record)
            SendTelegramMsg(message)
            # SendTelegramMsg(message, record.levelno)    # Passing level
            # SendTelegramMsg(message, record.levelname)  # Passing level name
    

    Add the handler:

    def setup_logger(telegram_integration=False):
        # ...
    
        if telegram_integration:
            telegram_handler = TelegramHandler()
            logger.addHandler(telegram_handler)
    
        return logger
    

    Usage, no change:

    logger = setup_logger()
    logger.info("Start")
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search