log4j not logging to some files - java

I've seen many questions concerning log4j not writing any files, but my problem is that, log4j does (seem to) work, but not for all files.
My configuration is as follows (I have omitted a few loggers for simplicity, the below lines appear in the order they are in in the file):
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 %5p %c{1}:%L - %m%n
# this logger works without any problems
log4j.appender.errorFile=org.apache.log4j.RollingFileAppender
log4j.appender.errorFile.File=${LOG_HOME}/errors.log
log4j.appender.errorFile.MaxFileSize=20MB
log4j.appender.errorFile.MaxBackupIndex=10
log4j.appender.errorFile.layout=org.apache.log4j.PatternLayout
log4j.appender.errorFile.layout.ConversionPattern=%d %5p %c{1}:%L - %m%n
log4j.appender.errorFile.Threshold=ERROR
# this file is created but always empty
log4j.appender.barcodeScanner=org.apache.log4j.RollingFileAppender
log4j.appender.barcodeScanner.File=${LOG_HOME}/barcodeScanner.log
log4j.appender.barcodeScanner.MaxFileSize=20MB
log4j.appender.barcodeScanner.MaxBackupIndex=5
log4j.appender.barcodeScanner.layout=org.apache.log4j.PatternLayout
log4j.appender.barcodeScanner.layout.ConversionPattern=%d %5p %c{1}:%L - %m%n
# this logger also works
log4j.appender.DoubleDtpaList=org.apache.log4j.RollingFileAppender
log4j.appender.DoubleDtpaList.File=${LOG_HOME}/DoubleDtpaList.log
log4j.appender.DoubleDtpaList.MaxFileSize=20MB
log4j.appender.DoubleDtpaList.MaxBackupIndex=1
log4j.appender.DoubleDtpaList.layout=org.apache.log4j.PatternLayout
log4j.appender.DoubleDtpaList.layout.ConversionPattern=%d %5p %c{1}:%L - %m%n
log4j.rootLogger=error, errorFile
log4j.logger.scanner.BarcodeScanner=debug, barcodeScanner
log4j.logger.util.DetectDoubles=trace, DoubleDtpaList
Using the logger within the BarcodeScanner class yields correct results when using logger.error() (this logs to the errors.log file), but all calls to logger.debug() and logger.info() have no effect.
The variable LOG_HOME is passed as an argument/option when executing the *.jar file.
EDIT 1: I added another (working) logger/appender configuration for clarification. Another piece of information I unfortunately left out ist that I want all logs for the scanner to go to the barcodeScanner.log file (with the errors additionally going to errors.log (root Logger)

Related

Log4j2: Logs only to a file, without console?

I'm using log4j2.
I have two appenders that write logs to two different files, and this works fine.
But these logs also get into the console appender.
I want to free the console from logs for files, and so that the rest of the logs get to the console.
How can I do that?
# Declare logger
name=LoggingConfig
appenders=a_console, a_test1, a_test2
loggers=l_project, l_test1, l_test2
# Root logger
rootLogger.level=INFO
rootLogger.appenderRefs=a_console
rootLogger.appenderRef.a_console.ref=STDOUT
# Project logger
logger.l_project.name=re.vianneyfaiv.log4j2
logger.l_project.level=DEBUG
logger.l_project.additivity=false
logger.l_project.appenderRef.a_console.ref=STDOUT
# Console logger
appender.a_console.type=Console
appender.a_console.name=STDOUT
appender.a_console.layout.type=PatternLayout
appender.a_console.layout.pattern=%d{ISO8601} [%t] %-5p (%F\:%L) - %m%n
# File logger - 1
appender.a_test1.type=RollingFile
appender.a_test1.name=FILE1
appender.a_test1.append=false
appender.a_test1.layout.type=PatternLayout
appender.a_test1.layout.pattern=%d{ISO8601} [%t] %-5p (%F\:%L) - %m%n
appender.a_test1.fileName=log4j2-sample-1.log
appender.a_test1.filePattern=log4j2-sample-1-%d{yyyy-MM-dd}.log
appender.a_test1.policies.type=Policies
appender.a_test1.policies.time.type=TimeBasedTriggeringPolicy
appender.a_test1.policies.time.interval=1
logger.l_test1.name=re.vianneyfaiv.log4j2.Log4J2Test1
logger.l_test1.level=ERROR
logger.l_test1.appenderRef.a_test1.ref=FILE1
# File logger - 2
appender.a_test2.type=RollingFile
appender.a_test2.name=FILE2
appender.a_test2.append=false
appender.a_test2.layout.type=PatternLayout
appender.a_test2.layout.pattern=%d{ISO8601} [%t] %-5p (%F\:%L) - %m%n
appender.a_test2.fileName=log4j2-sample-2.log
appender.a_test2.filePattern=log4j2-sample-2-%d{yyyy-MM-dd}.log
appender.a_test2.policies.type=Policies
appender.a_test2.policies.time.type=TimeBasedTriggeringPolicy
appender.a_test2.policies.time.interval=1
logger.l_test2.name=re.vianneyfaiv.log4j2.Log4J2Test2
logger.l_test2.level=INFO
logger.l_test2.appenderRef.a_test2.ref=FILE2
Just remove this console logger portion from properties file
# Console logger
appender.a_console.type=Console
appender.a_console.name=STDOUT
appender.a_console.layout.type=PatternLayout
appender.a_console.layout.pattern=%d{ISO8601} [%t] %-5p (%F\:%L) - %m%n
I'm add:
logger.l_test2.additivity=false
logger.l_test2.additivity=false

how to make Log4j configuration properly

I am new to the Log4j framework and after reading some stuff i got some fare idea about logging mechanism, but still
i have some doubt about the following properties.
log4j.category.com.cloud.sample=INFO, file, C
log4j.additivity.com.cloud.sample=true
log4j.appender.C=org.apache.log4j.ConsoleAppender
log4j.appender.C.Target=System.out
log4j.appender.C.ImmediateFlush=true
log4j.appender.C.layout=org.apache.log4j.PatternLayout
log4j.appender.C.layout.ConversionPattern=%-5p %d [%t] %m%n
#log4j.rootLogger=INFO, A1
log4j.appender.A1=org.apache.log4j.ConsoleAppender
log4j.appender.A1.layout=org.apache.log4j.PatternLayout
log4j.appender.A1.layout.ConversionPattern=%d [%t] %-5p %c - %m%n
### direct messages to file ###
log4j.appender.file=org.apache.log4j.RollingFileAppender
log4j.appender.file.File=${catalina.home}/var/basic/logs/sample.log
log4j.appender.file.Append=true
log4j.appender.file.MaxFileSize=10MB
# mylog.log.10 \u307e\u3067\u4fdd\u6301
log4j.appender.file.MaxBackupIndex=50
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%d %5p [%t] %c{1} - %m%n
log4j.rootLogger=INFO, C, file
In the first line of the above code contains two appenders(file, C) after that we will be having appender for both file and C. So as per my understanding logs will of category will be stored to Console and sample.log. Please let me know if i am wrong.
log4j.rootLogger=INFO, A1 and respective properties are not used right?
log4j.rootLogger=INFO, C, file: This line is about root logger, I think in my case it is not useful, because it is defined at the last line and there is no properties defined over here.
Please could any body confirm my understanding and suggest me if any changes required in the above configuration
The root logger resides at the top of the logger hierarchy. It is exceptional in three ways:
it always exists,
its level cannot be set to null
it cannot be retrieved by name.
The rootLogger is the father of all appenders. Each enabled logging request for a given logger will be forwarded to all the appenders in that logger as well as the appenders higher in the hierarchy (including rootLogger).

Logging to 2 log files using 2 loggers is duplicating the logs (using log4j)

I am trying to create 2 log files using log4j using this configuration:
log4j.rootLogger=debug,stdout,logfile
#debug info warn error fatal
# Set the enterprise logger category to FATAL and its only appender to CONSOLE.
log4j.logger.org.apache=FATAL,logfile
# ConsoleAppender properties
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.Threshold=debug
log4j.appender.stdout.layout.ConversionPattern=%d %5p %c{1}:%L - %m%n
#%-5p [%t]: %m%n
# LogFileAppender properties
log4j.appender.logfile=com.xx.util.SizeRollingFileAppender
log4j.appender.logfile.File=log.log
log4j.appender.logfile.MaxFileSize=50240KB
log4j.appender.logfile.BackupsDirectory=log
log4j.appender.logfile.layout=org.apache.log4j.PatternLayout
log4j.appender.logfile.layout.ConversionPattern=%d %5p %c{1}:%L - %m%n
log4j.appedner.logfile.Threshold=error
# Log1FileAppender properties
log4j.appender.log1=com.xx.util.SizeRollingFileAppender
log4j.appender.log1.File=log1.log
log4j.appender.log1.MaxFileSize=5120KB
log4j.appender.log1.BackupsDirectory=log
log4j.appender.log1.layout=org.apache.log4j.PatternLayout
log4j.appender.log1.layout.ConversionPattern=%d %5p %c{1} :%L - %m%n
(the xx is for the project that I am working on) -
And here it is the Java Code:
public class TestLog4j {
/**
* #param args
*/
private static org.apache.log4j.Logger log = org.apache.log4j.Logger.getLogger(TestLog4j.class);
private static org.apache.log4j.Logger log1= org.apache.log4j.Logger.getLogger("log1");
public static void main(String[] args) {
// TODO Auto-generated method stub
org.apache.log4j.PropertyConfigurator.configure("src/com/test/log4j.properties");
log.error("Error Test Log");
log1.info("Test Log1");
}
}
My problem is that I want to write two different files, one for log and one for log1. What I am getting with this code is only one file being created with both logs in the same file.
What I am doing wrong?
Short answer:
Add:
log4j.logger.log1=<yourLogLevel>,log1
to your log4j.properties.
NOTE:
Every enabled log-request to log1 (log-level >= yourLogLevel) will go to both stdout and logfile unless you turn off appender-additivity!

Log4j Logging to multiple files

In my Java console app, I want to log certain events to a log file and certain others to console. This is what I've got now
log4j.rootLogger=error, 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.appender.L1=org.apache.log4j.FileAppender
log4j.appender.L1.layout=org.apache.log4j.PatternLayout
log4j.appender.L1.layout.ConversionPattern=%-22d{dd/MMM/yyyy HH:mm:ss} – %m%n
log4j.appender.L1.file=failedtoaddusers.log
In my Java app, I instantiate two log instances using
private static Logger log = Logger.getLogger(ActiveDirectoryManage.class);
private static Logger failedToAddUsersLogger = Logger.getLogger("FailedToAddUsersLogging");
My issue is that failedToAddUsersLogger.warn("xyz") also writes to the console in addition to the log file failedtoaddusers.log.
I just want it to write to the log file and not to the console.
How do I accomplish that?
You need to set additivity to "false" (read more on logger additivity in the Appenders and Layouts section of the log4j manual):
log4j.additivity.FailedToAddUsersLogging=false
log4j.logger.FailedToAddUsersLogging = your level, L1
Also, make sure you have one of new versions of log4j, "additivity" setting was not available from beginning.

how to write different information to two different files using same logger of log4j?

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.
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");
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");
To create temp2.ppr, change from
log4j.OTHER_LOGGER=DEBUG, OtherAppender
to
log4j.**logger**.OTHER_LOGGER=DEBUG, OtherAppender

Categories