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.
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'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" />
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 am using log4j2 version 2.7 with sprint-boot 1.5:
my configuration file (log4j2.xml) is:
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="WARN" monitorInterval="60">
<Properties>
<Property name="LOG_PATH">./log</Property>
<Property name="FILE_LOG_PATTERN">%date{yyyy-MM-dd'T'HH:mm:ss.SSSZ}{UTC}}, %5p, %X{component:-SERVICE}, %X{event:-EVENT}, ${PID:-???}, [%t], %40.40logger{40}, FCID=%X{FCID:-N/A}, %msg%n</Property>
<Property name="MAX_HISTORY">7</Property>
<Property name="MAX_FILE_SIZE">100MB</Property>
</Properties>
<Appenders>
<RollingFile name="FILE"
fileName="${LOG_PATH}/tests.log"
filePattern="${LOG_PATH}/tests.%d{yyyy-MM-dd}.%i.log.gz">
<PatternLayout>
<Pattern>${FILE_LOG_PATTERN}</Pattern>
</PatternLayout>
<Policies>
<OnStartupTriggeringPolicy/>
<TimeBasedTriggeringPolicy />
<SizeBasedTriggeringPolicy size="${MAX_FILE_SIZE}"/>
</Policies>
<DefaultRolloverStrategy max="${MAX_HISTORY}"/>
</RollingFile>
</Appenders>
<Loggers>
<logger name="org.springframework" level="INFO"/>
<logger name="org.springframework.security.oauth2" level="INFO"/>
<logger name="org.springframework.http" level="ERROR"/>
<Logger name="org.springframework.boot" level="info"/>
<Logger name="org.springframework.boot.context.embedded.jetty" level="info"/>
<Root level="error">
<AppenderRef ref="FILE"/>
</Root>
</Loggers>
when I start the application I get this error:
ERROR Unable to invoke factory method in class class org.apache.logging.log4j.core.appender.RollingFileAppender for element RollingFile. java.lang.IllegalStateException: No factory method found for class org.apache.logging.log4j.core.appender.RollingFileAppender
If I remove one of the policies this error not showing. What is the problem?
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.