如何使用Log4j更改程序包的日志级别? [英] How do you Change a Package's Log Level using Log4j?

查看:110
本文介绍了如何使用Log4j更改程序包的日志级别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了以下错误:

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

它说明如下:

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

This error only occurs when log level for org.apache.axiom is DEBUG so a workaround is to set log level > 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.

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

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

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

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

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

现在我要使用

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

推荐答案

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

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.

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

Using Tomcat as an example, this document explains 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).

在使用log4j的情况下,这将是log4j.properties文件,如果您按照上面链接中的说明进行操作,则文件将看起来像这样:

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.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

类似:

log4j.rootLogger=WARN, R

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

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

log4j.category.com.mypackage=DEBUG

Log4J

Reading up a bit on Log4J and Commons-Logging will help you understand all this.

这篇关于如何使用Log4j更改程序包的日志级别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆