记录器在某些文件中不起作用

2024-01-30

我有这个 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()

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

和记录器.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

EDIT

正如 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())

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

一些记录器是之前创建的logging.config.fileConfig()打电话,打电话后其他人。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

记录器在某些文件中不起作用 的相关文章

随机推荐

  • 后端使用Vite

    我们使用 Vite 作为前端 在 SvelteKit 内 它可以很好地创建 SSR 和前端代码 我特别喜欢通过 esbuild 预捆绑第 3 方软件包 有人可以告诉我是否可以将 Vite 捆绑管道用于仅后端项目 基于 koa 的 Nodej
  • iOS Swift Mi Scale 2 蓝牙称重

    我正在编写一个可以从小米米秤2获取体重测量的应用程序 读取所有可用的uuid后 只有 181B 连接 特别是 2A9C 特征 蓝牙GATT中的体重测量 收到通知 值数据为 2 164 178 7 1 1 2 58 56 253 255 24
  • 在 php-fpm/mod_fastcgi 中使用 mod_headers 时出现问题

    我正在尝试添加HSTS http en wikipedia org wiki HTTP Strict Transport Security我的应用程序中每个响应的标题 我的第一个想法是使用 mod headers 我将此指令放置在文档根目录
  • WCF REST 服务 POST 在 IIS 上返回 404 Not Found

    我在Win8上用VS2012创建了一个WCF服务 如果我通过 VS localhost port 启动服务 我就可以执行 GET 和 POST 操作 当我部署到同一台计算机上的 IIS 时 只有 GET 有效 POST 返回 404 Not
  • ListView:通过 Filterable 进行过滤与使用新 URI 重新启动 CursorLoader

    The ListView实现过滤内容的方法 您能详细说明一下什么时候应该使用它吗 据我了解 这种过滤适用于基于数组的适配器 所有数据都已经在内存中 这样 过滤器只是帮助跳过不应该显示的数据 然而 如果ListView与游标适配器 SQLit
  • 清单合并失败:uses-sdk:minSdkVersion 8 不能更小

    问题是 Error Execution failed for task app processDebugManifest 清单合并失败 uses sdk minSdkVersion 8 不能小于库 com android support s
  • 运行 showdown.js 服务器端将 Markdown 转换为 HTML(在 PHP 中)

    我正在尝试在我的网站上实现 WMD 并且想知道如何运行 showdown js 服务器端以将 markdown 转换为 HTML 为了将两者存储在数据库中 我正在使用 PHP 任何提示都会有帮助 之前从未从 php 运行过任何类型的 js
  • 升级到 AngularDart 0.13.0 并收到有关路由和 di 的错误

    我升级到 AngularDart 0 13 0 但收到以下错误 使用新的路由模块初始化 RoutingModule 的正确方法是什么di包2 0 1 In main dart I have class RoutingModule exten
  • 运行时异常 (Chromecast)

    我尝试配置 Chromecast 但收到 RuntimeException 并且不明白原因 Fabric 有日志 致命异常 java lang RuntimeException 无法启动活动 ComponentInfo activity T
  • 在 Google App Engine 上使用 Python 验证 Android 应用内购买消息的签名

    Android 开发人员网站上的示例应用程序使用 java 代码验证购买 json 有没有人有幸弄清楚如何用 python 验证购买 特别是在 GAE 中 以下是android应用内计费的相关摘录示例程序 http developer an
  • 模式浏览器不显示我在 Solr 中添加到 schema.xml 中的字段

    我在 solr v5 1 中创建的名为 Core3 的核心的 schema xml 中添加了一些字段 我不是指示例文件夹 而是使用 服务器 文件夹来运行 solr 服务器并创建 solr 核心 我使用 bin 文件夹中提供的 solr cr
  • MVVM Light WPF 打开新窗口

    我是 MVVM 新手 正在使用 MVVM Light 学习它 我在 wpf 中有一个带有登录窗口的应用程序 当用户输入正确的凭据时 登录窗口应关闭并应打开一个新的主窗口 登录部分已经可以工作 但是如何打开一个新窗口并关闭当前窗口 login
  • F# 中的命令式多态性

    OCaml 的 Hindley Milner 类型系统不允许命令式多态性 类似于 System F 除非通过最近对记录类型的扩展 这同样适用于 F 然而 有时需要将用命令式多态性 例如 Coq 编写的程序翻译成此类语言 Coq 的 OCam
  • Rails 5 多个清单文件的问题

    我的问题 在我的 Rails 5 应用程序中 我有 2 个布局 因此我需要设置多个清单文件 我做了什么 我做了以下事情 The two layouts have a user html erb and a main html erbhtml
  • Bootstrap 模态表单发布两次 Django

    我正在按照以下指示进行操作django bootstrap modal forms django bootstrap modal forms https pypi org project django bootstrap modal for
  • 使地图图钉从簇中分散开来

    我的地图有两个自定义注释类 一个用于与某个位置相关的单个帖子 另一个用于这些帖子的集群 该集群存储指向它包含的所有帖子的指针 以及中心纬度 经度位置 使用它包含的帖子的位置计算 我的行为是 当我单击集群注释时 它会删除该集群并将其帖子添加到
  • 如何使结构可调用?

    feature unboxed closures feature fn traits struct foo impl std ops Add for foo type Output foo fn add self x foo gt foo
  • 浏览器支持 stopImmediatePropagation?

    IE 支持stopPropagation 缺乏 并且需要解决方法 https stackoverflow com a 387750 165673 但我无法判断是否同样的情况也适用stopImmediatePropagation 它对所有浏览
  • Azure 函数 V2 中的延迟消息:提供的锁无效

    在我的天蓝色功能中 有时我想推迟我的消息 但如果我这样做 我会得到一个例外 7 30 2020 5 59 02 PM Message processing error Action Complete ClientId MessageRece
  • 记录器在某些文件中不起作用

    我有这个 pydev 项目 我做这个项目是为了学习如何正确使用记录器 project src core init py classHanger py scripts entrypoint py util py cli cliloggerte