我是python的新手,我遇到了一个我无法解决的严重问题.
我有一些结构相同的日志文件:
[timestamp] [level] [source] message
Run Code Online (Sandbox Code Playgroud)
例如:
[Wed Oct 11 14:32:52 2000] [error] [client 127.0.0.1] error message
Run Code Online (Sandbox Code Playgroud)
我需要用纯Python编写一个程序,它应该将这些日志文件合并到一个文件中,然后按时间戳对合并的文件进行排序.在此操作之后,我希望将此结果(合并文件的内容)打印到STDOUT(控制台).
我不明白该怎么做才有所帮助.这可能吗?
我遇到了从以下脚本收集日志的问题.一旦我设置SLEEP_TIME为太"小"的值,LoggingThread线程就会以某种方式阻止日志记录模块.该脚本冻结了action函数中的日志记录请求.如果SLEEP_TIME大约为0.1,则脚本会按预期收集所有日志消息.
我试着按照这个答案,但它没有解决我的问题.
import multiprocessing
import threading
import logging
import time
SLEEP_TIME = 0.000001
logger = logging.getLogger()
ch = logging.StreamHandler()
ch.setFormatter(logging.Formatter('%(asctime)s %(levelname)s %(funcName)s(): %(message)s'))
ch.setLevel(logging.DEBUG)
logger.setLevel(logging.DEBUG)
logger.addHandler(ch)
class LoggingThread(threading.Thread):
def __init__(self):
threading.Thread.__init__(self)
def run(self):
while True:
logger.debug('LoggingThread: {}'.format(self))
time.sleep(SLEEP_TIME)
def action(i):
logger.debug('action: {}'.format(i))
def do_parallel_job():
processes = multiprocessing.cpu_count()
pool = multiprocessing.Pool(processes=processes)
for i in range(20):
pool.apply_async(action, args=(i,))
pool.close()
pool.join()
if __name__ == '__main__':
logger.debug('START')
#
# multithread part
#
for _ in range(10): …Run Code Online (Sandbox Code Playgroud)