how to make Log4j configuration properly - java

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).

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

log4j2 properties file for a custom appender

I created a custom appender and it's not getting called when I run my test. Here's what the properties look like:
name=config
appenders=console, myCustomAppender
appender.console.type=Console
appender.console.name=STDOUT
appender.console.layout.type=PatternLayout
#appender.console.layout.pattern =%d{HH:mm:ss} [%t] %c{1} [%-5level] - %msg%n
appender.console.layout.pattern=%d{dd-MM-yyyy HH:mm:ss} [%-5p] (%F:%L) - %m%n
appender.myCustomAppender = com.myCompany.logging.log4j.WindowsEventLogAppender
appender.myCustomAppender.name = WindowsEventLogAppender
appender.myCustomAppender.type = WindowsEventLogAppender
rootLogger.level=info
rootLogger.appenderRefs=stdout, myCustomAppender
rootLogger.appenderRef.stdout.ref=STDOUT
My appender is called a WindowsEventLogAppender. Any idea what's wrong with my properties file? I see the console test messages but none of the messages from my appender. Right now I'm just doing a System.out.println in my custom appender to verify it's getting called.
BTW, I've found lot's of XML examples out there for log4j2 configurations with custom appenders but none for using a properties file for configuration.
Thanks,
-Mike
I might be quite late here, but I think my answer can help other people looking for answers. Please accept this as an answer if this is correct!
If you have created a custom appender having annotation like this:
#Plugin(name = "MyCustomAppender", category = "Core",
elementType = "appender", printObject = true)
public final class MyCustomAppenderImpl extends AbstractAppender {
// other code for the plugin....
}
The log4j2 manual about Configuring Appenders states that:
"An appender is configured either using the specific appender plugin's name or with an appender element and the type attribute containing the appender plugin's name"
Meaning the type for appender should be Appender Plugin's Name attribute value.
Which in above case is MyCustomAppender (appender.identifierName.type=MyCustomAppender)
So, the Properties file configuration for this to work should be:
(Note : I have added a stdout(console) appender just to show
relevance/similarity of usage with OOTB appenders, and 2 example
usages with RootLogger and a custom logger)
# this packages attribute is important, please put comma seperated package(s) to the
# plugin(s) you have created
packages = com.package.to.your.plugin
# Example: Declare and Define OOTB Console appender, which sends log events to stdout
appender.console.name = stdout
appender.console.type = Console
# Declare and define the custom appender like this
# Note that the "abc" in "appender.abc.type" can be anything
# and the value for "appender.abc.type" should be the same as
# "Name" attribute value given in custom appender plugin which is "MyCustomAppender"
appender.abc.name=arbitrary_name
appender.abc.type=MyCustomAppender
rootLogger.appenderRef.stdout.ref = stdout
rootLogger.appenderRef.abc.ref = arbitrary_name
logger.loggeridentifier.name = com.test.SomeClass
logger.loggeridentifier.appenderRef.stdout.ref = stdout
logger.loggeridentifier.appenderRef.abc.ref = arbitrary_name
# Also note that the value of appenderRef should be the same name given to your
# appender in properties file, which in this case is "arbitrary_name" (as given above)
Try adding the packages property.
Like: packages = com.myCompany
You haven't included the package info
try the below configuration.
name=config
appenders=console, myCustomAppender
appender.console.type=Console
appender.console.name=STDOUT
appender.console.layout.type=PatternLayout
#appender.console.layout.pattern =%d{HH:mm:ss} [%t] %c{1} [%-5level] - %msg%n
appender.console.layout.pattern=%d{dd-MM-yyyy HH:mm:ss} [%-5p] (%F:%L) - %m%n
appender.myCustomAppender = com.myCompany.logging.log4j.WindowsEventLogAppender
appender.myCustomAppender.name = WindowsEventLogAppender
appender.myCustomAppender.type = WindowsEventLogAppender
rootLogger.level=info
rootLogger.appenderRefs=stdout, myCustomAppender
rootLogger.appenderRef.stdout.ref=STDOUT
rootLogger.com.mycompany.example=INFO,STDOUT

log4j not logging to some files

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)

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