java log4j日志级别,设置log4j日志级别

I'm currently working on a project that uses log4j.

I'm running a testcase (junit) and would like to set the log level to trace so that I can see if all the values are correct. Classes that use logging in the project contain a line like the following:

private static final Log LOG = LogFactory.getLog(MatchTaskTest.class);

and use a like like this to do the actual debugging

LOG.trace("value");

I have never used log4j before, does anybody know how I can change the log level just for the testcase, preferably simply by defining a parameter in eclipse's run configuration dialog.

解决方案

Using another configuration file

Perhaps you could point to another configuration file.

java -Dlog4j.configuration=config file yourApp

Where:

config, you file of configuration, e.g. log4j.properties or log4j.xml.

file, the log file, e.g. myApp.log

yourApp, you app, e.g. MyAppGUI

Or you can use a class

java -Dlog4j.configurationClass=config class yourApp

Where:

config, you file of configuration, e.g. log4j.properties or log4j.xml.

class, any customized initialization class, like LogManager, should implement

the org.apache.log4j.spi.Configurator

yourApp, you app, e.g. MyAppGUI

You can see more in Apache log4j 1.2 - Short introduction to log4j on Default Initialization Procedure section.

Modifying the level programmatically

Moreover, you can also use the methods that offers the Logger class, like public void setLevel(Level level), e.g.:

Logger.getRootLogger().setLevel(Level.TRACE);

Since you want only for testing purposes, you could use them. But it is recommended not to use in client code because they overwrite the default configuration parameters in hard coded. The best way is to use an external configuration file.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值