limit the number of log files using log4j2 RollingFileAppender - log4j

I'm trying to limit the number of log files i maintain, using MaxBackupIndex but fail to achieve that. this is my log4j2.xml, i expected to log to a different log file every second but to rotate only between 2 files and not create more than that (or delete older ones):
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="warn" name="OGBackend" packages="">
<Properties>
<Property name="log-path">C:/logs/</Property>
</Properties>
<Parameters>
<param name="MaxBackupIndex" value="2"/>
</Parameters>
<Appenders>
<RollingFile name="RollingFile" fileName="${log-path}/myexample.log"
filePattern="${log-path}/myexample-%d{yyyy-MM-dd-HH-mm-ss}-%i.log">
<PatternLayout>
<pattern>%d{dd/MMM/yyyy HH:mm:ss}- %c{1}: %m%n</pattern>
</PatternLayout>
<Policies>
<TimeBasedTriggeringPolicy/>
</Policies>
<DefaultRolloverStrategy max="2"/>
</RollingFile>
</Appenders>
<Loggers>
<Logger name="root" level="debug" additivity="false">
<appender-ref ref="RollingFile" level="debug"/>
</Logger>
<Root level="debug" additivity="false">
<AppenderRef ref="RollingFile"/>
</Root>
</Loggers>
</Configuration>
any advice?

At the moment there is no official support for that sadly. What the max backup index(used with the %i lookup in file pattern) does is prevent more than 2 files per second rather than 2 files total.
https://issues.apache.org/jira/browse/LOG4J2-524
A link to a feature request about the same issue.

Related

log4j2 does not roll application logs written without log4j

My log4j2.xml does not delete old logs.
My application writes logs to {sys:LOG_PATH}/onixs/fix/ without log4j (sys:LOG_PATH is a environment variable).
<?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] %-5level %logger{36} - %msg%n"/>
</Console>
<RollingFile name="onixs"
fileName="${sys:LOG_PATH}/onixs/engine/engine_log.txt"
filePattern="${sys:LOG_PATH}/onixs/archive/engine/engine_log.%d{yyyy-MM-dd-HH-mm-ss}.txt"
append="true"
immediateFlush="false">
<PatternLayout>
<Pattern>%d{HH:mm:ss:SSS}|%-5.5level|%-20.20thread|%-30.30logger{30}|%msg%n
</Pattern>
</PatternLayout>
<Policies>
<OnStartupTriggeringPolicy/>
</Policies>
<DefaultRolloverStrategy fileIndex="nomax">
<Delete basePath="${sys:LOG_PATH}/onixs/archive/">
<IfFileName glob="engine_log.*.txt"/>
<IfLastModified age="1d"/>
</Delete>
<Delete basePath="${sys:LOG_PATH}/onixs/fix/">
<IfAny>
<IfFileName glob="*.R*.summary"/>
<IfFileName glob="*.state"/>
</IfAny>
<IfLastModified age="1d"/>
</Delete>
</DefaultRolloverStrategy>
</RollingFile>
</Appenders>
<Loggers>
<logger name="biz.onixs" level="info" additivity="false">
<AppenderRef ref="onixs"/>
</logger>
<Root level="debug">
<AppenderRef ref="Console"/>
</Root>
</Loggers>
I expect my log4j to roll logs in ${sys:LOG_PATH}/onixs/fix/ every day (IfLastModified age="1d"). But this isn't happening. Could you help me understand why?
Your file pattern says you want the log to roll over every second however your policy indicates you only want to rollover when the application starts. The ifLastModified age="1d" indicates that you only want to keep the previous day's files in the archive folder. It has nothing to do with how frequently rollover will occur.
If you want the file to rollover while the application is running then you need to have a triggering policy that does that. One of SizeBasedTriggeringPolicy, TimeBasedTriggeringPolicy or CronBasedTriggeringPolicy will do the trick.
I suggest you review the configuration and RollingFileAppender sections of the manual one more time.

how to create log file under project base directory using log4j2

I wanted to create log file in my maven project under project base directory.I am using log4j2 framework kept the log4j2.xml file under src/mian/resources folder. below is the file. please update what is wrong in it.
<?xml version="1.0" encoding="UTF-8"?>
<Configuration xmlns:xi="http://www.w3.org/2001/XInclude"
packages="com.viveo.rtcc.common.client.traces" status="WARN">
<Properties>
<Property name="log-path">appLogs</Property>
</Properties>
<Appenders>
<Console name="Console" target="SYSTEM_OUT">
<!-- <PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36}
- %msg%n"/> -->
<PatternLayout pattern="[ %d ] %p %t %c : %m%n" />
</Console>
<RollingFile name="rollingFileAppender"
fileName="${log-path}/reLog.log"
filePattern="logs/$${date:yyyy-MM}/app-%d{MM-dd-yyyy}-%i.log.gz">
<PatternLayout>
<Pattern>%d [%t] %p %c - %m%n</Pattern>
</PatternLayout>
<Policies>
<!-- <OnStartupTriggeringPolicy /> <TimeBasedTriggeringPolicy /> -->
<SizeBasedTriggeringPolicy size="10000 KB" />
</Policies>
<DefaultRolloverStrategy max="20" />
</RollingFile>
<MessageAreaAppender name="MessageAreaAppender" />
</Appenders>
<Loggers>
<!-- <Root level="DEBUG"> <AppenderRef ref="RollingFileAppender" /> <AppenderRef
ref="Console" />
<AppenderRef ref="MessageAreaAppender" /> </Root> -->
<Logger name="com.yourloggername" level="debug"
additivity="false">
<!-- <AppenderRef ref="ConsoleAppender" /> -->
<AppenderRef ref="rollingFileAppender" />
</Logger>
<Root level="info">
<!-- <AppenderRef ref="ConsoleAppender" /> -->
<AppenderRef ref="rollingFileAppender" />
</Root>
</Loggers>
</Configuration>
Here is what I see wrong in your configuration:
Maven builds run with the working directory set to the root directory of the project. Any files creates will be respective of that. In your configuration that means it would create the logs in a directory named appLogs. This is a poor practice in Maven builds. You should change it to target/applogs.
Your filePattern contains %d{dMM-dd-yy} but none of your triggering policies are time based. Without a time-based trigger I would expect that time to never changed while the application is running.

Cassandra Appender - How to Log ONLY error messages?

I am using Cassandra appender, console and file appenders in my application. I want to store only ERROR messages in Cassandra table Logs whereas store INFO messages to Console and file.
Below is the log4j2.xml file.
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="INFO">
<Properties>
<Property name="LOG_PATTERN">%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n"</Property>
<Property name="APP_LOG_ROOT">./log</Property>
<Property name="MINLEVEL">INFO</Property>
<Property name="FILEMAXSIZE">800MB</Property>
<Property name="MAXFILES">5</Property>x
</Properties>
<Appenders>
<Console name="console" target="SYSTEM_OUT" follow="true">
<PatternLayout pattern="${LOG_PATTERN}"/>
</Console>
<RollingFile name="ROLLING" fileName="${APP_LOG_ROOT}/abc.log"
filePattern="${APP_LOG_ROOT}/abc-%d{yyyy-MM-dd}-%i.log">
<PatternLayout pattern="${LOG_PATTERN}"/>
<Policies>
<SizeBasedTriggeringPolicy size="${FILEMAXSIZE}" />
</Policies>
<DefaultRolloverStrategy max="${MAXFILES}"/>
</RollingFile>
<Cassandra name="Cassandra" clusterName="TestCluster" keyspace="Employee" table="Logs" bufferSize="10" username="tomcat" password="password">
<SocketAddress host="IPaddress" port="9042"/>
<ColumnMapping name="userid" pattern="%uuid{TIME}" type="java.util.UUID"/>
<ColumnMapping name="logger" pattern="%logger"/>
<ColumnMapping name="message" pattern="%message"/>
<ColumnMapping name="level" pattern="%level"/>
<ColumnMapping name="timestamp" literal="now()"/>
</Cassandra>
</Appenders>
<Loggers>
<Root level="${MINLEVEL}" additivity="false">
<AppenderRef ref="ROLLING"/>
</Root>
<Logger name="abc.xyz" level="ERROR" additivity="false">
<AppenderRef ref="Cassandra" />
<AppenderRef ref="console" />
</Logger>
</Loggers>
In the last segment when I change the level to ${MINLEVEL} i.e INFO, then it works but ERROR is not working. Please help!
The issue is that additivity is set to false, this is preventing to duplicate the messages to the root and abc.xyz loggers. If you set additivity to false, it will be sent to both loggers.
More information is available in this blog entry.

log4j2 custom layout not working with Rolling appender file

I'm working with log4j2 and rolling appender file.
I want to use a customize layout but it's not working properly.
I'm working with JBoss. I have put the lib with the cusotmize layout in the libs directory, so it's in the class path.
In the log4j2.xml, I have put the following configuration:
<Configuration status="trace" packages="mypackage.audit">
...
<Routing name="RoutingAppender">
<Routes pattern="$${ctx:FlowName">
<Route>
<RollingFile name="Rolling-${ctx:FlowName}" fileName="logs/Audit-${ctx:FlowName}.log"
filePattern="./logs/Audit-${ctx:FlowName}-%d{yyyy-MM-dd}-%i.log.gz" >
<PatternLayout>
<pattern>%d{ISO8601} [%t] %p %c{3} - %m%n</pattern>
</PatternLayout>
<Policies>
<TimeBasedTriggeringPolicy interval="6" modulate="true" />
</Policies>
<Layout type="AuditLayout" locationInfo="true"/>
</RollingFile>
</Route>
</Routes>
</Routing>
</Appenders>
<Loggers>
<Logger name="CustomizeAuditing" level="info" >
<AppenderRef ref="RoutingAppender"/>
</Logger>
</Loggers>
</Configuration>
But nothing is logged correctly!
Thanks in advance
Jamila
following worked for me:
log4j2 version 2.2
and
<?xml version="1.0" encoding="UTF-8"?>
<Configuration packages="com.redknee.bssauto.helpers">
<Appenders>
<RollingFile name="Rolling-default" fileName="logs/bssauto.html"
filePattern="logs/$${date:yyyy-MM}/bssauto-%d{MM-dd-yyyy}-%i.log.gz">
<CustomHTMLLayout charset="UTF-8" title="BSSAuto Logs" locationInfo="true" />
<Policies>
<TimeBasedTriggeringPolicy />
<SizeBasedTriggeringPolicy size="10 MB" />
</Policies>
</RollingFile>
</Appenders>
<Loggers>
<Root level="trace">
<AppenderRef ref="Rolling-default"/>
</Root>
</Loggers>
</Configuration>
Notice following
<Configuration packages="com.redknee.bssauto.helpers">
here packages should have all packages containing custom class for layouts
<CustomHTMLLayout charset="UTF-8" title="BSSAuto Logs" locationInfo="true" />
and CustomHTMLLayout is custom class created by extending AbstractStringLayout

log4j logging to console but not File blazeDS

I am seeing strange behavior using log4j that I am completely stumped on. This is my first time using it with BlazeDS and I am hoping it is a small issue I have missed.
In this web app, I am using log4j for logging from 2 java classes, as well as blazeDS. I am hooking BlazeDS into commons-logging via org.springframework.flex.core.CommonsLoggingTarget, and then using log4j as the underlying logger.
I have set appenders for both "Console" and "MyFile" under the blazeds logger, and while the console output is working as expected, nothing is written to MyFile. The file is in fact created, but stays at 0KB. On the other hand, my two java classes are logging just fine both to the console and their rolling file. I am seeing this behavior both when I test locally and also when I deploy to Tomcat.
One last detail - when testing locally, I changed the MyFile path to an absolute path, and saw that the file was beign appended to. This would lead me to believe it is an issue with the relative path however it has been working fine for my java logging.
commons-logging.properties:
org.apache.commons.logging.LogFactory=org.apache.commons.logging.impl.LogFactoryImpl
org.apache.commons.logging.Log=org.apache.commons.logging.impl.Log4JLogger
log4j.xml:
<?xml version="1.0" encoding="UTF-8"?>
<configuration status="trace" debug="true">
<appenders>
<Console name="Console" target="SYSTEM_OUT">
<PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/>
</Console>
<RollingFile name="RollingFile" fileName="logs/ImpactTradeQuery.log"
filePattern="logs/$${date:yyyy-MM}/app-%d{MM-dd-yyyy}-%i.log.gz">
<PatternLayout>
<pattern>%d %p %C{1.} [%t] %m%n</pattern>
</PatternLayout>
<Policies>
<TimeBasedTriggeringPolicy />
<SizeBasedTriggeringPolicy size="25 MB"/>
</Policies>
<DefaultRolloverStrategy max="25"/>
</RollingFile>
<File name="MyFile" immediateFlush="true" fileName="logs/app.log">
<PatternLayout>
<pattern>%d %p %C{1.} [%t] %m%n</pattern>
</PatternLayout>
</File>
</appenders>
<loggers>
<root level="trace">
<appender-ref ref="Console"/>
</root>
<logger name="flex.samples.trade.tradeService" level="trace" additivity="False">
<appender-ref ref="RollingFile"/>
<appender-ref ref="Console"/>
</logger>
<logger name="flex.samples.ConnectionHelper" level="trace" additivity="False">
<appender-ref ref="RollingFile"/>
<appender-ref ref="Console"/>
</logger>
<logger name="blazeds" level="trace" additivity="True">
<appender-ref ref="MyFile"/>
<appender-ref ref="Console"/>
</logger>
</loggers>
</configuration>
services-config.xml:
<logging>
<target class="flex.messaging.log.ConsoleTarget" level="Error">
<properties>
<prefix>[BlazeDS] </prefix>
<includeDate>false</includeDate>
<includeTime>false</includeTime>
<includeLevel>false</includeLevel>
<includeCategory>false</includeCategory>
</properties>
<filters>
<pattern>Endpoint.*</pattern>
<pattern>Service.*</pattern>
<pattern>Configuration</pattern>
</filters>
</target>
<target class="org.springframework.flex.core.CommonsLoggingTarget" level="All">
<properties>
<categoryPrefix>blazeds</categoryPrefix>
</properties>
<filters>
<pattern>Endpoint.*</pattern>
<pattern>Service.*</pattern>
<pattern>Configuration</pattern>
</filters>
</target>
</logging>
Turned out to be a missing jar file in Tomcat (log4j-jcl) that was causing the issue. Once the library was properly packaged with the app the logging worked as expected.

Resources