Logger无法在某些文件中使用

时间:2015-09-15 11:11:54

标签: python python-2.7 logging

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

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

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

#!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

#!/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

#!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

[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和其他脚本中以完全相同的方式记录

ps:我正在发表cliloggertest.py

修改

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

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

#!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())

1 个答案:

答案 0 :(得分:1)

可能猜测这是导入问题的顺序以及您的应用程序如何设置自己。

有些记录器在logging.config.fileConfig()呼叫之前创建,其他记录器在呼叫之后创建。