Understanding of Max Backup Index in log4j framework - java

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

Related

Log4j2 java properties based configuration not deleting old files

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.

FileHandler generate extra logs files when original log is locked

I need generate one log file by each application installed and running on websphere application server 9.
I use JUL for generate log's file. My solution was create a especific Class thas inherits from FileHandler and set logs properties by a config file.
This is my code:
//Read config file
LogManager.getLogManager().readConfiguration(LoggerJUL.class.getResourceAsStream("/Logger.properties"));
//Add handler to logger
Logger.getLogger(clazz)).addHandler(new PersonalFileHandler());
PersonalFileHandler extends FileHandler, and properties are set by configure method on FileHandler class on runtime.
In this way i achieve make one log file by application running over Websphere, without overwriting the destination of the server log.
Although I achieve part of the objective, extra files are generated if the original log file is locked, same like this: testLogs.log.0, testLogs.log.1, testLogs.log.0.1, etc.
I read many suggestions and solutions, but i can't stop this isue.
Any suggestions ?
handlers = com.mucam.xxxx.PersonalFileHandler
# Set the default formatter to be the simple formatter
com.mucam.xxxx.PersonalFileHandler.formatter = java.util.logging.SimpleFormatter
# Write the log files to some file pattern
com.mucam.xxxx.PersonalFileHandler.pattern = C:/Users/pmendez/Documents/Log/testLogs.log
# Limit log file size to 5 Kb
com.mucam.xxxx.PersonalFileHandler.limit = 5000
# Keep 10 log files
com.mucam.xxxx.PersonalFileHandler.count = 10
#Customize the SimpleFormatter output format
java.util.logging.SimpleFormatter.format = %d{ISO8601} [%t] %-5p %c %x - %m%n
#Append to existing file
com.mucam.xxxx.PersonalFileHandler.append = true
Although I achieve part of the objective, extra files are generated if the original log file is locked, same like this: testLogs.log.0, testLogs.log.1, testLogs.log.0.1, etc. I read many suggestions and solutions, but i can't stop this isue. Any suggestions ?
Since your count is set to 10 you need to specify the %g pattern to log the generation.
com.mucam.xxxx.PersonalFileHandler.pattern = C:/Users/pmendez/Documents/Log/testLogs%g.log
The pattern is absolute path so if you create multiple filehandlers it will resolve conflicts by appending unique number to the end. This is specified by the %u pattern. So if you want to move where the integer is placed in the file name you specify the %u token in the pattern.
This also means that you are creating multiple instances of your custom file handler and they are not being closed. If you want to control the number of files you either need to control the number of PersonalFileHandler you create and or share a reference to singleton PersonalFileHandler. Otherwise you need to make sure that if you explicitly create a PersonalFileHandler that you are explicit closing that PersonalFileHandler before you create a second new PersonalFileHandler.
Loggers are subject to garbage collection. The line:
Logger.getLogger(clazz).addHandler(new PersonalFileHandler());
Is subject to garbage collection unless the code elsewhere as already pinned that logger in memory. This can cause log files to be created, locked, and empty.

Log4j2 RollingFile: Use todays date in current logfile (refreshing fileName)

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?

How to create log file freshly every time I run my Java Program using Log4j?

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

How to invoke a new notepad with Java and save it in a directory in windows?

I am currently logging test execution results in to a textfile(An existing one.) How do I create a textfile with a timestamp and save it in a windows directory? The code I am using currently is
File outputResultFile = new File(CompleteFileNameForNotepad);
PrintWriter outputFile = new PrintWriter(new FileWriter(outputResultFile));
String str = "text to write in to the file";
outputFile.println(str);
outputFile.close();
Thanks for your help.
String fileName = new SimpleDateFormat("yyyy-MM-ddTHH-mm-ss").format(new Date()) + ".txt";
File outputResultFile = new File(fileName);
...
Note: what you call a notepad is more commonly called a "file".
You should consider using a logging framework instead. Check out Log4j for example. You can then alter the logging format and which classes are logged by tweaking the logging configuration. This means you don't have to alter your code.
The log4j.properties below would log all classes under the package foo, at WARN level or higher to the Console, using the pattern "2000-09-07 14:07:41,508 [main] INFO MyApp - Entering application."
log4j.rootLogger=DEBUG, A1
log4j.appender.A1=org.apache.log4j.ConsoleAppender
log4j.appender.A1.layout=org.apache.log4j.PatternLayout
# Print the date in ISO 8601 format
log4j.appender.A1.layout.ConversionPattern=%d [%t] %-5p %c - %m%n
# Print only messages of level WARN or above in the package com.foo.
log4j.logger.com.foo=WARN
To log to a file use a different Appender, such as org.apache.log4j.FileAppender or org.apache.log4j.RollingFileAppender. For your purpose org.apache.log4j.DailyRollingFileAppender would be suitable.

Categories