Set log4j default path in java - java

I want to set default file path for my application, in case ${custom.log.path} is not defined or by any means can't be created than logs should be generated at default location.
I am using spring application, not sure how to configure the default file path.
log4j.properties :
# Root logger option
log4j.rootLogger=DEBUG, file
log4j.appender.file=org.apache.log4j.RollingFileAppender
log4j.appender.file.File=${custom.log.path}/Application.log
log4j.appender.file.MaxFileSize=5MB
log4j.appender.file.MaxBackupIndex=10
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%d [%t] %-5p (%F:%L) - %m%n

Check this topic and answers: Is there a way to specify a default property value in Spring XML?
Spring Framework supports the following syntax:
${my.variable:defaultValue}

Related

Multiple process using the same log4j.properties

I'm actually on a project using slf4j/log4j.
For that, we use log4j.properties files to configure the logging, especially the DailyRollingFileAppender.
# Root logger option
log4j.rootLogger=INFO, file, stdout
log4j.logger.com.thales.ecosystem=DEBUG,stdout,file
log4j.additivity.com.thales.ecosystem=false
# Direct log messages to a log file
log4j.appender.file=org.apache.log4j.DailyRollingFileAppender
# this is set dynamically
log4j.appender.file.File=${log.basedir}/decoders/nm-flight-decoder.log
log4j.appender.file.DatePattern='.'yyyy-MM-dd
log4j.appender.file.ImmediateFlush=true
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss.SSS} v${release.version} %-5p %c{1}:%L - %m%n
log4j.appender.file.Append=true
# Direct log messages to 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{yyyy-MM-dd HH:mm:ss.SSS} v${release.version} %-5p %c{1}:%L - %m%n
And in code, we use the loggerFactory :
private static final Logger LOGGER = LoggerFactory.getLogger(xxxxxxxx.class);
But we have now to deal with multiple (but limited in number) identical process.
So the question is : how can we have two process (with the same class)
with only one log4j.properties per class, writting on a file each, and keeping the DailyRollingFileAppender ?
Thank's !
Make the file name in the configuration a property, and start each process with a different value for that property.
log4j.appender.file.File=${log.basedir}/decoders/${proc}.log
Start one process with -Dproc=nm-flight-decoder, and the other one with a different value.
(Also, consider upgrading to Log4j2. Log4j 1.2 has been End of Life since summer 2015 (archived here) and is known to be broken on Java 9 (archived here).)

Sending log to email using log4j SMTPAppender

I have a Spring Boot application. I'm trying to send log to mail using SMTPAppender, but I don't get any mails. I've managed to use log4j for ConsoleAppender and RollingFileAppender, so I guess I have log4j added properly, and the file with log4j properties is visible.
Here is my log4j.properties file:
log4j.rootLogger = info, email, stdout, file
log4j.appender.email=org.apache.log4j.net.SMTPAppender
log4j.appender.email.SMTPHost=smtp.google.com
log4j.appender.email.SMTPUsername=xyz#gmail.com
log4j.appender.email.SMTPPassword=abc123
log4j.appender.email.From=xyz#gmail.com
log4j.appender.email.To=xyz#gmail.com
log4j.appender.email.Subject=Log
log4j.appender.email.BufferSize=1
log4j.appender.email.EvaluatorClass=TriggerLogEvent
log4j.appender.email.layout=org.apache.log4j.PatternLayout
log4j.appender.email.layout.ConversionPattern=%m
#------------------------------------------------------------------------------------------
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%5p [%t] (%F:%L) - %m%n
#------------------------------------------------------------------------------------------
log4j.appender.file=org.apache.log4j.RollingFileAppender
log4j.appender.file.File=mylog.log
log4j.appender.file.MaxFileSize=10MB
log4j.appender.file.MaxBackupIndex=10
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n
I found a important piece from the documentation about SMTPAppender.
By default, an email message will be sent when an ERROR or higher severity message is appended. The triggering criteria can be modified by setting the evaluatorClass property with the name of a class implementing TriggeringEventEvaluator, setting the evaluator property with an instance of TriggeringEventEvaluator or nesting a triggeringPolicy element where the specified class implements TriggeringEventEvaluator
Also, you can't use "smtp.google.com" - it should be smtp.gmail.com
You should use GmailSMTPAppender, the details are here -
http://www.tgerm.com/2010/05/log4j-smtpappender-gmail-custom.html

Log4j is not finding configuration in properties file

My log4j.properties file is:
# Log levels
log4j.rootLogger=INFO,CONSOLE,file
# Appender Configuration
log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender
# Pattern to output the caller's file name and line number
log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout
log4j.appender.CONSOLE.layout.ConversionPattern=%5p [%t] (%F:%L) - %m%n
# Rolling File Appender
log4j.appender.file=org.apache.log4j.RollingFileAppender
# Path and file name to store the log file
log4j.appender.file.File=C:/Logging/log4jFile.log
log4j.appender.file.MaxFileSize=200KB
# Number of backup files
log4j.appender.file.MaxBackupIndex=2
# Layout for Rolling File Appender
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%d - %c - %p - %m%n
and the location of log4j.properties file is in WEB-INF of my project.
So when I run my servlet I get this below message:
log4j:WARN No appenders could be found for logger (...).
log4j:WARN Please initialize the log4j system properly.
Where I am going wrong, I don't understand!
I'm pretty sure by default Log4J searches in the classes directory. So try putting your log4j.properties there.
This is due to log4j searching the classpath for the log4j.properties and the WEB-INF Folder is not part of that but the classes directory is. So either put the file in classes directory, add the file to the classpath or manually load in your Servlet with:
Below is my log4j properties file which works for me. It needs to be in the src/main/resources source folder. I notice the top line is different between mine and yours so maybe this is incorrect - try changing rootLogger to rootCategory.
log4j.rootCategory=INFO, CONSOLE, FILE
log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender
log4j.appender.CONSOLE.Threshold=ERROR
log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout
log4j.appender.CONSOLE.layout.ConversionPattern=%d %-5p %c- %m%n
log4j.logger.com.myCompany.log4jexample= TRACE, FILE, CONSOLE
log4j.appender.FILE=org.apache.log4j.FileAppender
log4j.appender.FILE.File=Logs.log
log4j.appender.FILE.Threshold=WARN
log4j.appender.FILE.layout=org.apache.log4j.PatternLayout
log4j.appender.FILE.layout.ConversionPattern=%d %-5p %c- %m%n

Log4j output on console instead of configured file

I configured Log4j to log into a file but it is instead logging to stdout.
it creates the log file but it does not write to it, instead to stdout.
Here is my config file:
log4j.rootCategory=INFO, file, mail
log4j.appender.file=org.apache.log4j.RollingFileAppender
log4j.appender.file.File=/path-to/jobs-batch.log
log4j.appender.file.MaxFileSize=10MB
log4j.appender.file.MaxBackupIndex=2
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=[jobs-batch] %p [%t] %c{1}.%M(%L) | %m%n
log4j.logger.org.springframework.jdbc=WARN
# Configuration for receiving e-mails when ERROR messages occur.
log4j.appender.mail=org.apache.log4j.net.SMTPAppender
log4j.appender.mail.To=xxx#xxx.com
log4j.appender.mail.From=xxx#xxx.com
log4j.appender.mail.SMTPHost=mail.xxx.de
log4j.appender.mail.Threshold=ERROR
log4j.appender.mail.Subject=Jobs Batch Error
log4j.appender.mail.layout=org.apache.log4j.PatternLayout
log4j.appender.mail.layout.ConversionPattern=%d %-5p %c %x - %m%n
any ideas?
Thanks
Adjust to suite your needs:
log4j.rootCategory=DEBUG, C
log4j.appender.C=org.apache.log4j.ConsoleAppender
log4j.appender.C.layout=org.apache.log4j.PatternLayout
log4j.appender.C.layout.ConversionPattern=%d{MMddyyyy HH:mm} %-5p %c{1}:%L - %m%n
log4j.appender.C.Target=System.out
Things to check:
What is the name of your Log4j property file? The default is log4j.properties, which is what the Log4j library will look for on startup.
Related to what Thorbjorn asks in his comment, where is the log4j property file located? Placing it in the default package will ensure it's found (by default Log4j searches the classpath).
Optionally specify the location of the log4j property file using the log4j.configuration property when starting the JVM.
i was stupid by myself.
i was using the -Dlog4j.configuration to set my file.
BUT i was also configuring log4j programmatically to use the SAME configuration file.
i still do not understand why it did behave like that since i was just setting the SAME configuration file twice. But once i did it only once it worked as expected.

how to make log4j to write to the console as well

Is there any way to tell to log4j to write its log to the file and to the console?
thanks
there are my properties:
log4j.rootLogger=DEBUG,console,R
log4j.rootLogger=INFO, FILE
log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender
log4j.appender.CONSOLE.Target=System.out
log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout
log4j.appender.CONSOLE.layout.conversionPattern=%d{yyyy-MM-dd HH:mm:ss,SSS} %-5p - %m%n
log4j.appender.FILE=org.apache.log4j.RollingFileAppender
log4j.appender.FILE.File=log4j.log
log4j.appender.FILE.MaxFileSize=512KB
log4j.appender.FILE.MaxBackupIndex=3
log4j.appender.FILE.layout=org.apache.log4j.PatternLayout
log4j.appender.FILE.layout.conversionPattern=%d{yyyy-MM-dd HH:mm:ss,SSS} %-5p - %m%n
Your root logger definition is a bit confused.
See the log4j documentation.
This is a standard Java properties file, which means that lines are treated as key=value pairs. Your second log4j.rootLogger line is overwriting the first, which explains why you aren't seeing anything on the console appender.
You need to merge your two rootLogger definitions into one. It looks like you're trying to have DEBUG messages go to the console and INFO messages to the file. The root logger can only have one level, so you need to change your configuration so that the appenders have appropriate levels.
While I haven't verified that this is correct, I'd guess it'll look something like this:
log4j.rootLogger=DEBUG,console,file
log4j.appender.console=org.apache.log4j.ConsoleAppender
log4j.appender.file=org.apache.log4j.RollingFileAppender
Note that you also have an error in casing - you have console lowercase in one place and in CAPS in another.
Your log4j File should look something like below read comments.
# Define the types of logger and level of logging
log4j.rootLogger = DEBUG,console, FILE
# Define the File appender
log4j.appender.FILE=org.apache.log4j.FileAppender
# Define Console Appender
log4j.appender.console=org.apache.log4j.ConsoleAppender
# Define the layout for console appender. If you do not
# define it, you will get an error
log4j.appender.console.layout=org.apache.log4j.PatternLayout
# Set the name of the file
log4j.appender.FILE.File=log.out
# Set the immediate flush to true (default)
log4j.appender.FILE.ImmediateFlush=true
# Set the threshold to debug mode
log4j.appender.FILE.Threshold=debug
# Set the append to false, overwrite
log4j.appender.FILE.Append=false
# Define the layout for file appender
log4j.appender.FILE.layout=org.apache.log4j.PatternLayout
log4j.appender.FILE.layout.conversionPattern=%m%n
This works well for console in debug mode
log4j.appender.console=org.apache.log4j.ConsoleAppender
log4j.appender.console.Threshold=DEBUG
log4j.appender.console.Target=System.out
log4j.appender.console.layout=org.apache.log4j.PatternLayout
log4j.appender.console.layout.conversionPattern=%d{yyyy-MM-dd HH:mm:ss,SSS} %-5p - %m%n
Write the root logger as below for logging on both console and FILE
log4j.rootLogger=ERROR,console,FILE
And write the respective definitions like Target, Layout, and ConversionPattern (MaxFileSize for file etc).
Thats the exact usecase I was looking for my command line based tool. Wanted to show INFO on console but wanted to log more detailed info to log file. After various suggestion, finally go this solved by using 'log4j.appender.FILE.Threshold'
Something like below.:
log4j.rootLogger=DEBUG,CONSOLE,FILE
log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender
log4j.appender.FILE=org.apache.log4j.RollingFileAppender
log4j.appender.FILE.File=output/log.log
log4j.appender.CONSOLE.Threshold=INFO
log4j.appender.FILE.Threshold=DEBUG

Categories