Reputation: 4772
I would like to have log4j (v1.2.17) send ...
... to fileAppender
... to console
How can I do this? I'd prefer a properties file if possible, but would switch to XML if necessary.
I've tried combinations of loggers, non-additivity, Threshold, & LevelMatchFilter but can't figure it out.
Upvotes: 0
Views: 4837
Reputation: 4713
This can definitely be done. Below you will find some example code and log4j properties that will do what you want.
Here is the first example class:
package my;
import org.apache.log4j.Logger;
public class Example1 {
private static final Logger logger = Logger.getLogger(Example1.class);
public static void main(String[] args){
logger.debug("debug from my.Example1 - should display nowhere!");
logger.info("info from my.Example1 - should display in log file only!");
logger.warn("warning from my.Example1 - should display in console and log file!");
logger.error("error from my.Example1 - should display in console and log file!");
logger.fatal("fatal from my.Example1 - should display in console and log file!");
}
}
Here is the second example class:
package my.project;
import org.apache.log4j.Logger;
public class Example2 {
private static final Logger logger = Logger.getLogger(Example2.class);
public static void main(String[] args){
logger.debug("debug from my.project.Example2 - should display nowhere!");
logger.info("info from my.project.Example2 - should display in console and log file!");
logger.warn("warning from my.project.Example2 - should display in console and log file!");
logger.error("error from my.project.Example2 - should display in console and log file!");
logger.fatal("fatal from my.project.Example2 - should display in console and log file!");
}
}
Here is the log4j.properties file:
# The root logger will accept INFO messages or higher and send them to the log file
# and to a console appender that filters out any messages below WARN level
log4j.rootLogger=INFO, R, warnStdout
# Configure a console appender that will be used for messages of any level
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
# Pattern to output the caller's file name and line number.
log4j.appender.stdout.layout.ConversionPattern=%5p [%t] (%F:%L) - %m%n
#This will be used to print WARN level or higher messages to console
log4j.appender.warnStdout=org.apache.log4j.ConsoleAppender
log4j.appender.warnStdout.layout=org.apache.log4j.PatternLayout
log4j.appender.warnStdout.Threshold=WARN
# Pattern to output the caller's file name and line number.
log4j.appender.warnStdout.layout.ConversionPattern=%5p [%t] (%F:%L) - %m%n
log4j.appender.R=org.apache.log4j.RollingFileAppender
log4j.appender.R.File=test.log
log4j.appender.R.MaxFileSize=100KB
# Keep one backup file
log4j.appender.R.MaxBackupIndex=1
log4j.appender.R.layout=org.apache.log4j.PatternLayout
log4j.appender.R.layout.ConversionPattern=%p %t %c - %m%n
# Classes in the my.project package will accept messages of INFO level or higher
# and send those messages to the console and to the log file
log4j.logger.my.project=INFO, stdout, R
# Need to set additivity to false or else both the my.project and root loggers
# will accept messages from classes in package my.project
log4j.additivity.my.project=false
Here is the console output after running Example1 followed immediately by Example2:
WARN [main] (Example1.java:11) - warning from my.Example1 - should display in console and log file!
ERROR [main] (Example1.java:12) - error from my.Example1 - should display in console and log file!
FATAL [main] (Example1.java:13) - fatal from my.Example1 - should display in console and log file!
INFO [main] (Example2.java:11) - info from my.project.Example2 - should display in console and log file!
WARN [main] (Example2.java:12) - warning from my.project.Example2 - should display in console and log file!
ERROR [main] (Example2.java:13) - error from my.project.Example2 - should display in console and log file!
FATAL [main] (Example2.java:14) - fatal from my.project.Example2 - should display in console and log file!
Here is the test.log file content after running Example1 followed by Example2:
INFO main my.Example1 - info from my.Example1 - should display in log file only!
WARN main my.Example1 - warning from my.Example1 - should display in console and log file!
ERROR main my.Example1 - error from my.Example1 - should display in console and log file!
FATAL main my.Example1 - fatal from my.Example1 - should display in console and log file!
INFO main my.project.Example2 - info from my.project.Example2 - should display in console and log file!
WARN main my.project.Example2 - warning from my.project.Example2 - should display in console and log file!
ERROR main my.project.Example2 - error from my.project.Example2 - should display in console and log file!
FATAL main my.project.Example2 - fatal from my.project.Example2 - should display in console and log file!
Upvotes: 2