Java 如何在 Log4j 中启用 Logger.debug()
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1673485/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to enable Logger.debug() in Log4j
提问by Hariharbalaji
While trying to execute the following lines only the last two statements are displayed("Here is some ERROR" and "Here is some FATAL") and the first three statements are not displayed.I had just started to learn this topic, can anyone tell why is this happening?
尝试执行以下几行时,只显示最后两条语句(“这里有一些错误”和“这里有一些 FATAL”),前三个语句没有显示。我刚刚开始学习这个主题,谁能告诉为什么会这样?
logger.debug("Here is some DEBUG");
logger.info("Here is some INFO");
logger.warn("Here is some WARN");
logger.error("Here is some ERROR");
logger.fatal("Here is some FATAL");
the log4j.property has
log4j.property 有
log4j.rootLogger=debug,stdout
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.out
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=[%5p] %d{mm:ss}(%F:%M:%L)%n%m%n%n
采纳答案by Thomas L?tzer
You probably have a log4j.properties file somewhere in the project. In that file you can configure which level of debug output you want. See this example:
您可能在项目的某处有一个 log4j.properties 文件。在该文件中,您可以配置所需的调试输出级别。看这个例子:
log4j.rootLogger=info, console
log4j.appender.console=org.apache.log4j.ConsoleAppender
log4j.appender.console.layout=org.apache.log4j.PatternLayout
log4j.appender.console.layout.ConversionPattern=%-4r [%t] %-5p %c %x - %m%n
log4j.logger.com.example=debug
The first line sets the log level for the root logger to "info", i.e. only info, warn, error and fatal will be printed to the console (which is the appender defined a little below that).
第一行将根记录器的日志级别设置为“信息”,即只有信息、警告、错误和致命会打印到控制台(这是在其下方定义的附加程序)。
The last line sets the logger for com.example.* (if you get your loggers via LogFactory.getLogger(getClass())
) will be at debug level, i.e. debug will also be printed.
最后一行设置 com.example.* 的记录器(如果您通过 获取记录器LogFactory.getLogger(getClass())
)将处于调试级别,即 debug 也将被打印。
回答by yawn
Put a file named log4j.xml
into your classpath. Contents are e.g.
将一个名为的文件log4j.xml
放入您的类路径中。内容是例如
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">
<appender name="stdout" class="org.apache.log4j.ConsoleAppender">
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d{ABSOLUTE} %5p %t %c{1}:%L - %m%n"/>
</layout>
</appender>
<root>
<level value="debug"/>
<appender-ref ref="stdout"/>
</root>
</log4j:configuration>
回答by Thomas Owens
You need to set the logger level to the lowest you want to display. For example, if you want to display DEBUG messages, you need to set the logger level to DEBUG.
您需要将记录器级别设置为要显示的最低级别。例如,如果要显示 DEBUG 消息,则需要将记录器级别设置为 DEBUG。
The Apache log4j manualhas a section on Configuration.
在Apache的log4j的手册,对配置的部分。
回答by TomY
This is happening due to the fact that the logging level of your logger is set to 'error' - therefore you will only see error messages or above this level in terms of severity so this is why you also see the 'fatal' message.
发生这种情况是因为您的记录器的日志记录级别设置为“错误” - 因此您只会看到错误消息或在严重性方面高于此级别,因此这就是您也会看到“致命”消息的原因。
If you set the logging level to 'debug' on your logger in your log4j.xml you should see all messages.
如果您在 log4j.xml 中的记录器上将日志记录级别设置为“调试”,您应该会看到所有消息。
Have a look at the log4jintroduction for explaination.
查看log4j介绍以进行解释。
回答by rsp
This is probably happening because your log4j configuration is set to ERROR
. Look for a log4j.properties file with contents like the following:
这可能是因为您的 log4j 配置设置为ERROR
. 查找内容如下的 log4j.properties 文件:
log4j.rootLogger=ERROR, CONSOLE
# console logging
log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender
log4j.appender.CONSOLE.Threshold=DEBUG
log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout
log4j.appender.CONSOLE.layout.ConversionPattern=%d %-5p %-20.20t %-24c{1}: %m%n
The rootLogger
is set to ERROR
level here using a CONSOLE
appender.
将rootLogger
被设置为ERROR
使用此级别CONSOLE
的appender。
Note that some appenders like the console appender also have a Threshold
property that can be used to overrule the rootLoggers
level. You need to check both in this case.
请注意,某些附加程序(如控制台附加程序)也具有Threshold
可用于否决rootLoggers
级别的属性。在这种情况下,您需要检查两者。
回答by ChadNC
I like to use a rolling file appender to write the logging info to a file. My log4j properties file typically looks something like this. I prefer this way since I like to make package specific logging in case I need varying degrees of logging for different packages. Only one package is mentioned in the example.
我喜欢使用滚动文件附加程序将日志信息写入文件。我的 log4j 属性文件通常看起来像这样。我更喜欢这种方式,因为我喜欢制作特定于包的日志,以防我需要为不同的包进行不同程度的日志记录。示例中只提到了一个包。
log4j.appender.RCS=org.apache.log4j.DailyRollingFileAppender
log4j.appender.RCS.File.DateFormat='.'yyyy-ww
#define output location
log4j.appender.RCS.File=C:temp/logs/MyService.log
#define the file layout
log4j.appender.RCS.layout=org.apache.log4j.PatternLayout
log4j.appender.RCS.layout.ConversionPattern=%d{yyyy-MM-dd hh:mm a} %5 %c{1}: Line#%L - %m%n
log4j.rootLogger=warn
#Define package specific logging
log4j.logger.MyService=debug, RCS
回答by Stephen C
Here's a quick one-line hack that I occasionally use to temporarily turn on debug logging in a JUnit test:
这是我偶尔用来临时打开 JUnit 测试中的调试日志记录的快速单行技巧:
Logger.getRootLogger().setLevel(Level.DEBUG);
回答by Glenn Lawrence
If you are coming here because you are using Apache commons logging with log4j and log4j isn't working as you expect then check that you actually have a log4j.jar in your run-time classpath. That one had me puzzled for a little while. I have now configured the runner in my dev environment to include -Dlog4j.debugin the Java command line so I can always see that Log4j is being initialized correctly
如果您来到这里是因为您使用带有 log4j 的 Apache commons 日志记录并且 log4j 没有按预期工作,那么请检查您的运行时类路径中是否确实有一个 log4j.jar。那个让我疑惑了一会儿。我现在已经在我的开发环境中配置了运行器,在 Java 命令行中包含-Dlog4j.debug,所以我总是可以看到 Log4j 正在正确初始化