log4j2 filePattern doesn't interpolate - log4j2

I've tried to add the following RollingFile appender;
<RollingFile name="appFile" fileName="${sys:catalina.base}${sys:file.separator}logs${sys:file.separator}${web:contextPath}${sys:file.separator}app.log" filePattern="app-%d{dd-MM-yyyy}.log">
<PatternLayout pattern="%d{dd/MM/yyyy HH:mm:ss} %c{2} - %m%n" />
<Policies>
<TimeBasedTriggeringPolicy />
<SizeBasedTriggeringPolicy size="250 MB" />
</Policies>
<DefaultRolloverStrategy max="20" />
</RollingFile>
The file is created in the correct path but the name is always the same (app.log) instead of app-xx-xx-xxxx.log.
What do I miss?

The filePattern attribute is the pattern of the file name to use on rollover. But if you want the date pattern in the name of the file that is actively written tom, you can use Date Lookup in the filename attribute, i.e:
fileName="${sys:catalina.base}${sys:file.separator}logs${sys:file.separator}${web:contextPath}${sys:file.separator}app-${date:dd-MM-yyyy}.log"

Related

How to generate AM and PM log filename using log4j2.13 version

Helllo everyone,
we have log4j from 1.2.8 to log4j2.11, and observed half daily logs files are not generating as expected.Only i can see only AM log files are generating.
<RollingFile name="file" fileName="testAMPM.log" filePattern="testAMPM.log-%d{yyyy-MM-dd-a}" >
<PatternLayout pattern="%d %5p %-120m [%t - %c:%L] %n"/>
<Policies>
<TimeBasedTriggeringPolicy interval="1" modulate="true" />
</Policies>
Will not mark this as duplicate because this, question here did not get answered. The answer to another duplicate question does have an example though, so please try this here first:
<RollingFile name="fileAppender" filename ="${logName} append="true" FilePattern="${logName}.$$d{yyyy-MM-dd-a}">
<CronTriggeringPolicy schedule="0 0 0,12 * * ?" />
</RollingFile>
A comment in the first linked duplicate also suggests to check out Cron Triggering Policy, which could provide a better solution to your requirement than a TimeBasedTriggeringPolicy:
Just try it out.

Lo4j config - Log to different files based on type of mesasge prefix

Hello I am using Log4j2 for my logging and I am little confused as to how I can log message to a different file based on the message prefix.
For example, currently all messages are logged in a single logs folder.
A set of my messages look like this:
'com.project.latency: ProjectName=[MooPointProject].......'
Some of my other log messages are of the format:
'com.project.latency: ProjectName=[DataPlaneProject].......'
I want to log the messages which contain the MooPointProject in a specific file and one containing DataPlaneProject in a separate log file.
Is there a specific way I can do that other than changing the logging level itself?
Set up several appenders one for each project name. For example:
<RollingFile name="RollingFile" fileName="logs/MooPointProject.log"
filePattern="logs/app-%d{MM-dd-yyyy}.log.gz">
<RegexFilter regex=".*MooPointProject.*" onMatch="ACCEPT" onMismatch="DENY"/>
<PatternLayout>
<pattern>%d %p %c{1.} [%t] %m%n</pattern>
</PatternLayout>
<TimeBasedTriggeringPolicy />
</RollingFile>
and
<RollingFile name="RollingFile" fileName="logs/DataPlaneProject.log"
filePattern="logs/app-%d{MM-dd-yyyy}.log.gz">
<RegexFilter regex=".*DataPlaneProject.*" onMatch="ACCEPT" onMismatch="DENY"/>
<PatternLayout>
<pattern>%d %p %c{1.} [%t] %m%n</pattern>
</PatternLayout>
<TimeBasedTriggeringPolicy />
</RollingFile>
Then assign both the appenders to your logger(s). Since your RegexpFilter accepts only a project-spcific lines you will get them to the appropriate files separately.
P.S. - Find the detail on other types of filters here.

Convert log4j 1.x to log4j 2.x custom layout appender

<appender name="FILE" class="org.apache.log4j.DailyRollingFileAppender">
<param name="File" value="${catalina.base}/logs/server.log" />
<param name="Append" value="true" />
<param name="DatePattern" value="'.'yyyy-MM-dd" />
<layout class="com.mayank.base.logging.CustomPatternLayout">
<param name="ConversionPattern" value="%d{ABSOLUTE}#%X{requestId} %R %-5p [%c{1}] - %m%n" />
</layout>
</appender>
I am facing trouble converting it to log4j 2. How i can addmy custom pattern layout.
You should implement your CustomPatternLayout as a Layout plugin. You have a code example on (layout plugins manual).
In very general you should :
Implement the plugin class with proper annotations on the constructor
Implement the plugin factory method
Implement the desired formatting
Mention in log4j configuration your plugins package:
<Configuration packages="com.mayank.base.logging">
Use in the configuration the name of your plugin class
You can see explanations and examples of plugins implementation on this blog post1, post2, post3. There is no layout plugin example, but the technique is very similar.
The <RollingFile> tag should be present.
Here are couple of samples -
<RollingFile name="ROLLING"
fileName="f:/my_dir/logsroll.log"
filePattern="f:/my_dir/logsroll-%i.log">
OR
<RollingFile name="MyFile" fileName="d:/log/bsi/admin/total/totalLog.log"
filePattern="d:/log/totalLog-%d{MM-dd-yyyy}-%i.log">
<PatternLayout>
<Pattern>%d %p %c [%t] %m%n</Pattern>
</PatternLayout>
<Policies>
<TimeBasedTriggeringPolicy />
<SizeBasedTriggeringPolicy size="1 MB"/>
</Policies>
<DefaultRolloverStrategy max="2000"/>
</RollingFile>
Referthis for more.

Log4j2 time based rolling

The following is my time and size based file rolling appender
<RollingFile name="fileWriter" fileName="${LOG_DIR}/file.log"
filePattern="${ARCHIVE}/file_log.%d{yyyy-MM-dd}-%i.gz">
<PatternLayout pattern="${PATTERN}"/>
<Policies>
<TimeBasedTriggeringPolicy interval="1" modulate="true"/>
<SizeBasedTriggeringPolicy size="100 MB" />
</Policies>
</RollingFile>
To reduce the number of log files, I would like to make it rolling over by time, so that the files generated 30 days ago is automatically removed.
Could DefaultRolloverStrategy help in my case? If it doesn't, would anyone provide some suggestions? Many Thanks.
Yes, the DefaultRolloverStrategy will do what you want. You should be able to configure:
<DefaultRolloverStrategy max="30"/>
As an alternative, you can also specify a Delete action something like:
<DefaultRolloverStrategy>
<Delete basePath="${ARCHIVE}" maxDepth="2">
<IfFileName glob="*/file_log-*.gz" />
<IfLastModified age="30d" />
</Delete>
</DefaultRolloverStrategy>

How to generate half-daily logs using log4j2?

I want to generate half-daily log files through log4j2 configuration. I have given :
<RollingFile name="fileAppender" filename ="${logName} append="true"
FilePattern="${logName}. %d{yyyy-MM-dd-a}">
And also defined:
<Policies>
<TimeBasedTriggeringPolicy interval="1"
Module="true"/>
But i am getting only AM log in this way though i want it to be something :
a.log.2016-03-23-AM
b.log.2016-03-23-PM
Can somebody help me out in the same?
You can use the CronTriggeringPolicy for this. I borrowed the expression from the answer at Cron Expression (Quartz) for a program to run every midnight at 12 am for the actual expression to use.
<RollingFile name="fileAppender" filename ="${logName} append="true" FilePattern="${logName}.$$d{yyyy-MM-dd-a}">
<CronTriggeringPolicy schedule="0 0 0,12 * * ?" />
</RollingFile>

Resources