log4j2 RollingRandomAccessFile add one day in file name - java

I have this in my log4j2.xml file:
<Appenders>
<RollingRandomAccessFile name="RollingRandomAccessFile" fileName="${log-path}/server.log"
filePattern="${log-path}/server-%d{dd-MMM-yyyy-HH-mm-ss}.log"
immediateFlush="false" append="true">
<PatternLayout>
<Pattern>[%p] %d %c [%t] [%marker] %m %ex%n</Pattern>
</PatternLayout>
<policies>
<TimeBasedTriggeringPolicy interval="86400" modulate="true" />
<SizeBasedTriggeringPolicy size="10 MB" />
</policies>
</RollingRandomAccessFile>
</Appenders>
I want that my log files are generated when the log file is more than 10mb and / or change the date.
I want this example:
server.log (today file)
server-27-10-2015-16-26-45.log (today file)
server-27-10-2015-16-55-33.log (today file)
server-28-10-2015-08-11-42.log (tomorrow file)
But in my case, the first log file generated after server.log has the tomorrow date:
server.log (today file)
server-28-10-2015-16-26-45.log (today file)
server-28-10-2015-16-55-33.log (today file)
server-29-10-2015-08-11-42.log (tomorrow file)
The files are created each 10 mb but I see that the date is not correct.
For some reason it adds 1 day to the file name.
Today is oct 27 and the first file created after overcoming 10 mb contains the tomorrow date (oct 28) in the name.

Related

Log4j 2 rolling file with wrong timestamp

I have following Roling file:
<RollingFile name="mylog"
filePattern="${sys:server.output.dir}/logs/mylog_%d{yyyy-MM-dd}.%i.log"
ignoreExceptions="false">
<PatternLayout>
<Pattern>%d %-5p %c %m%n\n</Pattern>
</PatternLayout>
<Policies>
<OnStartupTriggeringPolicy minSize="100000000" />
<SizeBasedTriggeringPolicy size="100 MB" />
<TimeBasedTriggeringPolicy />
</Policies>
<DirectWriteRolloverStrategy />
</RollingFile>
But I am getting wrong timestamp.
If the log is created on 24th of June it is called mylog_2019-06-23.1.log and it contains files from 24th.
Last files generated to better illustrate:
mylog_2019-06-20.1.log - generated on 21st at 23:50 - contains logs from 21st
mylog_2019-06-21.1.log - 22nd at 23:50 - contains logs from whole 22nd
mylog_2019-06-22.1.log - 23rd at 00:00 - contains logs from 23rd to 7 AM
mylog_2019-06-23.1.log - 23rd at 23:50 - contains logs from 23rd 7 AM to end of day
mylog_2019-06-23.2.log - 24th at 00:00 - contains logs from today (24th) until now
What am I doing wrong?
(same happens with any interval, e.g. minutes - the stamp is always one unit off)
So in the end I had to add fileName attribute to RollingFile element and change strategy to and it works now. Nomax is there just not to limit number of files to keep.
The side effect is that now the logs are first logged into mylog.log and when the file gets rollovered it gets renamed to mylog_{stamp}.log.
<RollingFile name="myLog"
fileName="${sys:server.output.dir}/logs/mylog.log"
filePattern="${sys:server.output.dir}/logs/mylog_%d{yyyy-MM-dd}.%i.log"
ignoreExceptions="false">
<PatternLayout>
<Pattern>%d %-5p %c %m%n\n</Pattern>
</PatternLayout>
<Policies>
<OnStartupTriggeringPolicy/>
<SizeBasedTriggeringPolicy size="100 MB" />
<TimeBasedTriggeringPolicy/>
</Policies>
<DefaultRolloverStrategy fileIndex="nomax"/>
</RollingFile>

How do I delete a log file while using RollingFileAppender in log4j?

Sometimes, while rolling log files, there seems to be some issue occurring like: 'Unable to delete log file', 'Unable to move log file'.
Error: java.nio.file.FileSystemException a.log -> directory\a-2011-08-09-2.log: The process cannot access the file because it is being used by another process.
Following is the log4j configuration:
<RollingFile name="a" fileName="${sys:catalina.base}/logs/a.log" append="true" createOnDemand="true" filePattern="${sys:catalina.base}/logs/a-%d{yyyy-MM-dd}-%i.log">
<PatternLayout>
<Pattern>%d{ISO8601}|%-5p|%i|%T|%s|%R|%t|%c{1} - %m%n</Pattern>
</PatternLayout>
<Policies>
<TimeBasedTriggeringPolicy />
<SizeBasedTriggeringPolicy size="10MB"/>
</Policies>
<DefaultRolloverStrategy max="25"/>
</RollingFile>

log4j2 periodically cleaning log file

I use log4j2 to log my programs. In my xml configuration file, I have this appender:
<RollingFile name="General" fileName="log/logs/giornale.log" filePattern="log/logs/log-%d{yyyyMMdd}.log">
<PatternLayout>
<Pattern>%d{HH:mm:ss,SSS} [%t] %-5level %logger{-1} - %msg%n</Pattern>
</PatternLayout>
<Policies>
<TimeBasedTriggeringPolicy />
</Policies>
</RollingFile>
What I need is a way to configure it, so that it will delete automatically all files older than n days.
I already found some questions like this, but they don't help, since they don't say how to do it via xml configuration.
To make it short, where exactly am I suppose to indicate parameters like "MaxBackupIndex" in the above snippet? Or which other parameter should I use (and where can I put it)?
Add the following tag under 'RollingFile' tag. Remove the 'policies' tag. You probably don't need it.
<DefaultRolloverStrategy>
<Delete basePath="log/logs" maxDepth="2">
<IfLastModified age="60d" />
</Delete>
</DefaultRolloverStrategy>
With this configuration, logs older than 60 days will be auto-deleted.
Refer to the log4j2 docs for more configuration information.
You can add the DefaultRolloverStrategy to your xml as -
<RollingFile name="General" fileName="log/logs/giornale.log" filePattern="log/logs/log-%d{yyyyMMdd}.log">
<PatternLayout>
<Pattern>%d{HH:mm:ss,SSS} [%t] %-5level %logger{-1} - %msg%n</Pattern>
</PatternLayout>
<Policies>
<TimeBasedTriggeringPolicy />
</Policies>
<DefaultRolloverStrategy max="<specify maximum archive count>"/>
</RollingFile>
As well you can use the combination of time and size based roll over for logs using -
<Policies>
<TimeBasedTriggeringPolicy interval="6" modulate="true"/>
<SizeBasedTriggeringPolicy size="250 MB"/>
</Policies>
..a sample configuration that uses a RollingFileAppender with both the
time and size based triggering policies, will create up to 7 archives
on the same day (1-7) that are stored in a directory based on the
current year and month, and will compress each archive using gzip and
will roll every 6 hours when the hour is divisible by 6

log4j2 RollingFile Error renaming file

I have this appender
<RollingFile name="Application" fileName="/home/user/log/abc.log" filePattern="/var/log/mylog/abc-%d{yyy-MM-dd}-%i.log.gz">
<PatternLayout>
<Pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} abc %-10level %class{36} %L %M - %msg%xEx%n</Pattern>
</PatternLayout>
<Policies>
<TimeBasedTriggeringPolicy />
<SizeBasedTriggeringPolicy size="5 MB" />
</Policies>
<DefaultRolloverStrategy max="9999"/>
</RollingFile>
when the 5 MB are reached and there is no space on /var/log/mylog I get the error "ERROR renaming file from xx to yy If I make space afterwards, the logger does not try again to rename the file. Any ideas how I could solve that?
I use log4j2 2.2
This is not an answer. I don't have enough reputation to make a comment.
SUGGESTION: Log4j2 allows you to automatically delete files older then X days. I've been struggling with this some time ago. The issue has been resolved here:
Log4j2 - Configure RolloverStrategy to delete old log files
Maybe this will be helpful for you.
If you configure a FailoverAppender and put your RollingFileAppender within it then you can configure the FailoverAppender to retry at an interval you configure. In the meantime, the log events can be routed to a secondary appender that can do something else. See http://logging.apache.org/log4j/2.x/manual/appenders.html#FailoverAppender.
try removing ":" here {yyyy-MM-dd HH:mm:ss.SSS}
something like below
<PatternLayout>
<Pattern>%d{yyyy-MM-dd} abc %-10level %class{36} %L %M - %msg%xEx%n</Pattern>
</PatternLayout>

RollingFileAppender not clearing log file in log4j2 version 2.5

We are using log4j2 on Window 7 Enterprise.
JBoss Developer Studio 8(this really doesn't matter)
RollingFileAppender rolls over the log files properly, however the original log file keep old logs and continues to increase in size. There is a JIRA bug (LOG4J2-904) related to this . I followed some of the options like
fileIndex="min" or fileIndex="max"
Using RollingRandomAccessFile instead of RollingFile.
Some comments indicated problem fixed as of log4j 2.4.1 version. However, I am still seeing the same problem in log4j 2.5.
I tried logging to both RollingFile and RollingRandomAccessFile appenders at the same time. An image of the log folder in included Here is the image of Log folder from rollover
Here is my log4j2.xml
<Configuration>
<Properties>
<property name="appname">myapp</property>
<Property name="log-path">${server.dir}/myapp</Property>
</Properties>
<Appenders>
<RollingFile name="DATED_ROLLING_FILE" fileName="${log-path}/${myapp}.log" filePattern="${log-path}/${myapp}_%d{MM-dd-yyyy}-%i.log" maxFileSize="40 KB">
<PatternLayout>
<Pattern>%d: %-5p [%c{1}]:%L - %M %m%n</Pattern>
</PatternLayout>
<Policies>
<OnStartupTriggeringPolicy />
<TimeBasedTriggeringPolicy interval="1" modulate="true"/>
<SizeBasedTriggeringPolicy size="40 KB" />
</Policies>
<DefaultRolloverStrategy fileIndex="min" max="100" />
</RollingFile>
<RollingRandomAccessFile name="DATED_RAC_APPENDER" fileName="${log-path}/epermitsrac.log"
filePattern="${log-path}/epermitsrac_%d{MM-dd-yyyy}-%i.log" >
<PatternLayout>
<Pattern>%d: %-5p [%c{1}]:%L - %M %m%n</Pattern>
</PatternLayout>
<Policies>
<TimeBasedTriggeringPolicy interval="1" modulate="true"/>
<SizeBasedTriggeringPolicy size="40 KB" />
</Policies>
<DefaultRolloverStrategy fileIndex="min" max="100" />
</RollingRandomAccessFile>
</Appenders>
<Loggers>
<Root level="INFO">
<AppenderRef ref="DATED_ROLLING_FILE" />
<AppenderRef ref="DATED_RAC_APPENDER" />
</Root>
</Loggers>
</Configuration>
Here is the image of Log folder from rollover. As you can see it does not clear the original File.
Did anyone else experienced same problem and is there a fix for this.
I have tried with below configuration and it is working fine, except that the latest file will be with out date.
the log4j2.properties configuration
rootLogger.level = all
appenders = rolling, console
status = warn
appender.console.type = Console
appender.console.name = console
appender.console.layout.type = PatternLayout
appender.console.layout.pattern = %d [%-5p] (%F:%M:%L) %m%n
# Rolling file based on date for day wise
appender.rolling.type = RollingFile
appender.rolling.name = rolling
#if i use below file name the rollover will not happenning as expected
#appender.rolling.fileName = E:/LOGS/${date:yyyyMMdd}_test.log
#with below filename name the rollover will happen as expected
appender.rolling.fileName = E:/LOGS/test.log
appender.rolling.filePattern = E:/LOGS/%d{yyyyMMdd}_test.log
appender.rolling.layout.type = PatternLayout
appender.rolling.layout.pattern = %d [%-5p] [%F:%M:%L] %m%n
appender.rolling.policies.type = Policies
appender.rolling.policies.time.type = TimeBasedTriggeringPolicy
#use below for day wise roll over
appender.rolling.policies.time.interval = 1
appender.rolling.policies.time.modulate = true
rootLogger.appenderRefs = rolling, console
rootLogger.appenderRef.console.ref = console
rootLogger.appenderRef.rolling.ref = rolling
rootLogger.appenderRef.stdout.ref = console, rolling

Categories

Resources