how to write different information to two different files using same logger of log4j?
10,711
Solution 1
Just define a second logger variable:
Logger otherLogger = Logger.getLogger("OTHER_LOGGER");
define a configuration for it (notice the log4j.logger.OTHER_LOGGER syntax cf. log4j.rootLogger, as pointed out by user623395 and venkatesh Dodla):
log4j.logger.OTHER_LOGGER=DEBUG, OtherAppender
log4j.additivity.OTHER_LOGGER = false
#File Appender
log4j.appender.OtherAppender=org.apache.log4j.FileAppender
log4j.appender.OtherAppender.File=temp2.ppr
log4j.appender.OtherAppender.layout=org.apache.log4j.PatternLayout
log4j.appender.OtherAppender.append=false
log4j.appender.OtherAppender.layout.ConversionPattern= %m%n
and log your different text as usual:
logger.debug("My normal log");
otherLogger.info("My special text");
Solution 2
To create temp2.ppr, change from
log4j.OTHER_LOGGER=DEBUG, OtherAppender
to
log4j.**logger**.OTHER_LOGGER=DEBUG, OtherAppender
Solution 3
it worked for me. My Log file:
# log4j.properties
log4j.rootLogger=DEBUG,hfis,stdout
#For second log
log4j.logger.OTHER_LOGGER=DEBUG, OtherAppender
log4j.additivity.OTHER_LOGGER = false
#File Appender
log4j.appender.OtherAppender=org.apache.log4j.FileAppender
log4j.appender.OtherAppender.File=C:\\Legacy.log
log4j.appender.OtherAppender.ImmediateFlush=true
log4j.appender.OtherAppender.layout=org.apache.log4j.PatternLayout
log4j.appender.OtherAppender.layout.ConversionPattern=%d{dd MMM yyyy HH:mm:ss.SSS} [%t] %-5p %c - %m%n
log4j.rootCategory=ERROR
log4j.rootLogger.additivity=false
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.hfis=org.apache.log4j.RollingFileAppender
log4j.appender.hfis.Threshold=DEBUG
log4j.appender.hfis.file=C:\\hfis.log
log4j.appender.hfis.ImmediateFlush=true
log4j.appender.hfis.MaxFileSize=5MB
log4j.appender.hfis.layout=org.apache.log4j.PatternLayout
log4j.appender.hfis.layout.ConversionPattern=%d{dd MMM yyyy HH:mm:ss.SSS} [%t] %-5p %c - %m%n
# R is the RollingFileAppender that outputs to a rolling log
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Threshold=DEBUG
# Define a pattern layout for the file.
log4j.appender.mystdout.layout=org.apache.log4j.PatternLayout
log4j.appender.mystdout.layout.ConversionPattern=%d{dd MMM yyyy HH:mm:ss.SSS} %-5p: %m%n<br>
and my log variables are
final static Logger log = Logger.getLogger("hfis");
final static Logger log2 = Logger.getLogger("OTHER_LOGGER");
Author by
RanZilber
Updated on June 05, 2022Comments
-
RanZilber almost 2 years
I'd like to write to two different files using my logger, which is declared like this:
public static final Logger logger = Logger.getLogger(Adapt.class); PropertyConfigurator.configure("log4j.properties");
the file log4j contains:
log4j.rootLogger=DEBUG, FA #File Appender log4j.appender.FA=org.apache.log4j.FileAppender log4j.appender.FA.File=temp.ppr log4j.appender.FA.layout=org.apache.log4j.PatternLayout log4j.appender.FA.append=false log4j.appender.FA.layout.ConversionPattern= %m%n
Is it possible at all to use logger to write different text to two different files easily?
If not, is there a way to do that with two loggers? (I tried that and got problems because of the function configure which is static.)
Thanks.