- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
我是 Log4j 2 的新手。目前,我有这个配置文件:
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="WARN">
<Appenders>
<File name="DebugFile" fileName="../../logs/debug.log">
<PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/>
</File>
<File name="BenchmarkFile" fileName="../../logs/benchmark.log">
<PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/>
</File>
</Appenders>
<Loggers>
<Logger name="com.messaging.main.ConsoleMain" level="debug">
<AppenderRef ref="DebugFile"/>
</Logger>
<Logger name="com.messaging.main.ClientMain" level="debug">
<AppenderRef ref="BenchmarkFile"/>
</Logger>
<Root level="error">
<AppenderRef ref="DebugFile"/>
</Root>
</Loggers>
</Configuration>
如果我通过静态记录器在这两个类 ConsoleMain 和 ClientMain 中记录一些内容
static Logger _logger = LogManager.getLogger(ClientMain.class.getName());
和
static Logger _logger = LogManager.getLogger(ConsoleMain.class.getName());
他们总是使用根记录器的附加程序和级别。如果根记录器的级别如上所示是“错误”,它永远不会显示任何调试级别的日志输出,即使各个记录器的级别是调试。此外,它始终附加到根记录器中指定的日志文件,而不是类记录器中指定的日志文件。
因此,根记录器似乎以某种方式覆盖了所有内容。我如何让 log4j 实际使用 appender 和类记录器的级别?
我尝试删除根的附加程序,但它不会记录任何内容。
谢谢!
最佳答案
我尝试了您的设置,但无法重现该问题。这是我使用的代码:
package com.messaging.main;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
public class ClientMain {
public static void main(String[] args) throws Exception {
Logger logger = LogManager.getLogger(ClientMain.class);
logger.debug("debug from ClientMain");
}
}
package com.messaging.main;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
public class ConsoleMain {
public static void main(String[] args) throws Exception {
Logger logger = LogManager.getLogger(ConsoleMain.class);
logger.debug("debug from ConsoleMain");
}
}
当我使用您的确切配置文件运行这些时,我得到以下输出:
基准测试日志:
07:59:51.070 [main] DEBUG com.messaging.main.ClientMain - debug from ClientMain
调试日志:
07:59:51.070 [main] DEBUG com.messaging.main.ClientMain - debug from ClientMain
07:59:58.306 [main] DEBUG com.messaging.main.ConsoleMain - debug from ConsoleMain
07:59:58.306 [main] DEBUG com.messaging.main.ConsoleMain - debug from ConsoleMain
这是预期的行为。重复条目是正常的,因为在 log4j 中默认可加性为真,因此根记录器和命名记录器将记录相同的消息(参见 http://logging.apache.org/log4j/2.x/manual/configuration.html#Additivity )。我没有看到您报告的问题,即当根级别为“错误”时,调试级别的消息从不出现在日志文件中。
也许还有其他事情正在发生。您使用的是什么版本的 log4j2(最新版本是 beta9)?您是否也可以尝试使用上面的最少示例代码重现该问题,看看问题是否仍然存在?
关于java - Log4j 2 根记录器覆盖一切?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19210527/
有没有办法配置 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
我是一名优秀的程序员,十分优秀!