Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how can I disable output to log4j.rootLogger?

Tags:

java

log4j

I want to disable the output to the console when logging to file. See config-file below:

log4j.rootLogger=info,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=%d{ABSOLUTE} %5p %c{1}:%L --- %m%n

log4j.category.FileLog=info,R
log4j.appender.R=org.apache.log4j.DailyRollingFileAppender
log4j.appender.R.File=E:\\temp\\FileLog
log4j.appender.R.Append = true
log4j.appender.R.DatePattern='.'yyyy-MM-dd'.log'
log4j.appender.R.layout=org.apache.log4j.PatternLayout  
log4j.appender.R.layout.ConversionPattern=%m[%d{MM-dd HH:mm:ss}]%n

when I using:

Logger.getLogger("FileLog").info("LogText-FileLog");

this log print to stdout too,how can I disable it?

like image 566
Koerr Avatar asked Sep 22 '11 10:09

Koerr


2 Answers

You can use `log4j.additivity.FileLog = false, that is the 'flag' you are looking for.

From the official log4j documentation:

The output of a log statement of logger C will go to all the appenders in C and its ancestors. This is the meaning of the term "appender additivity".

However, if an ancestor of logger C, say P, has the additivity flag set to false, then C's output will be directed to all the appenders in C and its ancestors upto and including P but not the appenders in any of the ancestors of P.

Loggers have their additivity flag set to true by default.

Removing "stdout" from your root logger (as proposed in other answers) might not be a solution, because I suppose you are still interested in that console logs in some cases.

like image 65
Guido Avatar answered Oct 12 '22 05:10

Guido


remove stdout from here

log4j.rootLogger=info,stdout

To:

log4j.rootLogger=info
like image 26
Igor Konoplyanko Avatar answered Oct 12 '22 05:10

Igor Konoplyanko