用于多处理日志记录的 QueueHandler

如何解决用于多处理日志记录的 QueueHandler

我正在尝试调整我的程序以将不同进程的日志记录到单个日志文件中。 我一直在寻找解决方案很多天都没有成功。我想我仍然不明白队列处理程序是如何工作的。在我看来,这个过程是这样的:

  • 创建 q
  • 将 qHandler 添加到主记录器
  • 所有日志都将被重定向到 q,然后 q 将使用附加到记录器的其他处理程序(通过 logger.handle(record))。 我创建了一个程序的简化版本来说明记录器的行为
# logger.py

import logging
   
def listener_configurer():
    """This sets the settings for the root logger. The highest in the hierarchy. 
    All the handlers added to this root logger are available for all the subloggers.
    """
    root = logging.getLogger('main')
    file = logging.FileHandler(r'logs\temp.log','w')
    fmt = logging.Formatter('%(asctime)s %(processName)-10s %(name)s %(levelname)-8s %(message)s')
    stream = logging.StreamHandler()
    stream.setFormatter(fmt)
    file.setFormatter(fmt)
    root.addHandler(file)
    root.addHandler(stream)
    root.setLevel(logging.DEBUG)


def listener_process(queue):
    listener_configurer()
    while True:
        try:
            record = queue.get()
            if record is not None:
                print("-------------- using q ------------------ " + record.name + " -> " + record.message)
                logger = logging.getLogger(record.name)
                logger.handle(record)
            else:
                break
        except Exception:
            import sys,traceback
            logger.error('Whoops! Problem: %s',"problem",exc_info=1)
            traceback.print_exc(file=sys.stderr)
# saver.py (worker)
import logging
import typing

log = logging.getLogger('main.Saver')

class Saver:
    def __init__(self) -> None:
        log.warning("Instantiating a saver obj")
       
    def doStuff(self,input_line: typing.Tuple,) -> None:
        log.info(f"Exporting: {input_line}") # ASSUMING A TUPLE AS INPUT like: email,email_id,email_url
        (email,email_url,*other) = input_line
        log.info("Source URL: " + email_url)
        log.info(f"EmailName: {email}")
        log.warning(f"EmailID: {email_id}")
        log.debug("Exporting done!")
# manager.py
import logging
import logging.config
import logging.handlers
import multiprocessing
import logger
from saver import Saver

class Manager:

    def __init__(self) -> None:
        ### LOGGER
        # initializing listener -> this queue is going to be used for the multiprocessing logging
        self.queue = multiprocessing.Queue(-1)
        self.log = self.root_configurer(self.queue)  # getting a reference to the root logger -> used to log from this module
        self.listener = multiprocessing.Process(target=logger.listener_process,args=(self.queue,))
        self.listener.start()
        # utils
        self.log.info(f"Starting program at 10 am")
        # instantiate
        self.save = Saver()

    def root_configurer(self,queue):
        root = logging.getLogger('main')
        h = logging.handlers.QueueHandler(queue)  # Just the one handler needed
        root.setLevel(DEBUG)
        root.addHandler(h)
        return root # this is the main function -> we need to retrieve the root logger here

    def run(self):
        tuples = [("email1","id1","url1",""),("email2","id2","url2",("email3","id3","url3",("email4","id4","url4","")]
        procs = []
        for res in tuples:
            proc = multiprocessing.Process(target=self.save.doStuff,args=(res,)) 
            procs.append(proc)
            proc.start()
        # complete the processes
        for proc in procs:
            proc.join()
        
        self.log.debug("We reached this part!")
        # close listener
        self.queue.put_nowait(None)
        self.listener.join()   

if __name__ == "__main__":
    m = Manager()
    m.run()

我期望的是一堆像:

-------- using q -------------  main.saver INFO Source URL: ...
-------- using q -------------  main.saver INFO EmailName ...
-------- using q -------------  main.saver WARNING EmailID
-------- using q -------------  main.saver DEBUG ....

加上所有这些写入日志的行。出于某种原因,我得到:

EmailID: id4
EmailID: id3
EmailID: id2
-------------- using q ------------------ main -> Starting program at 10 am
2021-07-01 11:42:16,385 MainProcess main INFO     Starting program at 10 am      
-------------- using q ------------------ main.Saver -> Instantiating a saver obj
2021-07-01 11:42:16,386 MainProcess main.Saver WARNING  Instantiating a saver obj
EmailID: id4
EmailID: id1
-------------- using q ------------------ main -> We reached this part!
2021-07-01 11:42:16,852 MainProcess main DEBUG    We reached this part!

和一个文件,如:

2021-07-01 11:42:16,385 MainProcess main INFO     Starting program at 10 am
2021-07-01 11:42:16,386 MainProcess main.Saver WARNING  Instantiating a saver obj
2021-07-01 11:42:16,852 MainProcess main DEBUG    We reached this part!

有什么想法吗?

编辑 代码取自以下组合:

解决方法

您的工作人员不会写入队列。

您的代码似乎基于 Loging Cookbook 的 Logging to a single file from multiple processes。您可以在那里看到工作人员将队列作为参数,使用(通过 worker_configurer)配置自己。在您的代码中,您只配置您的经理,而不是您的工作人员。

只需将 self.queue 添加到 Process args 并将(稍微编辑的)root_configurer 方法复制到 saver.py 中以在 doStuff 启动时调用,就足以按预期工作.



主题吹毛求疵(您没有要求,但它们是免费的!):

  • “根记录器”未命名,您可以通过执行 logging.getLogger()(不带参数)来获得它。因此记录器 "main" 不是根。考虑改为将其称为 main_logger
  • break 时保留关于您为什么 record is None 退出循环的评论,我起初认为这是一个错误。
  • 如果您第一次从队列中 get 一条记录发生错误,您永远不会设置 logger 变量,因此您的异常处理程序将在它被写入 stderr 之前引发一个 UnboundLocalError .
  • 您未编写的信用代码:您提交的内容很大程度上基于 Logging Cookbook 示例。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


使用本地python环境可以成功执行 import pandas as pd import matplotlib.pyplot as plt # 设置字体 plt.rcParams['font.sans-serif'] = ['SimHei'] # 能正确显示负号 p
错误1:Request method ‘DELETE‘ not supported 错误还原:controller层有一个接口,访问该接口时报错:Request method ‘DELETE‘ not supported 错误原因:没有接收到前端传入的参数,修改为如下 参考 错误2:cannot r
错误1:启动docker镜像时报错:Error response from daemon: driver failed programming external connectivity on endpoint quirky_allen 解决方法:重启docker -> systemctl r
错误1:private field ‘xxx‘ is never assigned 按Altʾnter快捷键,选择第2项 参考:https://blog.csdn.net/shi_hong_fei_hei/article/details/88814070 错误2:启动时报错,不能找到主启动类 #
报错如下,通过源不能下载,最后警告pip需升级版本 Requirement already satisfied: pip in c:\users\ychen\appdata\local\programs\python\python310\lib\site-packages (22.0.4) Coll
错误1:maven打包报错 错误还原:使用maven打包项目时报错如下 [ERROR] Failed to execute goal org.apache.maven.plugins:maven-resources-plugin:3.2.0:resources (default-resources)
错误1:服务调用时报错 服务消费者模块assess通过openFeign调用服务提供者模块hires 如下为服务提供者模块hires的控制层接口 @RestController @RequestMapping("/hires") public class FeignControl
错误1:运行项目后报如下错误 解决方案 报错2:Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.8.1:compile (default-compile) on project sb 解决方案:在pom.
参考 错误原因 过滤器或拦截器在生效时,redisTemplate还没有注入 解决方案:在注入容器时就生效 @Component //项目运行时就注入Spring容器 public class RedisBean { @Resource private RedisTemplate<String
使用vite构建项目报错 C:\Users\ychen\work>npm init @vitejs/app @vitejs/create-app is deprecated, use npm init vite instead C:\Users\ychen\AppData\Local\npm-
参考1 参考2 解决方案 # 点击安装源 协议选择 http:// 路径填写 mirrors.aliyun.com/centos/8.3.2011/BaseOS/x86_64/os URL类型 软件库URL 其他路径 # 版本 7 mirrors.aliyun.com/centos/7/os/x86
报错1 [root@slave1 data_mocker]# kafka-console-consumer.sh --bootstrap-server slave1:9092 --topic topic_db [2023-12-19 18:31:12,770] WARN [Consumer clie
错误1 # 重写数据 hive (edu)> insert overwrite table dwd_trade_cart_add_inc > select data.id, > data.user_id, > data.course_id, > date_format(
错误1 hive (edu)> insert into huanhuan values(1,'haoge'); Query ID = root_20240110071417_fe1517ad-3607-41f4-bdcf-d00b98ac443e Total jobs = 1
报错1:执行到如下就不执行了,没有显示Successfully registered new MBean. [root@slave1 bin]# /usr/local/software/flume-1.9.0/bin/flume-ng agent -n a1 -c /usr/local/softwa
虚拟及没有启动任何服务器查看jps会显示jps,如果没有显示任何东西 [root@slave2 ~]# jps 9647 Jps 解决方案 # 进入/tmp查看 [root@slave1 dfs]# cd /tmp [root@slave1 tmp]# ll 总用量 48 drwxr-xr-x. 2
报错1 hive> show databases; OK Failed with exception java.io.IOException:java.lang.RuntimeException: Error in configuring object Time taken: 0.474 se
报错1 [root@localhost ~]# vim -bash: vim: 未找到命令 安装vim yum -y install vim* # 查看是否安装成功 [root@hadoop01 hadoop]# rpm -qa |grep vim vim-X11-7.4.629-8.el7_9.x
修改hadoop配置 vi /usr/local/software/hadoop-2.9.2/etc/hadoop/yarn-site.xml # 添加如下 <configuration> <property> <name>yarn.nodemanager.res