失败时进行Python日志转储 [英] Python logging dump upon failure

查看:48
本文介绍了失败时进行Python日志转储的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Python编写一个从消息队列读取的服务.对于每条消息,它运行一个进程,完成后将从队列中获取另一条消息.我正在使用日志记录包来记录信息,警告和错误.如果该过程由于某种原因失败,则该过程将捕获该错误并发送带有追溯的电子邮件.我希望电子邮件中也包含应用程序开始处理消息时的日志.需要明确的是-我仍然希望应用程序立即记录.但是,如果应用程序失败并成功捕获到异常,我希望它也将日志发送到电子邮件中.

I'm using Python to write a service that reads from a message queue. For each message, it runs a process and upon completion will grab another message from the queue. I am using the logging package to log info, warnings, and errors. If the process fails for whatever reason, the process will catch the error and send out an email with the traceback. I would like for the email to also contain the logs from when the application began processing the message. To be clear - I would still like the application to log immediately. But, if the application fails and successfully catches the exception, I would like it to send the logs in the email as well.

我的第一个想法是使用一个列表,该列表将仅保存日志字符串,如下所示:

My first thought was to use a list that would just save log strings like so:

while True:
  # Grab the message here
  log_list = []
  try:
    log_msg = "Some log message here"
    log_list.append(log_msg)
    ...
  except:
    # send email here using log_list

这会解决我的问题,除了我想查看python日志记录软件包添加到日志消息(如时间戳记)的其他信息.我意识到我可以手动将其放入并将其添加到我的log_list对象中.我想做的是:

This would kind of solve my problem except I would like to see the other info that the python logging package adds to log messages like a timestamp. I realize I could go about just manually putting this in and adding it to my log_list object. What I would like to do is:

import logging
logger = logging.getLogger()
while True:
  logger.reset()
  # Grab the message here
  try:
    logger.info("Some log message here")
    ...
  except:
    # send email here using something like logger.dump()

所以...我的问题-还有另一种方法可以避免使用列表保存日志并保存其他日志记录信息(例如时间戳,日志记录级别等)的方法吗?

So... my question - Is there a another way to go about this in which I can avoid using a list to save my logs and save other logging info such as the timestamp, logging level, etc?

推荐答案

我最终使用了riscnotcisc的建议,但是我扩展了logging.Logger类

I ended up using riscnotcisc's suggestion but I extended the logging.Logger class

from logging import Logger
import logging
import datetime

class CustomLogger(Logger):
    def __init__(self, process_name):
        self.process_name = process_name
        super(CustomLogger, self).__init__(process_name)
        self.log_list = []

        ch = logging.StreamHandler()
        ch.setFormatter(logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s'))
        self.handlers.append(ch)

    def info(self, message, *args, **kwargs):
        self.log_list.append('{} - {} - INFO - {}'.format(datetime.datetime.now(), self.process_name, message))
        super(CustomLogger, self).info(message, *args, **kwargs)

    def debug(self, message, *args, **kwargs):
        self.log_list.append('{} - {} - DEBUG - {}'.format(datetime.datetime.now(), self.process_name, message))
        super(CustomLogger, self).debug(message, *args, **kwargs)

    def warning(self, message, *args, **kwargs):
        self.log_list.append('{} - {} - WARNING - {}'.format(datetime.datetime.now(), self.process_name, message))
        super(CustomLogger, self).warning(message, *args, **kwargs)

    def dump(self):
        return '\n'.join(self.log_list)

    def reset(self):
        self.log_list = []

这篇关于失败时进行Python日志转储的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆