The below log4j2 configuration is failing to delete my old files.
# Rolling file appender
log4j2.appender.rolling.type = RollingRandomAccessFile
log4j2.appender.rolling.name = RollingFile
log4j2.appender.rolling.fileName = ${karaf.data}/log/karaf.log
log4j2.appender.rolling.filePattern = ${karaf.data}/log/karaf-%d{yyyy-MM-dd-HH-mm}-%i.log.zip
# uncomment to not force a disk flush
# log4j2.appender.rolling.immediateFlush = false
log4j2.appender.rolling.append = true
log4j2.appender.rolling.layout.type = PatternLayout
log4j2.appender.rolling.layout.pattern = ${log4j2.pattern}
log4j2.appender.rolling.policies.type = Policies
log4j2.appender.rolling.policies.time.type = TimeBasedTriggeringPolicy
log4j2.appender.rolling.policies.time.interval = 1
log4j2.appender.rolling.policies.time.modulate = true
log4j2.appender.rolling.policies.size.type = SizeBasedTriggeringPolicy
log4j2.appender.rolling.policies.size.size = 5MB
log4j2.appender.rolling.strategy.type = DefaultRolloverStrategy
log4j2.appender.rolling.strategy.fileIndex = nomax
# uncomment to set max number of files to create per day
# log4j2.appender.rolling.strategy.max = 10
log4j2.appender.rolling.strategy.action.type = Delete
log4j2.appender.rolling.strategy.action.basePath = ${karaf.data}/log
log4j2.appender.rolling.strategy.action.maxdepth = 1
log4j2.appender.rolling.strategy.action.condition.type = IfFileName
log4j2.appender.rolling.strategy.action.condition.glob = karaf-*.log.zip
log4j2.appender.rolling.strategy.action.condition.nested_condition.type = IfLastModified
log4j2.appender.rolling.strategy.action.condition.nested_condition.age = 5m
My log files are being generated correctly as setup by the Time Based and Size Based policies but I cannot figure out why the files aren't being deleted. Everything seems to be correct and I am getting no errors in my logs.
I have no leading or trailing spaces.
I've wasted so much time trying to debug this. The official log4j2 documentation is woeful as it doesn't give java properties based config examples.
Any ideas on how I can fix this?
Thanks
Got it working by changing:
log4j2.appender.rolling.strategy.action.condition.glob = karaf-*.log.zip
to
log4j2.appender.rolling.strategy.action.condition.glob = *.zip
Looks like the old regex pattern isn't being interpreted properly which is extremely strange since it is definitely correct.
Related
We are moving from Log4j1 to log4j2. I am able to create multiple files and added logging in those files like below:
name=PropertiesConfig
appenders = file1, file2
appender.file1.type = File
appender.file1.name = LOG1FILE1
appender.file1.fileName= ./logs/operation.log
appender.file1.layout.type=PatternLayout
appender.file1.layout.pattern= %-d{yyyy MMM dd HH:mm:ss:SSS} GMT %-d{Z} %-5p[%t] %m%n
appender.file2.type = File
appender.file2.name = LOGFILE
appender.file2.fileName= ./logs/Connection.log
appender.file2.layout.type=PatternLayout
appender.file2.layout.pattern= %-d{yyyy MMM dd HH:mm:ss:SSS} GMT %-d{Z} %-5p[%t] %m%n
rootLogger.level = info
rootLogger.additivity = false
rootLogger.appenderRefs = logfile
rootLogger.appenderRef.logfile.ref = LOGFILE
rootLogger.appenderRefs = LOG1FILE1
rootLogger.appenderRef.LOG1FILE1.ref = LOG1FILE1
I need to understand, how I can do logging in one particular file for a particular type of appender. I was able to do this in log4j1 earlier. Let's consider I have two appenders one is for Connection and another one is for Operation, so when instantiating the connection, write logs in Connection.log file whereas when operation is performing the logging happens in operation.log file. So same thing I wanted to handle in log4j2.
In Log4j 1.x you attached each appender to a different logger. In Log4j 2.x you just need to do the same:
logger.1.name = BusinessFunction
logger.1.level = TRACE
logger.1.appenderRef.1.ref = LOG1FILE1
logger.2.name = Connection
logger.2.level = TRACE
logger.2.appenderRef.1.ref = LOGFILE
You should also consider attaching the root logger to a file.
I have a java application which runs with Perl script.
In Perl, I have configured the log file properties path.
Every time I run the application, it overwrites the log file.
I want to have a new file with a unique name every time that I execute it, and not be overwritten or just a one file but loges be appended at the end of the file.
Any idea why, and how to fix it?
logconf.properties
handlers= java.util.logging.FileHandler, java.util.logging.ConsoleHandler
.level= WARNING
java.util.logging.FileHandler.pattern= /pipa/log/waach_%u_%g.log
java.util.logging.FileHandler.limit = 50000
java.util.logging.FileHandler.count = 1
java.util.logging.FileHandler.level = INFO
java.util.logging.FileHandler.formatter = java.util.logging.SimpleFormatter
java.util.logging.FileHandler.append=true
java.util.logging.ConsoleHandler.level = INFO
java.util.logging.ConsoleHandler.formatter = java.util.logging.SimpleFormatter
com.xyz.foo.level = SEVERE
ci.pipa.tari.level = INFO
.Perl script
use warnings;
use strict;
use POSIX;
my $java = "/usr/bin/java";
my $classpath = "/pipata/java/lib/pipatar.jar";
my $properties = "-Djava.util.logging.config.file=/pipata/java/log/logconfig.properties";
my $mainClass = "ci.pipa.upload.RunPipaFile";
my $command = " $java -cp $classpath $properties $mainClass 2021 2";
print "$command \n";
system ("$command");
Acording to API, java.util.logging.FileHandler.limit
specifies an approximate maximum amount to write (in bytes)
to any one file. If this is zero, then there is no limit.
(Defaults to no limit).
In my case, 5000 was not enough and every time I run, it adds the rest of the content to a new file which was overwritten the original file. I set the limit to zero, now it appends the log at the end of content inside the same file.
java.util.logging.FileHandler.limit = 0
I had a webservice using log4j, and upgraded it to use log4j2 so I could take advantage of the way log4j2 can delete old log dated log files for you while log4j can't.
The logs are stored in the pattern /my_log_root/yyyyMMdd/myservice.log. The relevant log4j.properties config looked like this:
log4j.appender.RollingAppender=org.apache.log4j.rolling.RollingFileAppender
log4j.appender.RollingAppender.RollingPolicy=org.apache.log4j.rolling.TimeBasedRollingPolicy
log4j.appender.RollingAppender.RollingPolicy.FileNamePattern=/my_log_root/%d{yyyyMMdd}/myservice.log
This worked well, except old log files were never deleted. Now in log4j2 the properties file looks like this:
property.basePath = /my_log_root/
appender.rolling.type = RollingFile
appender.rolling.fileName= ${basePath}/${date:yyyyMMdd}/myservice.log
appender.rolling.filePattern= ${basePath}/%d{yyyyMMdd}/myservice.log
appender.rolling.policies.time.type = TimeBasedTriggeringPolicy
appender.rolling.policies.time.interval = 1
appender.rolling.policies.time.modulate = true
appender.rolling.strategy.type = DefaultRolloverStrategy
appender.rolling.strategy.delete.type = Delete
appender.rolling.strategy.delete.basePath = ${basePath}
appender.rolling.strategy.delete.maxDepth = 3
appender.rolling.strategy.delete.ifLastModified.type = IfLastModified
appender.rolling.strategy.delete.ifLastModified.age = 30d
With this properties file, the old log files are deleted (the empty directories remain, but that's not too bad).
The only problem is with the current days log. It appears as if log4j2 is never refreshing the fileName once it's calculated. For example, when I relaunched the service on 2019-06-21, it wrote the file correctly to /my_log_root/20190621/myservice.log. However, now a few days later, it's still writing the current log to /my_log_root/20190621/myservice.log. At the end of the day it will rotate the log to the appropriate dated directory, and then start writing the new log to the 21st.
Is it possible to make log4j2 refresh it's current logs location each day or does it not support this?
Currently I am using log4j for logging. It is generating the log file perfectly.But the problem is: whenever I run my program new logs which are generated are getting appended to already generated logs in the same file.
Below is my log4j properties file:
log4j.rootCategory=INFO,LOGFILE
log4j.logger.org.apache.axis.enterprise=FATAL, LOGFILE
log4j.appender.LOGFILE = org.apache.log4j.DailyRollingFileAppender
log4j.appender.LOGFILE.File=applicationLogs.txt
log4j.appender.LOGFILE.Append = true
log4j.appender.LOGFILE.Threshold=DEBUG
log4j.appender.LOGFILE.DatePattern = \u2018.\u2019yyy-MM-dd
log4j.appender.LOGFILE.layout = org.apache.log4j.PatternLayout
log4j.appender.LOGFILE.layout.ConversionPattern = %d{yyyy-MM-dd
HH:mm:ss} %c{1} [%p] %m%n
I need to generate a new log file every-time I run my program.How to do that?
This file is perfect.But I want a slight modification.
Take following steps:
1. log4j.appender.LOGFILE.File=applicationLogs_${current_date}.txt
2. log4j.appender.LOGFILE.Append=false.
For current_date to work edit your main class and add following code:
static{
SimpleDateFormat dateFormat = new SimpleDateFormat("dd-MM-yyyy hhmmss");
System.setProperty("current_date", dateFormat.format(new Date()));
}
First setting will create a new log file each time your program starts depending on current date (so that you do not lose old logs) and by second setting it will not append to your old file but overwrite it.
You can use any one of the setting. First one is preferable as you are not losing your old logs.
Set the append to false, overwrite
log4j.appender.FILE.Append=false
This article helps you. https://www.tutorialspoint.com/log4j/log4j_logging_files.htm
Also this question will help you.
One logfile per run with log4j
In your code add something like below:
String today= Calendar.getInstance()
.getTime()
.toString()
.replaceAll(" ", "_")
.replaceAll(":", "");
System.setProperty("logfilename", today);
and in your properties file:
log4j.appender.file.File=C:\\${logfilename}.log
I will introduce to you a simple source example:
#Level
log4j.rootLogger = DEBUG, f
#Appender
log4j.appender.f = org.apache.log4j.RollingFileAppender
log4j.appender.f.File = C:\\Users\\myname\\Desktop\\Test.txt
#File size
log4j.appender.f.MaxFileSize = 100KB
log4j.appender.f.MaxBackupIndex = 1
I understand the working of source and see that the output result will be "logger messages" written in file Test.txt. And when I have reached the max file size of 100KB it will be created new file with name Test.txt.1
My simple issue is can I generate new file to be with name Test1.txt no Test.txt.1
Best regards,
D.Balamjiev
You may want to use a custom FileNamePattern, using %i which is the index of the file :
#Appender
log4j.appender.f = org.apache.log4j.RollingFileAppender
log4j.appender.f.File = C:\\Users\\myname\\Desktop\\Test.txt
log4j.appender.f.rollingPolicy.FileNamePattern=C:\\Users\\myname\\Desktop\\Test%i.txt
That parameter is defining how many files will be kept after deleting from rollback policy:
the official doc states:
maxBackupIndex: Maximum number of backup files to keep.
in your case that will happen every time the loger file reaches the 100KB you defined...
Thanks to Berger for the idea.
Here is my full working source
log4j.rootLogger = DEBUG, Roller
log4j.appender.Roller=org.apache.log4j.rolling.RollingFileAppender
log4j.appender.Roller.RollingPolicy=org.apache.log4j.rolling.FixedWindowRollingPolicy
log4j.appender.Roller.RollingPolicy.maxIndex=5
log4j.appender.Roller.TriggeringPolicy=org.apache.log4j.rolling.SizeBasedTriggeringPolicy
log4j.appender.Roller.TriggeringPolicy.MaxFileSize= 50000
log4j.appender.Roller.RollingPolicy.ActiveFileName=D:\\Test\\Fixed.txt
log4j.appender.Roller.layout = org.apache.log4j.PatternLayout
log4j.appender.Roller.layout.ConversionPattern = Date: %d{dd-MM-yyyy} Time: %d{HH:mm:ss} [Message is: %m] Other/%r [%t] %-5p %c %x/%n