-->

how can I disable output to log4j.rootLogger?

2020-08-15 10:25发布

问题:

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?

回答1:

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.



回答2:

remove stdout from here

log4j.rootLogger=info,stdout

To:

log4j.rootLogger=info


回答3:

I don't know the answer to the exact question the user provided, but facing a similar problem with the following log4j code:

log4j.rootCategory=INFO, console
log4j.appender.console=org.apache.log4j.ConsoleAppender
log4j.appender.console.target=System.err
log4j.appender.console.layout=org.apache.log4j.PatternLayout
log4j.appender.console.layout.ConversionPattern=%d{yy/MM/dd HH:mm:ss} %p %c{1}: %m%n

I removed all logging lines from the console by changing

log4j.rootCategory=INFO, console

to

log4j.rootCategory=OFF, console


回答4:

Remove "stdout" from root logger. It will stop writing on the console.



标签: java log4j