问题
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
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?
回答1:
Try just a custom log class? Something like:
import time
class Log:
msgs = []
def __init__(self):
pass
def addLog(self,msg):
self.msgs.append("LOG AT "+str(time.time())+":\n"+msg)
def dumpLog(self):
s = ""
for each in self.msgs:
s+= each+"\n------------------------\n"
return s
I'm not familiar with the logging module and it looks over complicated for most use cases.
回答2:
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 = []
来源:https://stackoverflow.com/questions/44034737/python-logging-dump-upon-failure