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" />
Related
I am using below config file to generate log files .
details are given below, I can see only GoCreditDL.log is generating in Unix server after deployment.
there is no error regarding Data.log.
I have tested it my local machine its working fine, but on Unix server I am getting only one logs.
Not sure Regex filter is not working in Unix Env.
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<Properties>
<Property name="LOG_PATTERN">%d{dd MMM yyyy HH:mm:ss.SSS} %5p [%t] (%F:%L) - %m%n</Property>
<Property name="FILE_PATTERN">%d{yyyy-MM-dd-HH}-%i.log</Property>
<Property name="APP_LOG_ROOT">/home/gocredit2/gocredit_DL/log</Property>
<Property name="FILE_PATTERN_PATH">logs/$${date:yyyy-MM-dd}</Property>
<Property name="INTERVAL">1</Property>
<Property name="SIZE10MB">10 MB</Property>
<Property name="SIZE20MB">20 MB</Property>
<Property name="RolloverStrategy">10</Property>
</Properties>
<Appenders>
<RollingFile name="EJBGoCreditLog" fileName="${APP_LOG_ROOT}/GoCreditDL.log"
filePattern="${FILE_PATTERN_PATH}/GoCreditDL-${FILE_PATTERN}">
<PatternLayout>
<Pattern>${LOG_PATTERN}</Pattern>
</PatternLayout>
<Policies>
<TimeBasedTriggeringPolicy interval="${INTERVAL}" modulate="true" />
<SizeBasedTriggeringPolicy size="${SIZE20MB}"/>
</Policies>
<DefaultRolloverStrategy max="${RolloverStrategy}"/>
</RollingFile>
<RollingFile name="Stored" fileName="${APP_LOG_ROOT}/Data.log"
filePattern="${FILE_PATTERN_PATH}/Data-${FILE_PATTERN}">
<PatternLayout>
<Pattern>${LOG_PATTERN} </Pattern>
</PatternLayout>
<Policies>
<TimeBasedTriggeringPolicy interval="${INTERVAL}" modulate="true" />
<SizeBasedTriggeringPolicy size="${SIZE20MB}"/>
</Policies>
<DefaultRolloverStrategy max="${RolloverStrategy}"/>
<RegexFilter regex=".*Stored.*" onMatch="ACCEPT" onMismatch="DENY"/>
</RollingFile>
</Appenders>
<Loggers>
<root level="INFO">
<appender-ref ref="Stored" level="INFO"/>
<appender-ref ref="EJBGoCreditLog" level="INFO"/>
</root>
</Loggers>
</configuration>
I have the following log4j2 xml configuration that I'm using to log TRACE and DEBUG level messages.
<Configuration status="WARN" packages="" monitorInterval="300">
<Properties>
<Property name="log-path">/logs/webapp</Property>
</Properties>
<Appenders>
<RollingFile name="TRACE" append="true" immediateFlush="true" fileName="${log-path}/trace.log" filePattern="${log-path}/trace.log.%d{MM-dd-yyyy}">
<PatternLayout>
<pattern>%d{DEFAULT}: %-20C{1} : %m%n</pattern>
</PatternLayout>
<Policies>
<TimeBasedTriggeringPolicy />
</Policies>
<DefaultRolloverStrategy>
<Delete basePath="${log-path}" maxDepth="1">
<IfFileName glob="*/trace.log.*" />
<IfLastModified age="14d" />
</Delete>
</DefaultRolloverStrategy>
</RollingFile>
<RollingFile name="DETAIL" append="true" immediateFlush="true" fileName="${log-path}/detail.log" filePattern="${log-path}/detail.log.%d{MM-dd-yyyy}">
<PatternLayout>
<pattern>%d{DEFAULT}: %-20C{1} : %m%n</pattern>
</PatternLayout>
<Policies>
<TimeBasedTriggeringPolicy />
</Policies>
<DefaultRolloverStrategy max="15"/>
</RollingFile>
</Appenders>
<Loggers>
<Root level="trace">
<AppenderRef ref="TRACE" level="trace"/>
<AppenderRef ref="DETAIL" level="debug"/>
</Root>
</Loggers>
</Configuration>
The intention for this configuration is that the trace log files 14 days and older would be deleted but this is not the case. I have much older trace log files on my system still, the oldest being trace.log.11-02-2016
I'm aware of file deletion issues in log4j2 up to version 2.5 and 2.5 introduced the Delete configuration element but I am using 2.6.2.
Has anyone come across this and been able to get it to work either via an implementation version update or a configuration change?
I have rollover strategy with 2 days, but the files don't get remove. Can you please let know if this log4j2 configuration is valid for rollover strategy?
<?xml version="1.0" encoding="UTF-8"?>
<Configuration>
<Properties>
<Property name="baseDir">/log</Property>
<Property name="fileName">filelog</Property>
</Properties>
<Appenders>
<RollingFile name="LOG" fileName="${baseDir}/${fileName}.log"
filePattern="${baseDir}/${fileName}-%d{yyyy-MM-dd}.log" append="true">
<PatternLayout pattern="%d{yyyy-MM-dd'T'HH:mm:ss.SSSXXX}|%m%n" />
<Policies>
<TimeBasedTriggeringPolicy interval="1"/>
</Policies>
<DefaultRolloverStrategy>
<Delete basePath="${baseDir}" maxDepth="1">
<IfFileName glob="${fileName}-%d{yyyy-MM-dd}.log" />
<IfLastModified age="2d" />
</Delete>
</DefaultRolloverStrategy>
</RollingFile>
</Appenders>
<Loggers>
<Root level="info">
<AppenderRef ref="LOG" />
</Root>
</Loggers>
</Configuration>
Change the line of IfFileName to
<IfFileName glob="${fileName}-*.log" />
You can also use IfAccumulatedFileCount if you like
<Delete basePath="${baseDir}">
<IfFileName glob="${fileName}-*.log">
<IfAccumulatedFileCount exceeds="2"/>
</IfFileName>
</Delete>
Actually, I think the latter is better.
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.
I'd like to configure log4j 2 with two different patterns in same appender. i.e., Whenever there is an error, a specific pattern should be present in the log file. I am not trying two different log files, but two different pattern in same log file. Whenever there is an error, I would see "MYDOMAINDOTCOM_SUPPORT_NEEDED" and this string will trigger an automatic email to support team.
I have the below configuration which prints error message in "RollingFile" appender only and "RollingFileError" appender is ignored. What am I missing ?
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="DEBUG">
<Properties>
<Property name="log-path">/documents/log</Property>
</Properties>
<Appenders>
<RollingFile name="RollingFile" fileName="${log-path}/myexample.log"
filePattern="${log-path}/$${date:yyyy-MM}/myexample-%d{yyyy-MM-dd}-%i.log"
immediateFlush="true">
<PatternLayout>
<pattern>%d{yyyy-MM-dd HH:mm:ss} [%t] %-5p %c{1}:%L - %X{packetRefId} - %msg%n</pattern>
<!-- %d{dd/MMM/yyyy HH:mm:ss,SSS}- %c{1}: %m%n -->
</PatternLayout>
<Policies>
<SizeBasedTriggeringPolicy size="100 KB" />
</Policies>
<DefaultRolloverStrategy max="4" />
</RollingFile>
<RollingFile name="RollingFileError" fileName="${log-path}/myexample.log"
filePattern="${log-path}/$${date:yyyy-MM}/myexample-%d{yyyy-MM-dd}-%i.log"
immediateFlush="true">
<param name="threshold" value="error" />
<PatternLayout>
<pattern>MYDOMAINDOTCOM_SUPPORT_NEEDED %d{yyyy-MM-dd HH:mm:ss} [%t] %-5p %c{1}:%L - %X{packetRefId} - %msg%n</pattern>
<!-- %d{dd/MMM/yyyy HH:mm:ss,SSS}- %c{1}: %m%n -->
</PatternLayout>
<Policies>
<SizeBasedTriggeringPolicy size="100 KB" />
</Policies>
<DefaultRolloverStrategy max="4" />
</RollingFile>
</Appenders>
<Loggers>
<Logger name="org.springframework.beans.factory" level="info" additivity="false">
<AppenderRef ref="Console"/>
</Logger>
<Logger name="root" level="debug" additivity="false">
<appender-ref ref="RollingFile" level="debug" />
<appender-ref ref="RollingFileError" level="error" />
</Logger>
<Root level="debug" additivity="false">
<AppenderRef ref="RollingFile" />
</Root>
</Loggers>
</Configuration>
You should never configure two appenders to write to the same file. Having two rolling file appenders that both use the same file and roll over to the same file pattern is never going to work correctly.
Also, your configuration would end up with all error messages being logged twice; once with the RollingFile appender due to its debug level, and once on the RollingFileError appender due to its error level.
Instead, you should have a single rolling file appender and use a PatternSelector to decide which pattern to use. See http://logging.apache.org/log4j/2.x/manual/layouts.html#Pattern_Selectors for documentation on pattern selectors.