log4j2 (2.6.2) configuration not deleting old log files - log4j2

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?

Related

Log File is not creating for RegexFilter in Log4j2.xml

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>

log4j2 environment variable substitution not working for filename attribute

I am trying to configure my daily rolling log file with a filename that contains server port number because this is a multi server application and each server will generate it's own log.
I have the following configuration
<Configuration status="WARN">
<Appenders>
<Console name="Console" target="SYSTEM_OUT">
<PatternLayout
pattern="[%d{ISO8601}:$${env:SERVER_PORT}-L1] [th:%t] [%c] [%level] [%logger{36}] - %n[%d{ISO8601}:$${env:SERVER_PORT}-L2]%msg%n"/>
</Console>
<RollingFile name="dailyLog" immediateFlush="false" append="true">
<FileName>logs/rCOI.${env:SERVER_PORT:8090}.log</FileName>
<FilePattern>logs/rCOI.${env:SERVER_PORT:8090}.%d{yyyy-MM-dd}.log.zip</FilePattern>
<PatternLayout
pattern="[%d{ISO8601}:$${env:SERVER_PORT}-L1] [th:%t] [%c] [%level] [%logger{36}] - %n[%d{ISO8601}:$${env:SERVER_PORT}-L2]%msg%n"/>
<Policies>
<TimeBasedTriggeringPolicy interval="1" modulate="true" />
</Policies>
<DefaultRolloverStrategy max="5" />
</RollingFile>
</Appenders>
<Loggers>
<Root level="debug">
<AppenderRef ref="dailyLog"/>
</Root>
</Loggers>
</Configuration>
This produces a log called
logs/rCOI.${env:SERVER_PORT:8090}.log
so it's not even using the default. I am sure that SERVER_PORT is defined and exported because I use it in the application with no issues.
I have tried $${env:SERVER_PORT:8090} and just ${SERVER_PORT} as well. Nothing is working.
It would be nice to be able to use an environment variable for the logs/ directory as well.

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" />

log4j2 configuration empty log files

the following log4j2 configuration always results in empty log files and everything getting written to console. I don't want anything on the console except for the errors. Could you please help me give a hint on where I am going wrong?
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="TRACE" name="MyApp">
<Appenders>
<RollingFile name="RollingFile" fileName="logs/app.log"
filePattern="logs/$${date:yyyy-MM}/app-%d{yyyy-MM-dd-HH-mm}-%i.log" immediateFlush="true">
<PatternLayout>
<Pattern>%d %p %c{1.} [%t] %m%n</Pattern>
</PatternLayout>
<Policies>
<TimeBasedTriggeringPolicy interval="60"/>
<SizeBasedTriggeringPolicy size="950 MB"/>
</Policies>
</RollingFile>
<RollingFile name="DataRollingFile" fileName="logs/data.log"
filePattern="logs/completed/data-%d{yyyy-MM-dd-HH-mm}-%i.log" immediateFlush="true">
<PatternLayout>
<Pattern>%m%n</Pattern>
</PatternLayout>
<Policies>
<TimeBasedTriggeringPolicy interval="60"/>
<SizeBasedTriggeringPolicy size="250 MB"/>
</Policies>
</RollingFile>
<!--<Console name="Console" target="SYSTEM_OUT">-->
<!--<PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/>-->
<!--</Console>-->
</Appenders>
<Loggers>
<Logger name="com.myapp.xdf.mimic" level="WARN"/>
<Logger name="org.springframework" level="ERROR"/>
<Logger name="com.myapp.xdf.mimic.adapter.DataWriter" level="WARN" additivity="false">
<AppenderRef ref="DataRollingFile"/>
</Logger>
<Root level="INFO">
<AppenderRef ref="RollingFile"/>
</Root>
</Loggers>
</Configuration>
Your configuration has the Console appender commented out so it shouldn't be possible to have any logs directed to it. Are you sure your configuration is being read? Since you have trace enabled you should see logs generated when it is processed.

Two patterns in same log file is not working in log4j2

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.

Resources