I have a log4j logger that currently writes the log both to console and to a file, which works fine.
Later I'd like to configure it to log INFO + ERROR to the logfile, but only show ERROR on console. What would I have to change to achieve this?
log4j.rootLogger=INFO, console, MyFileAppender
log4j.logger.org.apache.cxf=INFO, console
log4j.logger.org.apache.cxf.interceptor.LoggingInInterceptor=INFO, console
log4j.logger.org.apache.cxf.interceptor.LoggingOutInterceptor=INFO, console
log4j.appender.console=org.apache.log4j.ConsoleAppender
log4j.appender.console.Target=System.out
log4j.appender.MyFileAppender=org.apache.log4j.RollingFileAppender
log4j.appender.MyFileAppender.Append=true
log4j.appender.MyFileAppender.File=c:/logs.log
Further, I'd like to prevent the CXF XML requests to be logged in the file. But I want them still to be shown in the console. How?
Appender based configuration
Configuring the log levels per each appender has to be done separately unless it is same as the root level configuration. Below sample
log4.properties
file is configured to logINFO
and above into the console, but onlyERROR
and above into the file.Look at the last line of the below example (from "How to integrate log4j with your Java project").
Package based log levels
Any of the followings will help.
As an example:
As per this SO question (and its answer) you have to set a threshold on your appenders: