eclipse中用 log4j 控制台不显示日志怎么办?

 我来答
灵异星星
2018-01-12 · TA获得超过8227个赞
知道小有建树答主
回答量:78
采纳率:0%
帮助的人:1.8万
展开全部

#--------------------------------   

#  

log4j.rootLogger=INFO, stdout  

log4j.logger.com.ibatis = DEBUG  

log4j.logger.com.ibatis.common.jdbc.SimpleDataSource = DEBUG  

log4j.logger.com.ibatis.common.jdbc.ScriptRunner = DEBUG  

log4j.logger.com.ibatis.sqlmap.engine.impl.SqlMapClientDelegate = DEBUG  

log4j.logger.java.sql.Connection = DEBUG  

log4j.logger.java.sql.Statement = DEBUG  

log4j.logger.java.sql.PreparedStatement = DEBUG 

log4j.logger.java.sql.ResultSet = INFO  

log4j.appender.stdout=org.apache.log4j.ConsoleAppender   

log4j.appender.stdout.layout=org.apache.log4j.PatternLayout   


1. 在运行的VM的参数里面增加-Dlog4j.debug 打印出log4j的测试信息。

2. 或者在VM参数里面强制增加log4j配置文件地址: -Dlog4j.configuration=log4j-

config_folder/log4j.xml

一般你应该通过第一步找到原因然后根据情况解决

如何加参数:

  1. 如果你是用的run/debug

右键->run/debug下面有一个run configurations/debug configurations->(x)=Argument

s->下方有个VM arguments: 把参数加到里面,选apply和run/debug就可以了。

2. 如果你是用的myeclipse,要在Tomcat->Tomcat n.x->JDK->Optional Java VM 

arguments下增加 -Dlog4j.debug

原文:http://stackoverflow.com/questions/3501355/log4j-output-not-displayed-in-

eclipse-console

For some reason my Eclipse console no longer displays Log4j INFO and DEBUG

statements when I run JUnit tests. In terms of code there hasn't been any change, 

so it must something to do with the Eclipse configuration.

All I do in my Unit test is the following and for some reason ONLY the ERROR 

statement is displayed in the Eclipse console. Why? Where shall I look for clues?

public class SampleTest

{ private static final Logger LOGGER = Logger.getLogger(SampleTest.class);

@Before

public void init() throws Exception

{ // Log4J junit configuration.

BasicConfigurator.configure();

LOGGER.info("INFO TEST");

LOGGER.debug("DEBUG TEST");

LOGGER.error("ERROR TEST");}}

Details:

  • log4j-1.2.6.jar

  • junit-4.6.jar Eclipse

  • IDE for Java Developers, Version: Helios Release, Build id: 20100617-1415

  • java eclipse junit log4j

    share|improve this questionedited Aug 17 '10 at

    10:21leppie68.9k8106203asked Aug 17 '10 at 10:11javaExpert1242210

  • 1 Did you already got the answer? Their is nearly every possible solution explained,

    so it would be interesting, whether one helped. –  user357206Sep 2 '10 at 7:52

    add a comment

11 Answers

active oldest votes

up vote 11 down vote

Go to Run configurations in your eclipse then -VM arguments add this: -

Dlog4j.configuration=log4j-config_folder/log4j.xml

replace log4j-config_folder with your folder structure where you have your log4j.xml 

file

share|improve this answeranswered Aug 17 '10 at 12:24Huzi--- Javiator51945    

Thanks for the suggestion. This way it worked! Although I'm still a bit annoyed for 

two reasons: 1) I thought if I used the BasicConfigurator.configure() I didn't have to 

bother having the log4j.properties 2) When I run a simple JUnit test I just want to right 

clik and "run", not having to define a configuration I've tried again my original test, 

but adding the following line to see if it was using some variable defined somewhere

in Eclipse: System.out.println(System.getProperty("log4j.configuration")); But it prints

out "null" –  javaExpertAug 17 '10 at 13:50   hmmm.. interesting.. k ll get back to yu –

Huzi--- Javiator Aug 18 '10 at 8:59

add a comment

up vote 8 down vote

Look in the log4j.properties or log4j.xml file for the log level. It's probably set

to ERROR instead of DEBUG

share|improve this answeranswered Aug 17 '10 at 10:15Aaron Digulla153k36236405    

1 Thanks for the reply Aaron. I have checked the log4.properties and it was set on

INFO. I have now changed it to DEBUG, but it doesn't make any difference. Any 

other idea? log4j.rootLogger=DEBUG, stdout

log4j.appender.stdout=org.apache.log4j.ConsoleAppender

log4j.appender.stdout.layout=org.apache.log4j.PatternLayout

log4j.appender.stdout.layout.ConversionPattern=%t:%d{ddMMMyy HH:mm:ss.SSS}

%-5p (%F:%L) %m%n – javaExpert Aug 17 '10 at 10:2310 add -Dlog4j.debug to your

VM environment startup properties to have log4j print out it's debugging info -

perhaps a different log4j configuration file is being picked up on the classpath (such

as one bundled inside a JAR) than the one you intend – matt b Aug 17 '10 at

11:35   @javaExpert: In that case, it find a different log4j.properties somewhere on 

the classpath. Look into your JARs, too! –  Aaron Digulla Aug 17 '10 at 12:23

add a comment

up vote 5 down vote

One thing to note, if you have a log4j.properties file on your classpath you do not

need to call BasicConfigurator. A description of how to configure the properties file

is here.

You could pinpoint whether your IDE is causing the issue by trying to run this class

from the command line with log4j.jar and log4j.properties on your classpath.

推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式