- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我一直在研究自定义记录器类,我创建该类是为了实现另一个“详细级别”(即跟踪)。我知道,根据记录器类的文档不推荐这样做——但是我有代码,必须有它才能进行广泛的调试。那个蜜蜂说,我可以按预期使用所有方法(LOG.warning、LOG.trace 等)调用记录器,但是当我尝试在另一个类中执行相同操作时,它不会按预期工作。我猜记录器的属性由于某种原因丢失了。
基本上我想要实现的是,我最初设置的属性(如日志格式、日志处理程序、日志级别等)保留在每个类中。
我有一个记录器类,它实现了“TRACE”:
from logging import getLoggerClass, addLevelName, setLoggerClass, NOTSET
class AioLogger(getLoggerClass()) :
TRACE = 1
def __init__(self, name, level=NOTSET):
super().__init__(name, level)
addLevelName(TRACE, "TRACE")
def trace(self, message, *args, **kwargs):
if self.isEnabledFor(AioLogger.TRACE):
self._log(AioLogger.TRACE, message, args, **kwargs)
setLoggerClass(AioLogger)
甲类:
import logging
from AioLogger import AioLogger
from b import b
LOG = AioLogger(logging.getLogger('myapp'))
class a:
def __init__(self):
LOG.setLevel(AioLogger.TRACE)
# create a file handler
file_handler = logging.FileHandler('test.log')
file_handler.setLevel(logging.DEBUG)
file_formatter = logging.Formatter('[%(asctime)s] %(filename)-15s: line %(lineno)-5d: %(levelname)-8s: '
'%(funcName)-50s: %(message)s')
file_handler.setFormatter(file_formatter)
LOG.addHandler(file_handler)
# create console handler
console_handler = logging.StreamHandler()
console_handler.setLevel(AioLogger.TRACE)
console_formatter = logging.Formatter('[%(asctime)s] %(filename)-15s: line %(lineno)-5d: %(levelname)-8s: '
'%(funcName)-50s: %(message)s')
console_handler.setFormatter(console_formatter)
LOG.addHandler(console_handler)
LOG.trace('trace')
LOG.debug('debug')
LOG.info('info')
LOG.warning('warning')
LOG.error('error')
b 类:
import logging
from AioLogger import AioLogger
LOG = AioLogger(logging.getLogger('myapp'))
class b:
def __init__(self):
LOG.trace('trace')
LOG.debug('debug')
LOG.info('info')
LOG.warning('warning')
LOG.error('error')
在类 a 的文件末尾添加以下内容:
ls = a()
cls2 = b()
并运行它,它会产生以下输出:
[2019-05-05 18:03:19,908] a.py : line 27 : TRACE : __init__ : trace
[2019-05-05 18:03:19,908] a.py : line 28 : DEBUG : __init__ : debug
[2019-05-05 18:03:19,908] a.py : line 29 : INFO : __init__ : info
[2019-05-05 18:03:19,908] a.py : line 30 : WARNING : __init__ : warning
[2019-05-05 18:03:19,908] a.py : line 31 : ERROR : __init__ : error
warning
error
出于某种原因,自定义记录器类的定义属性正在消失。我猜错误是在我的自定义记录器类中的某个地方,因为通过 logging.getlogger('myapp') 获取“默认”记录器工作正常 - 但它不提供所需的 TRACE 日志级别(因此自定义记录器类,BTW我在 stackoverflow 的某处找到了)。
最佳答案
Logger 类不是单例。在 a
类中,您调用 LOG = AioLogger(logging.getLogger('myapp'))
。 这将返回 AioLogger
的实例,而不是类。因此,在后续代码中,您自定义的是该实例,而不是类。
如果您希望 AioLogger
拥有这些处理程序和格式化程序,则必须在类中将其定义为类的属性。然后,每次实例化它时,所有这些属性都会被带走。
关于python - 自定义记录器类 python : How to make it work across classes?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55993971/
有没有办法配置 python 记录器来调用自定义函数并在它记录时将记录消息传递给它? 谢谢! 最佳答案 子类 logging.Handler 并实现 emit方法: import logging cl
我有一个这样的记录器设置: import logging from logging.handlers import RotatingFileHandler import sys # root logg
已关闭。此问题旨在寻求有关书籍、工具、软件库等的建议。不符合Stack Overflow guidelines .它目前不接受答案。 我们不允许提问寻求书籍、工具、软件库等的推荐。您可以编辑问题,以
我一直在使用 Netbeans 作为 Java IDE 进行开发。当 Netbeans 在 try-catch block 中包含一条语句时,它在捕获区域中使用类似 Logger.getLogger(
我正在记下远程日志记录的功能从头开始构建时可能需要库。我查了这个:http://www.aggsoft.com/serial-data-logger.htm 我想知道a之间有什么区别远程日志记录库和远
我需要跟踪包含数千个 JAR 和 .CLASS 文件的已编译 Java 应用程序,您知道有什么合适的工具可以附加到 JVM 来跟踪函数调用(无需源代码)吗? 最佳答案 是的。 Jprofiler无需源
我想使用记录器并设置其属性,但不想在每个对象类中创建记录器,我的目标是:在Spring中创建一个bean,创建属性文件我如何使用注释,可以做到吗? 最佳答案 是的,你可以做到, 在你的
首先,我已经阅读了real python article on the subject . 了解到记录器具有层次结构后,我想在这样的层次结构中创建一个名为 MyProjectLogger 的新记录器:
我设置了一个记录器。像这样: def initLogger(self): self.logger = logging.getLogger('MyApp') if not self.lo
是否允许为您的日志创建一个静态类? public final class Log { public static final Logger LOGGER = Logger.getLogger(
我对 java.util.logging 有一些无法解释的行为。让我们看一下这两个示例: 首先: boolean var = false; log.log( Level.WARNING, "Cant
我正在尝试开始在 python 中使用日志记录并阅读了几篇博客。一个让我感到困惑的问题是是按功能还是按模块创建记录器。在这个Blog: Good logging practice in Python建
我正在使用 https://pub.dartlang.org/packages/logging ,但它不会在我的日志中显示任何内容。这是我的代码: class Test { final Logge
public static Logger getLogger() { final Throwable t = new Throwable(); final StackTraceElem
我是 Poco 的新手,我在 Poco 在线帮助中看到了以下示例: int main(int argc, char** argv) { AutoPtr pChannel(new SimpleF
我有一个带有函数、文档字符串和文档测试的 Julia 模块文件。我加载它并在 Julia 帮助中显示文档字符串,但 Documenter.jl 找不到文档字符串。 一个示例模块文件,src/my_mo
我正在尝试在我的 Django 项目( django 1.11 、 Python 3.6 )中实现日志记录。我正在使用默认的 django 记录器。 获取 username在日志中,我使用了 djan
是否可以有多个 serilog 记录器?目前,在我的 WebApi 中,我可以调用 Log.Information 来记录信息事件,但是有没有一种方法可以改为制作不同的日志并从我的 Controlle
我有一个 python 应用程序,其文件结构类似于以下内容: /Project file1.py file2.py file3.py ... 该应用程序使用 Pytho
如何访问 python Bokeh 记录器? 我尝试使用 basicConfig包装器,但它似乎不起作用。 from bokeh.util import logconfig logconfig.bas
我是一名优秀的程序员,十分优秀!