Python 将日志滚动到变量



我有一个利用多线程并在服务器上后台运行的应用程序。为了在不登录服务器的情况下监视应用程序,我决定包含 Bottle 以响应一些 HTTP 端点和报告状态,执行远程关闭等。

我还想添加一种查询日志文件的方法。我可以使用FileHandler登录并在请求 URL 时发送目标文件(例如/log)。

但是,我想知道是否有可能实现类似RotatingFileHandler的东西,但不是记录到文件,而是记录到变量(例如BytesIO)。这样,我可以将日志限制为最新信息,同时能够将其作为文本而不是单独的文件下载返回到浏览器。

RotatingFileHandler需要文件名,因此无法将其传递给BytesIO流。记录到变量本身是完全可行的(例如,在变量中捕获 Python 日志输出),但我对如何进行滚动部分有点困惑。

任何想法,提示,建议将不胜感激。

使用在变量中捕获 Python 日志输出中所述的技术,但将其捕获到丢弃旧数据的自定义流中。

这样:

# Adapted from http://alanwsmith.com/capturing-python-log-output-in-a-variable
import logging
import io
import collections
class FIFOIO(io.TextIOBase):
def __init__(self, size, *args):
self.maxsize = size
io.TextIOBase.__init__(self, *args)
self.deque = collections.deque()
def getvalue(self):
return ''.join(self.deque)
def write(self, x):
self.deque.append(x)
self.shrink()
def shrink(self):
if self.maxsize is None:
return
size = sum(len(x) for x in self.deque)
while size > self.maxsize:
x = self.deque.popleft()
size -= len(x)
### Create the logger
logger = logging.getLogger('basic_logger')
logger.setLevel(logging.DEBUG)
### Setup the console handler with a FIFOIO object
log_capture_string = FIFOIO(256)
ch = logging.StreamHandler(log_capture_string)
ch.setLevel(logging.DEBUG)
### Optionally add a formatter
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
ch.setFormatter(formatter)
### Add the console handler to the logger
logger.addHandler(ch)

### Send log messages. 
logger.debug('debug message')
logger.info('info message')
logger.warn('warn message')
logger.error('error message')
logger.critical('critical message')

### Pull the contents back into a string and close the stream
log_contents = log_capture_string.getvalue()
log_capture_string.close()
### Output as lower case to prove it worked. 
print(log_contents.lower())

根据 Andrew Guy 的建议,我进一步类化了logging.Handler并实现了一个处理程序,该处理程序使用固定长度的collections.deque来记录日志消息。

import logging
import collections

class TailLogHandler(logging.Handler):
def __init__(self, log_queue):
logging.Handler.__init__(self)
self.log_queue = log_queue
def emit(self, record):
self.log_queue.append(self.format(record))

class TailLogger(object):
def __init__(self, maxlen):
self._log_queue = collections.deque(maxlen=maxlen)
self._log_handler = TailLogHandler(self._log_queue)
def contents(self):
return 'n'.join(self._log_queue)
@property
def log_handler(self):
return self._log_handler

用法示例:

import random
logger = logging.getLogger(__name__)
tail = TailLogger(10)
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
log_handler = tail.log_handler
log_handler.setFormatter(formatter)
logger.addHandler(log_handler)
levels = [logging.INFO, logging.ERROR, logging.WARN, logging.DEBUG, logging.CRITICAL]
logger.setLevel(logging.ERROR)
for i in range(500):
logger.log(random.choice(levels), 'Message {}'.format(i))
print(tail.contents())

输出:

2016-06-22 13:58:25,975 - __main__ - CRITICAL - Message 471
2016-06-22 13:58:25,975 - __main__ - ERROR - Message 472
2016-06-22 13:58:25,975 - __main__ - ERROR - Message 473
2016-06-22 13:58:25,975 - __main__ - ERROR - Message 474
2016-06-22 13:58:25,975 - __main__ - ERROR - Message 477
2016-06-22 13:58:25,975 - __main__ - CRITICAL - Message 481
2016-06-22 13:58:25,975 - __main__ - CRITICAL - Message 483
2016-06-22 13:58:25,975 - __main__ - ERROR - Message 484
2016-06-22 13:58:25,975 - __main__ - CRITICAL - Message 485
2016-06-22 13:58:25,976 - __main__ - CRITICAL - Message 490

最新更新