记录器在某些文件中无法工作

2024-09-27 21:26:27 发布

您现在位置:Python中文网/ 问答频道 /正文

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

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

你知道吗类机库.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")

你知道吗脚本.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")

你知道吗入口点.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()

你知道吗客户端测试.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())

以及记录器.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"

我不明白为什么,我要求记录器以完全相同的方式登录客户端测试.py其他剧本呢

ps:我在洗衣客户端测试.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())

Tags: thenamefrompyimportinfoconfiglogging
1条回答
网友
1楼 · 发布于 2024-09-27 21:26:27

可能的猜测是,这是一个导入顺序问题以及应用程序如何设置自身。你知道吗

一些记录器在logging.config.fileConfig()调用之前创建,其他记录器在调用之后创建。你知道吗

相关问题 更多 >

    热门问题