Log4j2 automatic reconfiguration when file size exceeds - log4j2

In log4j2, I wanted to reconfigure automatically when file size exceeds. I have seen the attribute monitorinterval but it is defined for time based triggering policy. But I am looking for size based triggering policy.

I am going to assume that you want to know how to roll over a file when its size exceeds some threshold since reconfiguring automatically when a file exceeds some size is not something I have ever been asked before.
To roll over the file you would use the RollingFileAppender with the SizeBasedTriggeringPolicy. The configuration below will roll over the file once it hits 250 MB.
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="warn" name="MyApp" packages="">
<Appenders>
<RollingFile name="RollingFile" fileName="logs/app.log"
filePattern="logs/$${date:yyyy-MM}/app-%i.log">
<PatternLayout>
<Pattern>%d %p %c{1.} [%t] %m%n</Pattern>
</PatternLayout>
<Policies>
<SizeBasedTriggeringPolicy size="250 MB"/>
</Policies>
</RollingFile>
</Appenders>
<Loggers>
<Root level="error">
<AppenderRef ref="RollingFile"/>
</Root>
</Loggers>
</Configuration>

Related

Names of log archives generated using Log4j2

Here i am trying to implement Log4j2 archival strategy using DefaultRollOverStrategy. Basically i want to have number of archive files being generated once size of actual file crosses 10Mb.
With current programmatic configuration i am able to do so, but problem here is archived file name is not reflecting accurate time part.
For example, over the period log archives are generated but time part(highlighted) is not changing
Sample-2019-12-02_17-09-37-1.log
Sample-2019-12-02_17-09-37-2.log
Sample-2019-12-02_17-09-37-3.log
Sample-2019-12-02_17-09-37-4.log
I am sharing XML representation of configuration generated grammatically, rather than XML based config.
<Configuration name="RollingBuilder" status="DEBUG">
<Appenders>
<RollingFile name="XYZRollingFileAppender" fileName="logs/Sample.log" filePattern="logs/Sample-${date:yyyy-MM-dd_HH-mm-ss}-%i.log" createOnDemand="false">
<PatternLayout pattern="%d{ISO8601} %5p %c{1} - %m%n"/>
<Policies>
<CronTriggeringPolicy schedule="0 0 0 * * ?"/>
<SizeBasedTriggeringPolicy size="10MB"/>
</Policies>
<DefaultRolloverStrategy max="100"/>
</RollingFile>
</Appenders>
<Loggers>
<Root level="ERROR">
<AppenderRef ref="XYZRollingFileAppender"/>
</Root>
<Logger name="com.sample.test" level="ERROR">
<AppenderRef ref="XYZRollingFileAppender"/>
</Logger>
</Loggers>
</Configuration>```

log4j2 unlimited RollingFile

I'm using log4j2 and trying to log with log-rotation. Specifically, I want to log at the maximum size of 10MB and rotate unlimitedly. The configuration below generates 3 generations of rolling files because "DefaultRolloverStrategy max" is set to 3. Could you please, guide me how to log unlimited number of files with at the maximum size of 10MB?
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="INFO">
<Properties>
<Property name="format1">%m%n</Property>
<Property name="logfile">${sys:logDirectory}/log.log</Property>
<Property name="logfile-archive">${sys:logDirectory}/log_%d{yyyy-MM-dd}.%i.log
</Property>
</Properties>
<Appenders>
<RollingFile name="logfile001" append="true" fileName="${logfile}"
filePattern="${logfile-archive}">
<PatternLayout>
<pattern>${format1}</pattern>
</PatternLayout>
<Policies>
<SizeBasedTriggeringPolicy size="10MB" />
</Policies>
<DefaultRolloverStrategy max="3" />
</RollingFile>
</Appenders>
<Loggers>
<Root level="trace">
<AppenderRef ref="logfile001" />
</Root>
</Loggers>
</Configuration>
Set an extreme value to DefaultRolloverStrategy max. E.g.
<DefaultRolloverStrategy max="1000000000" />
Update:
According to Log4j2 documentation, as of release 2.8, it can be done by setting fileIndex attribute to nomax. E.g.
<DefaultRolloverStrategy fileIndex="nomax" />

Can I keep rolling file in log4j2 even though the log is empty

I have a program in java to write log using log4j2 that configured in xml configuration file.
Can I keep rolling file in log4j2 even though the log is empty. I need to use the archived log generated by log4j2. Now the archived log is only created if the log is not empty, if there is no log then the archived is not created. But I want log4j2 to keep generated the log even there is empty log. Any advice?
Here is my code:
<?xml version="1.0" encoding="UTF-8"?>
<Appenders>
<RollingFile
fileName="logs/app-%d{yyyyMMdd}.log"
filePattern="logs/$${date:yyyy-MM-dd}/app-%d{yyyyMMdd}-%i.log.gz"
name="app_file">
<PatternLayout>
<Pattern>%d %p %m %ex%n
</Pattern>
</PatternLayout>
<Policies>
<OnStartupTriggeringPolicy />
<TimeBasedTriggeringPolicy />
<SizeBasedTriggeringPolicy
size="100 MB" />
</Policies>
<DefaultRolloverStrategy max="20" />
</RollingFile>
<Console name="stdout" target="SYSTEM_OUT">
<PatternLayout pattern="%d{HH:mm:ss.SSS} %highlight{%class{1.}:%L} [%c] [%p] %m%n" />
</Console>
</Appenders>
<Loggers>
<Logger additivity="false" includeLocation="true"
name="app">
<AppenderRef ref="app_file" level="trace"/>
<AppenderRef ref="stdout" level="trace"/>
</Logger>
<Logger level="error" name="com.ulisesbocchio.jasyptspringboot">
</Logger>
<Logger level="error" name="org.springframework">
</Logger>
<Root includeLocation="true">
<AppenderRef ref="stdout" />
</Root>
</Loggers>
I also added some code below in my java code
System.setProperty("Log4jContextSelector",
"org.apache.logging.log4j.core.async.AsyncLoggerContextSelector");
Seems that the file only roll over when something is written into the file. I think that basically you should do some customization, here are 2 options:
Set a timer, log empty string periodic to trigger the roll over.
Override the RollingFile and roll over file whatever you want.

Log4j2 RollingFile appenders clashing

Log4j2 RollingFile appenders clashing
Below is a simplified debug version of our log4j2 configuration file (We rollover nightly not every minute!).This configuration, instead of it creating a Rollover file each minute (as per theTimeBasedTriggeringPolicy) will create one rollover file (non-tarred), containing JSON formatted logging, which will be overwritten every 20KB(although it will end up being slightly greater than 20KB (See Screenshot).We also get the following errors (abbreviated with "..."):-
2016-10-07 08:47:34,433 default-workqueue-4 ERROR Unable to copy file /.../logs/logFile-2016-10-07-08:47:11.log to /.../logs/logFile-2016-10-07-08:47:11.log: java.nio.file.NoSuchFileException /.../logs/logFile-2016-10-07-08:47:11.log
If we switch the order of the timeBasedRollingFileJsonLayout appender and the sizeBasedRollingFilePatternLayoutWithZippedArchive appender then no rollover occurs at all.
If we remove the sizeBasedRollingFilePatternLayoutWithZippedArchive appender then the timeBasedRollingFileJsonLayout appender works as expected.
We have the two different appenders for different environments, where the logs may or may not be hooked up to ELK.In our real log4j2 config file we use properties to select the appropriate appender for the environment.I have removed the properties from this file for clarity and to rule them out as a possible cause of the problem.
We are using log4j 2.6.2.
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="WARN">
<Appenders>
<Console name="Console" target="SYSTEM_OUT">
<PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %logger{36} - %msg %n" />
</Console>
<RollingFile name="timeBasedRollingFileJsonLayout" append="true" fileName="logs/logFile.log" filePattern="logs/logFile-%d{yyyy-MM-dd-HH:mm}.log">
<JSONLayout properties="true" compact="true" eventEol="true" />
<Policies>
<TimeBasedTriggeringPolicy />
</Policies>
</RollingFile>
<RollingFile name="sizeBasedRollingFilePatternLayoutWithZippedArchive" append="true" fileName="logs/logFile.log" filePattern="logs/logFile-%d{yyyy-MM-dd}-%i.log.gz">
<PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %logger{36} - %msg %n" />
<Policies>
<SizeBasedTriggeringPolicy size="20KB" />
</Policies>
</RollingFile>
</Appenders>
<Loggers>
<logger name="logger.one" level="info" additivity="false">
<AppenderRef ref="timeBasedRollingFileJsonLayout" />
</logger>
<Logger name="logger.two" level="info" additivity="false">
<AppenderRef ref="timeBasedRollingFileJsonLayout" />
</Logger>
<Logger name="logger.three" level="info" additivity="false">
<AppenderRef ref="timeBasedRollingFileJsonLayout" />
</Logger>
<Root level="info">
<AppenderRef ref="timeBasedRollingFileJsonLayout" level="all" />
</Root>
</Loggers>
</Configuration>
I am at a loss to understand why you think this should work. You have two appenders trying to write to the same file trying to rollover based on different criteria and rollover to files with different names. It is no surprise that you are getting the file is in use error since two things have it open at once.

log4j2 - rolling file not created

I just moved to log4j2. It works totally OK, but rolling file is not created. I've searched around, but haven't found any clues ( there are some similar topics on stackoverflow, but they don't seem to help me ).
Here is my configuration:
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="warn">
<Appenders>
<RollingFile name="RollingFile" fileName="logging-file.log" filePattern="logging-file-%d{yyyy-MM-dd}-%i.log">
<PatternLayout>
<pattern>%d{ISO8601} %X %5p %c{1}: - %m%n</pattern>
</PatternLayout>
<Policies>
<SizeBasedTriggeringPolicy size="100 MB" />
</Policies>
<DefaultRolloverStrategy max="10"/>
</RollingFile>
<Console name="Stdout" target="SYSTEM_OUT">
<PatternLayout pattern="%d{ISO8601} %X %5p %c{1}: - %m%n"/>
</Console>
</Appenders>
<Loggers>
<Root level="info">
<AppenderRef ref="Stdout"/>
<AppenderRef ref="RollingFile"/>
</Root>
</Loggers>
</Configuration>
Any help would be highly appreciated!
Thank you in advance!
Your filePattern contains a %d (date conversation pattern) and a %i (index within the specified date pattern, in your case day). However, you only specified a SizeBasedTriggeringPolicy in the rollover Policies section. You also need to add a TimeBasedTriggeringPolicy. That way you will get the desired combination of time based and size based rollover.
For details see https://logging.apache.org/log4j/2.x/manual/appenders.html#RollingFileAppender

Resources