如何将日志消息从多处理模块路由到文件? [英] How to route logging messages from the multiprocessing module to a file?

查看:68
本文介绍了如何将日志消息从多处理模块路由到文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Python文档中,我发现可以在python脚本中记录日志消息使用多处理模块生成不同的进程.

In the Python docs I found that logging messages are possible in python scripts that use the multiprocessing module to spawn different processes.

import multiprocessing
import logging

logger = multiprocessing.log_to_stderr()
logger.setLevel(logging.INFO)

所以logger.warning('doomed')会给我消息'doomed',但只给控制台.

So logger.warning('doomed') would give me the message 'doomed', but only to the console.

如何将日志消息切换到文件?

推荐答案

多进程日志记录并不简单,但也不困难.您需要 logging 模块.

Multiprocess logging isn't trivial, but isn't difficult either. You'll need the logging module.

重点是要有一个侦听器进程,该进程将通过 FileHandler 或类似的

The main point is to have a listener process that will receive all the logging records through multiprocessing queues. This listener process will then handle those records with whathever internal handlers you'll decide to use. In your case you may want to use a FileHandler or similar.

如果您使用的是Python 3.2或更高版本,则将同时具有 QueueHandler QueueListener 在标准库中. Quelistener是我之前讨论的侦听器进程(是的,它确实具有start()方法).

If you are in Python 3.2 or later version you'll have both QueueHandler and QueueListener in the standard libary. Quelistener is the listener process that I was talking about before (yes, it does have a start() method).

如果您使用的是以前的Python版本,请查看此

If you are on a previous Python version take a look at this link that basically shows how to rewrite them (and how to use them).

如果您想从官方文档中获得参考,请查看会话登录到单个文件. html"rel =" nofollow noreferrer>记录食谱.

If you want a references from the official doc, take a look at the Logging to a single file from multiple processes session from the Logging Cookbook.

您可能还对此答案感兴趣. /q/641420/1132524>在Python中使用多重处理时应该如何记录?

You may also be interested in this answer from the question How should I log while using multiprocessing in Python?

这篇关于如何将日志消息从多处理模块路由到文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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