Python记录多个文件

Loi*_*ix0 6 python logging error-logging

我已经阅读了日志记录模块文档,虽然我可能错过了一些明显的东西,但我得到的代码似乎没有按预期工作.我正在使用Python 2.6.4.

我的程序包含几个不同的python文件,我想从中将日志消息发送到文本文件,可能还有屏幕.我想这是常见的事情,所以我在某处弄乱了.

我的代码在一分钟做的是正确地记录到文本文件,有点.但是正在复制登录到屏幕,一个具有指定的格式,一个没有.此外,当我关闭屏幕输出时,我仍然打印一次文本,这是我不想要的 - 我只是想将它记录到文件中.

无论如何,一些代码:

#logger.py
import logging
from logging.handlers import RotatingFileHandler
import os

def setup_logging(logdir=None, scrnlog=True, txtlog=True, loglevel=logging.DEBUG):        
    logdir = os.path.abspath(logdir)

    if not os.path.exists(logdir):
        os.mkdir(logdir)

    log = logging.getLogger('stumbler')
    log.setLevel(loglevel)

    log_formatter = logging.Formatter("%(asctime)s - %(levelname)s :: %(message)s")

    if txtlog:
        txt_handler = RotatingFileHandler(os.path.join(logdir, "Stumbler.log"), backupCount=5)
        txt_handler.doRollover()
        txt_handler.setFormatter(log_formatter)
        log.addHandler(txt_handler)
        log.info("Logger initialised.")

    if scrnlog:
        console_handler = logging.StreamHandler()
        console_handler.setFormatter(log_formatter)
        log.addHandler(console_handler)
Run Code Online (Sandbox Code Playgroud)

没有什么不寻常的.

#core.py
import logging
corelog = logging.getLogger('stumbler.core')  # From what I understand of the docs, this should work :/

class Stumbler:
    [...]

    corelog.debug("Messages and rainbows...")
Run Code Online (Sandbox Code Playgroud)

屏幕输出显示了这是如何重复的:

2010-01-08 22:57:07,587 - DEBUG :: SCANZIP: Checking zip contents, file: testscandir/testdir1/music.mp3
DEBUG:stumbler.core:SCANZIP: Checking zip contents, file: testscandir/testdir1/music.mp3
2010-01-08 22:57:07,587 - DEBUG :: SCANZIP: Checking zip contents, file: testscandir/testdir2/subdir/executable.exe
DEBUG:stumbler.core:SCANZIP: Checking zip contents, file: testscandir/testdir2/subdir/executable.exe
Run Code Online (Sandbox Code Playgroud)

虽然文本文件正在获得格式正确的输出,但在logger.py中关闭屏幕日志仍然会显示格式错误的输出.

根据我对文档的理解,调用corelog.debug(),看到corelog是"stumbler"记录器的子代,它应该使用该格式并输出日志.

对这篇琐碎问题的文章表示歉意.

TL; DR:如何从多个文件进行记录?

Nic*_*ick 12

您确定在导入的任何内容中都没有进行其他日志记录设置.

控制台日志中的错误输出看起来像是记录器的默认配置,因此其他可能正在设置它.

运行此快速测试脚本:

import logging
from logging.handlers import RotatingFileHandler
import os

def setup_logging(logdir=None, scrnlog=True, txtlog=True, loglevel=logging.DEBUG):
    logdir = os.path.abspath(logdir)

    if not os.path.exists(logdir):
        os.mkdir(logdir)

    log = logging.getLogger('stumbler')
    log.setLevel(loglevel)

    log_formatter = logging.Formatter("%(asctime)s - %(levelname)s :: %(message)s")

    if txtlog:
        txt_handler = RotatingFileHandler(os.path.join(logdir, "Stumbler.log"), backupCount=5)
        txt_handler.doRollover()
        txt_handler.setFormatter(log_formatter)
        log.addHandler(txt_handler)
        log.info("Logger initialised.")

    if scrnlog:
        console_handler = logging.StreamHandler()
        console_handler.setFormatter(log_formatter)
        log.addHandler(console_handler)



setup_logging('/tmp/logs')
corelog = logging.getLogger('stumbler.core')
corelog.debug("Messages and rainbows...")
Run Code Online (Sandbox Code Playgroud)

产生这个结果:

2010-01-08 15:39:25,335 - DEBUG ::消息和彩虹......

在我的/tmp/logs/Stumbler.log中

2010-01-08 15:39:25,335 - INFO :: Logger初始化.2010-01-08 15:39:25,335 - DEBUG ::消息和彩虹......

当我在python 2.4,2.5和2.6.4中运行它时,这可以正常工作