Java 如何使用 Log4j 更改包的日志级别?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1725958/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 21:55:34  来源:igfitidea点击:

How do you Change a Package's Log Level using Log4j?

javalogginglog4jaxis2axiom

提问by Ryan Elkins

I've encountered the following bug:

我遇到了以下错误:

http://issues.apache.org/jira/browse/AXIS2-4363

http://issues.apache.org/jira/browse/AXIS2-4363

It states the following:

它声明如下:

This error only occurs when log level for org.apache.axiom is DEBUG so a workaround is to set log level > DEBUG.

此错误仅在 org.apache.axiom 的日志级别为 DEBUG 时发生,因此解决方法是设置日志级别 > DEBUG。

My question is HOW do I go about doing that? I've been scouring my directories for a properties file or something and I've been looking to see if there was something I could set in code, but I really have no idea what I'm doing. I'm running a console app on my desktop right now while trying to get this to work.

我的问题是我该怎么做?我一直在我的目录中搜索属性文件或其他东西,我一直在寻找是否可以在代码中设置一些东西,但我真的不知道我在做什么。我现在正在我的桌面上运行一个控制台应用程序,同时试图让它工作。

Update 1: I noticed that my Axis2 directory has its own log4j.properties file in its root. Is this safely ignored or is it part of the solution (or part of the problem)?

更新 1:我注意到我的 Axis2 目录在其根目录中有自己的 log4j.properties 文件。这是安全地忽略还是它是解决方案(或问题的一部分)的一部分?

Update 2: The root level log4j.properties file is apprently not set correctly. Right now it looks like this:

更新 2:根级 log4j.properties 文件显然没有正确设置。现在它看起来像这样:

log4j.rootLogger=DEBUG, R 
log4j.logger.org.apache.axiom=WARN
log4j.appender.R=org.apache.log4j.RollingFileAppender 
log4j.appender.R.MaxFileSize=10MB 
log4j.appender.R.MaxBackupIndex=10 
log4j.appender.R.layout=org.apache.log4j.PatternLayout 
log4j.appender.R.layout.ConversionPattern=%p %t %c - %m%n

but that is apparently wrong as this code returns "Log level is null":

但这显然是错误的,因为此代码返回“日志级别为空”:

System.out.println("Log level is " + logger.getLevel());

For now I am setting the log level in code using

现在我正在使用代码设置日志级别

Logger logger = Logger.getLogger("org.apache.axiom");
logger.setLevel(Level.WARN);

采纳答案by Benjamin Cox

Which app server are you using? Each one puts its logging config in a different place, though most nowadays use Commons-Logging as a wrapper around either Log4J or java.util.logging.

您使用的是哪个应用服务器?每个人都将其日志配置放在不同的位置,尽管现在大多数使用 Commons-Logging 作为 Log4J 或 java.util.logging 的包装器。

Using Tomcat as an example, this documentexplains your options for configuring logging using either option. In either case you need to find or create a config file that defines the log level for each package and each place the logging system will output log info (typically console, file, or db).

以 Tomcat 为例,本文档解释了使用任一选项配置日志记录的选项。在任何一种情况下,您都需要查找或创建一个配置文件,该文件定义每个包的日志级别以及日志系统将输出日志信息的每个位置(通常是控制台、文件或数据库)。

In the case of log4j this would be the log4j.properties file, and if you follow the directions in the link above your file will start out looking like:

在 log4j 的情况下,这将是 log4j.properties 文件,如果您按照上面链接中的说明进行操作,您的文件将开始如下所示:

log4j.rootLogger=DEBUG, R 
log4j.appender.R=org.apache.log4j.RollingFileAppender 
log4j.appender.R.File=${catalina.home}/logs/tomcat.log 
log4j.appender.R.MaxFileSize=10MB 
log4j.appender.R.MaxBackupIndex=10 
log4j.appender.R.layout=org.apache.log4j.PatternLayout 
log4j.appender.R.layout.ConversionPattern=%p %t %c - %m%n

Simplest would be to change the line:

最简单的方法是更改​​行:

log4j.rootLogger=DEBUG, R

To something like:

类似于:

log4j.rootLogger=WARN, R

But if you still want your own DEBUG level output from your own classes add a line that says:

但是,如果您仍然希望从自己的类中输出自己的 DEBUG 级别输出,请添加一行内容:

log4j.category.com.mypackage=DEBUG

Reading up a bit on Log4Jand Commons-Loggingwill help you understand all this.

稍微阅读一下Log4JCommons-Logging将帮助你理解这一切。

回答by ian_scho

I encountered the exact same problem today, Ryan.

我今天遇到了完全相同的问题,Ryan。

In my src(or your root) directory, my log4j.propertiesfile now has the following addition

在我的src(或您的根)目录中,我的log4j.properties文件现在添加了以下内容

# https://issues.apache.org/jira/browse/AXIS2-4363
log4j.category.org.apache.axiom=WARN

Thanks for the heads up as to how to do this, Benjamin.

感谢您提醒如何做到这一点,本杰明。

回答by msangel

This work for my:

这对我的工作:

log4j.logger.org.hibernate.type=trace

Also can try:

也可以试试:

log4j.category.org.hibernate.type=trace

回答by James Armstrong

set the system property log4j.debug=true. Then you can determine where your configuration is running amuck.

设置系统属性 log4j.debug=true。然后您可以确定您的配置在哪里运行。

回答by kaza

I just encountered the issue and couldn't figure out what was going wrong even after reading all the above and everything out there. What I did was

我刚刚遇到了这个问题,即使在阅读了上述所有内容和所有内容之后,也无法弄清楚出了什么问题。我所做的是

  1. Set root logger level to WARN
  2. Set package log level to DEBUG
  1. 将根记录器级别设置为 WARN
  2. 将包日志级别设置为 DEBUG

Each logging implementation has it's own way of setting it via properties or via code(lot of help available on this)

每个日志记录实现都有自己的通过属性或代码设置它的方式(有很多可用的帮助)

Irrespective of all the above I would not get the logs in my console or my log file. What I had overlooked was the below...

不管上述所有内容,我都不会在控制台或日志文件中获取日志。我忽略的是下面的...



enter image description here

在此处输入图片说明



All I was doing with the above jugglery was controlling only the production of the logs(at root/package/class etc), left of the red line in above image. But I was not changing the way displaying/consumption of the logs of the same, right of the red line in above image. Handler(Consumption) is usually defaulted at INFO, therefore your precious debug statements wouldn't come through. Consumption/displaying is controlled by setting the log levels for the Handlers(ConsoleHandler/FileHandler etc..) So I went ahead and set the log levels of all my handlers to finest and everything worked.

我对上述杂耍所做的只是控制日志的生产(在根/包/类等处),位于上图中红线的左侧。但我并没有改变上图中红线右侧的相同日志的显示/消耗方式。Handler(Consumption) 通常默认为 INFO,因此您宝贵的调试语句不会通过。通过设置处理程序(ConsoleHandler/FileHandler 等)的日志级别来控制消耗/显示。所以我继续将所有处理程序的日志级别设置为最好,一切正常。

This point was not made clear in a precise manner in any place.

这一点在任何地方都没有以准确的方式说清楚。

I hope someone scratching their head, thinking why the properties are not working will find this bit helpful.

我希望有人摸不着头脑,思考为什么属性不起作用会发现这有点帮助。