记录器在某些文件中无法使用 [英] Logger won't work in some files

查看:72
本文介绍了记录器在某些文件中无法使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个pydev项目,目的是学习如何正确使用记录器

I have this pydev project which I did in order to learn how to properly use the logger

project
..|.src
..|..|.core
..|..|...|__init__.py
..|..|...|classHanger.py
..|..|...|scripts
..|..|.entrypoint.py
..|..|.util.py
..|
..|.cli
..|..|.cliloggertest.py
..|
..|.config
.....|.logger.conf

classHangar.py

classHangar.py

#!/usr/bin/env python
# --*-- encoding: iso-8859-1 --*--

import logging

class SpeakingClass(object):

    def __init__(self):
        self.logger = logging.getLogger("%s.%s" % (__name__, "SpeakingClass"))

    def speakingMethod(self):
        self.logger.info("I'm a method from a SpeakingClass instance")

scripts.py

scripts.py

#!env python
# --*-- encoding: iso-8859-1 --*--

import logging

logger = logging.getLogger("%s.%s" % (__name__, "scripts"))

def anotherRandomMethod():
    logger.info("Now I'm talking from core.scripts.anotherRandomMethod")

entrypoint.py

entrypoint.py

#!/usr/bin/env python
# --*-- encoding: iso-8859-1 --*--

import logging

from core.classHangar import SpeakingClass
from core.scripts import anotherRandomMethod

logger = logging.getLogger("%s.%s" % (__name__, "entrypoint"))

def randomMethod():
    logger.info("Now I'm in the entrypoint.randomMethod")

def methodCalledByCli():
    logger.info("Now I'm in the entrypoint.methodCalledByCli")
    randomMethod()
    anotherRandomMethod()
    speaking_object = SpeakingClass()
    speaking_object.speakingMethod()

cliloggertest.py

cliloggertest.py

#!env python
# --*-- encoding: iso-8859-1 --*--

import sys
sys.path.insert(0, '../src/')
import os
import logging.config
import util

from entrypoint import methodCalledByCli

def main():

    logging.config.fileConfig(os.path.join(util.getProjectPath(), "config/logger.conf"))
    logger = logging.getLogger("%s.%s" % (__name__, "cli"))

    logger.info("I'm talking from the CLI script")

    return methodCalledByCli()

if __name__ == "__main__":
    sys.exit(main())

和logger.conf

and logger.conf

[loggers]
keys=root

[handlers]
keys=syserr

[formatters]
keys=basicformatter

[logger_root]
level=DEBUG
handlers=syserr

[handler_syserr]
class=StreamHandler
formatter=basicformatter
args=(sys.stderr,)

[formatter_basicformatter]
format=%(asctime)s  %(levelname)-9s%(name)-35s: %(message)s
datefmt=

我通常应该得到:

"I'm talking from the CLI script"
"Now I'm in the entrypoint.methodCalledByCli"
"Now I'm in the entrypoint.randomMethod"
"Now I'm talking from core.scripts.anotherRandomMethod"
"I'm a method from a SpeakingClass instance"

但是我得到的只是:

"I'm talking from the CLI script"
"I'm a method from a SpeakingClass instance"

我不明白为什么,我要求记录器以完全相同的方式记录cliloggertest.py和其他脚本中的

I don't see why, I requested the logger from logging exactly the same way in cliloggertest.py and the other scripts

ps:我正在启动cliloggertest.py

ps : I'm lauching cliloggertest.py

如Mikko Ohtamaa所述,这是由于导入顺序造成的,cli在导入入口点,而入口点正在导入脚本,因此,在CLI中设置配置之前,已创建了脚本的记录器和入口点的记录器

As stated by Mikko Ohtamaa it's because of the import order, cli was importing entrypoint and entrypoint is importing scripts, thus script's logger and entrypoint's logger are created BEFORE setting the configuration in CLI

以这种方式更改cliloggertest可解决问题(在任何非内部python导入之前设置配置:

Changing cliloggertest this way solves the problem (set the configuration before ANY non internal python import :

#!env python
# --*-- encoding: iso-8859-1 --*--

import sys
sys.path.insert(0, '../src/')
import os
import util
import logging.config
logging.config.fileConfig(os.path.join(util.getProjectPath(), "config/logger.conf"))

from entrypoint import methodCalledByCli

def main():

    logger = logging.getLogger("cliloggertest")

    logger.info("I'm talking from the CLI script")

    return methodCalledByCli()

if __name__ == "__main__":
    sys.exit(main())

推荐答案

可能会猜测这是一个导入顺序问题,以及您的应用程序如何进行设置.

May guess is that this is an order of imports issue and how your application sets up itself.

logging.config.fileConfig()调用之前创建了一些记录器,在调用之后创建了其他记录器.

Some loggers get created before logging.config.fileConfig() call, others after the call.

这篇关于记录器在某些文件中无法使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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