7

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?

Guilherme Matheus
  • 573
  • 10
  • 30

3 Answers3

9

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")
aaron
  • 39,695
  • 6
  • 46
  • 102
3

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
CallMeStag
  • 5,467
  • 1
  • 7
  • 22
1
#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')


BhavinT
  • 338
  • 1
  • 10