I have a project and I work with threads in this project. I have the log4j file and thanks to this file I can write the logs to a log file but what I want is not to write to a log file.I want separate log files to be kept for each thread and they are not compatible with each other. Is it possible ?
I using this log4j.properties
#log4j.rootLogger = INFO, FILE
log4j.rootLogger = DEBUG, DAILY
# Define the FILE appender
log4j.appender.FILE=org.apache.log4j.FileAppender
log4j.appender.R.File=Logfile.log
# Define the layout for FILE appender
log4j.appender.FILE.layout=org.apache.log4j.PatternLayout
log4j.appender.FILE.layout.conversionPattern=%d{dd MMM yyyy HH:mm:ss,SSS} %t %-5p %c %M %x - %m%n
# Define the DAILY appender
log4j.appender.DAILY=org.apache.log4j.DailyRollingFileAppender
log4j.appender.DAILY.File=Logfile.log
log4j.appender.DAILY.DatePattern='.'yyyyMMdd
# Define the layout for DAILY appender
log4j.appender.DAILY.layout=org.apache.log4j.PatternLayout
log4j.appender.DAILY.layout.conversionPattern=%d{dd MMM yyyy HH:mm:ss,SSS} %t %-5p %c %M %x - %m%n
But I must change this now. For example, I have 2 classes extended from the thread running on the main class. these classes are called foo and bar. I want it to be written to the log file number 1 while the foo class is running, and to the log file number 2 while the bar class is running. What should I do ?
Related
My log4j configuration below. I want to write error in a separate file. But if I use the configuration below error info is captured in both main log file as well as error log file. How to prevent error log being written into main log file?
# LOG4J configuration
log4j.rootLogger=DEBUG,Appender1,Appender2
log4j.appender.Appender1=org.apache.log4j.FileAppender
log4j.appender.Appender1.File=/Log/Main.log
log4j.appender.Appender1.layout=org.apache.log4j.PatternLayout
log4j.appender.Appender1.Threshold=info
log4j.appender.Appender1.layout.ConversionPattern=%-7p %d [%t] %c %x - %m%n
log4j.appender.Appender2=org.apache.log4j.FileAppender
log4j.appender.Appender2.File=/Log/Error.log
log4j.appender.Appender2.layout=org.apache.log4j.PatternLayout
log4j.appender.Appender2.Threshold=error
log4j.appender.Appender2.layout.ConversionPattern=%-7p %d [%t] %c %x - %m%n
You can use the LevelMax and LevelMin configuration parameters for that
log4j.appender.Appender1.filter.a=org.apache.log4j.varia.LevelRangeFilter
log4j.appender.Appender1.filter.a.LevelMin=INFO
log4j.appender.Appender1.filter.a.LevelMax=WARN
log4j.appender.Appender2.filter.a=org.apache.log4j.varia.LevelRangeFilter
log4j.appender.Appender2.filter.a.LevelMin=ERROR
log4j.appender.Appender2.filter.a.LevelMax=ERROR
Related: https://stackoverflow.com/a/26768859/3542400
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).)
I am using below log4j.properties. I want to create file minute basis. I checked many post on this same question and tried but it didnt worked for me. I am using log4j first time, Please suggest if I am doing something wrong in properties file.
log4j.rootLogger=debug, stdout, R
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d %-5p [%c{1}] %m %n
log4j.appender.R=org.apache.log4j.RollingFileAppender
log4j.appender.R.File=/opt/aTest/log.out
log4j.appender.R.MaxFileSize=100KB
# Keep one backup file
log4j.appender.R.MaxBackupIndex=10
log4j.appender.R.layout=org.apache.log4j.PatternLayout
log4j.appender.R.layout.ConversionPattern="%d %-5p [%c{1}] %m %n
log4j.appender.DailyRoller=org.apache.log4j.DailyRollingFileAppender
log4j.appender.DailyRoller.datePattern='.'yyyy-MM-dd-HH-mm
log4j.appender.DailyRoller.file=/opt/aTest/log.out
log4j.appender.DailyRoller.layout=org.apache.log4j.PatternLayout
log4j.appender.DailyRoller.layout.ConversionPattern=%d{yyyy-MMM-dd HH:mm:ss,SSS} [%t] %c %x%n %-5p %m%n
log4j.appender.LOGFILE=org.apache.log4j.rolling.RollingFileAppender
log4j.appender.LOGFILE.RollingPolicy=org.apache.log4j.rolling.TimeBasedRollingPolicy
log4j.appender.LOGFILE.RollingPolicy.FileNamePattern=/opt/aTest/log_%d{yyyy-MM-dd-HH}.out
I am getting only one log.out file created and for each run log gets appended in same file.
You should try the XML format for configuration; the properties files are hard to understand, easy to get wrong (as you can tell :-)
The problem here is that it's not enough to define an appender, you also have to tell log4j to use it:
log4j.rootLogger=debug, stdout, DailyRoller
Also DailyRoller is a confusing name for a per-minute rolling appender.
I have found a similar question for log4net(log4net one file per run), but i am struggling to convert this to log4j.properties file format. The patters just don't get applied:
log4j.appender.file=org.apache.log4j.FileAppender
log4j.appender.file.File = logs/log-%d{yy/MM/dd HH:mm:ss}.log
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%d{yy/MM/dd HH:mm:ss} %p %c{1}: %m%n
...
I see what is wrong here, but I don't know how to fix it.
May be this is what you are asking.
http://veerasundar.com/blog/2009/08/how-to-create-a-new-log-file-for-each-time-the-application-runs/
courtesy :Is it possible to configure log4j to create a new file with every run of the application?
You need to use RollingFileAppender with the RollingStyle set to Date. So the config would look something like
log4j.appender.file=org.apache.log4j.RollingFileAppender
log4j.appender.file.RollingStyle=Date
log4j.appender.file.DatePattern=yy-MM-dd_HH-mm-ss
log4j.appender.file.StaticLogFileName=false
log4j.appender.file.File=logs/log.log
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%d{yy/MM/dd HH:mm:ss} %p %c{1}: %m%n
I don't think it is possible to use slashes or colons in the date pattern as these are not characters that can occur in a file name.
Note that this will actually create a new log file every second, which doesn't match what you've asked for in the title of your question. If you really want a new log file for every run then you should set the RollingStyle to Once. But in this case your file name will not contain a date pattern.
Try RollingFileAppender
log4j.rootLogger=debug, stdout, file
log4j.appender.file=org.apache.log4j.RollingFileAppender
log4j.appender.file.File=C:\\\\log4j-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{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n
My log4j.properties file is pasted below. My understanding is that we need to add Appenders to the root logger for the appender to work. As you can see in the below properties file, only appender A is attached to the root logger (log4j.rootLogger=info, A). However, what I see is that the logging information is printed to both the appenders (ConsoleAppender - A and File Appender - B). How is this possible?
log4j.rootLogger=info, A
log4j.appender.A=org.apache.log4j.ConsoleAppender
log4j.appender.A.layout=org.apache.log4j.PatternLayout
log4j.appender.A.layout.ConversionPattern=%-4r [%t] [rid=%X{RID} ] %-5p %c %x - %m%n
log4j.appender.B=org.apache.log4j.FileAppender
log4j.appender.B.layout=org.apache.log4j.PatternLayout
log4j.appender.B.file=target/server.log
log4j.appender.B.layout.ConversionPattern=%-4r [%t] %-5p %c %x - %m%n
Well, it turns out that the Console appender (A) was printing out to the server.log (glassfish log) which incidentally is also the name of the log file associated with appender B.
The following property was attached to the glassfish server (startup parameters) which helped me understand which log4j.properties (in case of stray properties file) file is loaded along with information like what appenders are used for logging.
-Dlog4j.debug=true