Log4j 2 rolling file with wrong timestamp - java

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>

Related

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>

log4j2 RollingRandomAccessFile add one day in file name

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.

User JVM parameter in log4j2 configuration

I have an RollingFile Appender defined in my log4j2.xml.
<RollingFile name="RollingFile" fileName="/logs/app.log"
filePattern="logs/$${date:yyyy-MM}/app-%d{MM-dd-yyyy}-%i.log.gz">
<PatternLayout>
<Pattern>%d{HH:mm:ss.SSS} - %-5p - %m - [%l]%n</Pattern>
</PatternLayout>
<Policies>
<TimeBasedTriggeringPolicy />
<SizeBasedTriggeringPolicy size="10 MB" />
</Policies>
<DefaultRolloverStrategy max="20" />
</RollingFile>
What I want to do is to pass this parameter to the JVM at startup:
-Dapp_home=/home/admin/server
The documentation is pretty straight forward. And from what I understand it should work like this:
<RollingFile name="RollingFile" fileName="${jvmrunargs:app_home}/logs/app.log"
But it doesn't. I verified that it is working in general by using the absolut path.
In an other application where I use log4j (1.x) it works like this:
log4j.appender.file.File=${app_home}/logs/app.log
Take a look at the System Properties Lookup section of the documentation. If you define a variable as system property using -D like this:
-Dapp_home=/home/admin/server
use
${sys:app_home}
in your Log4j 2 configuration to access it.

Categories

Resources