How to use current date pattern in log4j2 fileName? - java

<?xml version="1.0" encoding="UTF-8"?>
<Configuration>
<Properties>
<property name="filePattern">%d{yyyy-MM-dd}</property>
</Properties>
<Appenders>
<RollingFile name="TEST"
fileName="application-${filePattern}.log"
filePattern="application-${filePattern}-rolled.log">
<Policies>
<TimeBasedTriggeringPolicy modulate="true"/>
</Policies>
</RollingFile>
</Appenders>
...
</Configuration>
I'd like to use the current date directly in the written logfile. But the result of the configuration above is application-%{yyyy-MM-dd} as filename.
Why is the date placeholder not resolved?
By the way: the renamed file on midnight is properly renamed like application-2016-03-13-rolled.log. Why does it work there, but not in the current logfile?
I'm running tomcat 8 and java 8, if that matters.

Remove the filename attribute. It worked for me. (got the solution from: https://issues.apache.org/jira/browse/LOG4J2-1859) Here is my working configuration
<RollingFile name="File" filePattern="${basePath}/api_test_execution_log_%d{yyyy-MM-dd}_%d{HH-mm-ss}_%i.log" immediateFlush="true">
<PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/>
<Policies>
<TimeBasedTriggeringPolicy interval="1" modulate="true"/>
<SizeBasedTriggeringPolicy size="32 MB" />
<OnStartupTriggeringPolicy/>
</Policies>
</RollingFile>

This one worked (whyever):
<property name="filePattern">${date:yyyy-MM-dd}</property>

I don't know why the placeholder isn't resolved but here is my working configuration:
<Appenders>
<RollingFile name="Permament"
fileName="E:/workspace/myproject/logs/ergo.log"
filePattern="E:/workspace/myproject/logs/ergo.%d{yyyy-MM-dd.HH:mm}.log"
immediateFlush="true">
<PatternLayout pattern="%d{HH:mm:ss.SSS} %5p{length=5} - %c{1} %m %ex%n"/>
<Policies>
<TimeBasedTriggeringPolicy/>
</Policies>
</RollingFile>
...

For those are using Log4j2 version 2.6.2 and below:
The fileName is indeed a file name that never changes while the
application is running. If you do not want this behavior upgrade to
the latest version of Log4j 2 and do not specify the fileName
attribute.
From https://issues.apache.org/jira/browse/LOG4J2-1859

Related

how to define linux system log root path var/logs in log4j.xml

I have a java application where the log files are written inside applicationfolder/var/log/application.log.
Instead of writing the log inside my application folder. It has to been written in linux root default log path var/log/application.log.
Could you suggest on this.
log4j.xml
<Configuration monitorInterval="60">
<Properties>
<Property name="log-path">/var/log</Property>
</Properties>
<Appenders>
<Console name="Console-Appender" target="SYSTEM_OUT">
<PatternLayout>
<pattern>
[%-5p] %d [%t] %c %X - %m%n
</pattern>>
</PatternLayout>
</Console>
<RollingFile name="RollingFile-Appender"
fileName="${log-path}/application.log"
filePattern="${log-path}/application.log.%d{yyyy-MM-dd-hh-mm}.gz">
<PatternLayout pattern="[%-5p] %d [%t] %c %X - %m%n"/>
<Policies>
<!-- <TimeBasedTriggeringPolicy/> -->
<SizeBasedTriggeringPolicy size="1 MB"/>
</Policies>
<DefaultRolloverStrategy max="10"/>
</RollingFile>
</Appenders>
</Configuration>
Please try a relative path for the log-path property:
./var/log
Have tried with //var/log. It is working as expected

ImmediateFlush not working in Log4j2

I am getting this error:
ERROR asyncRoot contains an invalid element or attribute "immediateFlush"
When I use immediateFlush attribute in appender in log4j2.xml.
<Appenders>
<Console name="STDOUT" target="SYSTEM_OUT">
<PatternLayout pattern="%d{dd-MM-yy HH:mm:ss} [$${ctx:instId} - $${ctx:userId}] %c.%M(%L)- %m%n" />
</Console>
<RollingFile name="csroot"
fileName="${cslogs}/cslog.log" filePattern="${cslogs}/$${date:yyyy-MM}/cslog-%d{yyyy-MM-dd}-%i.log">
<PatternLayout pattern="%d{dd-MM-yy HH:mm:ss} [$${ctx:instId} - $${ctx:userId}] %c.%M(%L)- %m%n" immediateFlush="false"/>
<Policies>
<SizeBasedTriggeringPolicy size="100 MB" />
</Policies>
<DefaultRolloverStrategy max="1000">
</DefaultRolloverStrategy>
</RollingFile>
</Appenders>
Try with this modification:
<RollingFile name="csroot"
fileName="${cslogs}/cslog.log" filePattern="${cslogs}/$${date:yyyy-MM}/cslog-%d{yyyy-MM-dd}-%i.log" immediateFlush="false">
<PatternLayout pattern="%d{dd-MM-yy HH:mm:ss} [$${ctx:instId} - $${ctx:userId}] %c.%M(%L)- %m%n"/>
<Policies>
<SizeBasedTriggeringPolicy size="100 MB" />
</Policies>
<DefaultRolloverStrategy max="1000">
</DefaultRolloverStrategy>
</RollingFile>
immediateFlush is not an attribute of PatternLayout.
I know its quite an old question but might help somebody.
RollingFile Appender Parameters points out the immediateFlush is the property of the appender. But you have it declared as an attribute for PatternLayout which is what is causing the error.
Sample RollingFile appender configuration:
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="warn" name="MyApp" packages="">
<Appenders>
<RollingFile name="RollingFile" fileName="logs/app.log"
filePattern="logs/$${date:yyyy-MM}/app-%d{MM-dd-yyyy}-%i.log.gz"
immediateFlush="false">
<PatternLayout>
<Pattern>%d %p %c{1.} [%t] %m%n</Pattern>
</PatternLayout>
<Policies>
<TimeBasedTriggeringPolicy/>
<SizeBasedTriggeringPolicy size="250 MB"/>
</Policies>
</RollingFile>
</Appenders>
<Loggers>
<Root level="error">
<AppenderRef ref="RollingFile"/>
</Root>
</Loggers>
</Configuration>

how to configure log4j2 webapplication

Am little new to web applications, recently I was in need to employ a logging mechanism and for that I choose Log4J2, I went through there guide, and downloaded required libraries. This is what so far I did.
1. Added following jars to web-inf/lib
-- log4j-core2.1.jar
-- log4j-api-2.1.jar
-- log4j-web-2.1.jar
2. Added below xml as, log4j2.xml in java/src directory
<?xml version="1.0" encoding="UTF-8"?>
<configuration name="NONPROD" status="OFF">
<Properties>
<Property name="log-path">logs</Property>
</Properties>
<Appenders>
<Console name="console-log" target="SYSTEM_OUT">
<!-- <pattern>[%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %c{1} - %msg%n -->
<!-- </pattern> -->
<PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n" />
</Console>
<RollingFile name="info-log" fileName="${log-path}/web-info.log"
filePattern="${log-path}/web-info-%d{yyyy-MM-dd}.log">
<PatternLayout>
<pattern>[%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %c{1} - %msg%n
</pattern>
</PatternLayout>
<Policies>
<TimeBasedTriggeringPolicy interval="1"
modulate="true" />
<OnStartupTriggeringPolicy />
<SizeBasedTriggeringPolicy size="20 MB" />
</Policies>
</RollingFile>
<RollingFile name="error-log" fileName="${log-path}/web-error.log"
filePattern="${log-path}/web-error-%d{yyyy-MM-dd}.log">
<PatternLayout>
<pattern>[%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %c{1} - %msg%n
</pattern>
</PatternLayout>
<Policies>
<TimeBasedTriggeringPolicy interval="1"
modulate="true" />
<OnStartupTriggeringPolicy />
<SizeBasedTriggeringPolicy size="20 MB" />
</Policies>
</RollingFile>
<RollingFile name="debug-log" fileName="${log-path}/web-debug.log"
filePattern="${log-path}/web-debug-%d{yyyy-MM-dd}.log">
<PatternLayout>
<pattern>[%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %c{1} - %msg%n
</pattern>
</PatternLayout>
<Policies>
<TimeBasedTriggeringPolicy interval="1"
modulate="true" />
<OnStartupTriggeringPolicy />
<SizeBasedTriggeringPolicy size="20 MB" />
</Policies>
</RollingFile>
<RollingFile name="trace-log" fileName="${log-path}/web-trace.log"
filePattern="${log-path}/web-trace-%d{yyyy-MM-dd}.log">
<PatternLayout>
<pattern>[%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %c{1} - %msg%n
</pattern>
</PatternLayout>
<Policies>
<TimeBasedTriggeringPolicy interval="1"
modulate="true" />
<OnStartupTriggeringPolicy />
<SizeBasedTriggeringPolicy size="20 MB" />
</Policies>
</RollingFile>
</Appenders>
<Loggers>
<Logger name="com.demo.web.log4j2.file" level="debug"
additivity="false">
<appender-ref ref="trace-log" level="trace" />
<appender-ref ref="error-log" level="error" />
<appender-ref ref="debug-log" level="debug" />
<appender-ref ref="info-log" level="info" />
</Logger>
<Logger name="com.demo.web.log4j2.console" level="all"
additivity="false">
<AppenderRef ref="console-log" />
</Logger>
<Root level="info" additivity="true">
<AppenderRef ref="console-log" />
</Root>
</Loggers>
</configuration>
3. third and last i wrote an small web service to test logging
#Path("/register")
public class Register {
private DataManager mManager;
private static final Logger LOG = LogManager.getLogger(Register.class);
public Register(){
mManager = DataManager.getInstance();
}
#GET
#Path("/{param}")
public Response getMsg(#PathParam("param") String msg) {
LOG.error("Not supported operations");
return Response.status(Response.Status.BAD_REQUEST)
.entity("Not supported").build();
}
But in console, upon trigger get request this is all i got printed.
INFO: Server startup in 35959 ms
Not supported operations // this is not in pattern i supplied in log4j2.xml
// this is my pattern
<PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n" />
I figure out this is probably due to some thing gone wrong, and am struggling to find the actual cause, being totally new to web programming, please help me out to configure my logger.
Thanks,
Techfist
Your servlet container is using some other logging framework which logs to console. Figure out which logging framework that is and use one of log4j2's bridge libraries to redirect output to your log4j2 setup.
Alright, I found the cause behind this, Issue was primarily happening due to two reasons.
First I was suppose to exclude log4j* pattern from jarsToSkip attribute in catilina property
Second, i had kept two log4j2.xml one inside web-inf other inside java/src, it was supposed to be only present at java/src. two files were not required.
Third, but not mandatory, before deployment just check if log4j2.xml is included inside web-inf/classes or not, if not then simply add it.
that's it, after following this issue was resolved now am getting proper logs.

Time based triggering policy in log4j2

I am trying to create new log files on an hourly basis. I am using TimeBasedTriggerringPolicy of lo4j2 in RollingFileAppender. Below is the sample xml configuration code i have taken from log4j2 official site.
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="warn" name="MyApp" packages="">
<Appenders>
<RollingFile name="RollingFile" fileName="logs/app.log" filePattern="logs/$${date:yyyy-MM}/app-%d{yyyy-MM-dd-HH}-%i.log.gz">
<PatternLayout>
<Pattern>%d %p %c{1.} [%t] %m%n</Pattern>
</PatternLayout>
<Policies>
**
<TimeBasedTriggeringPolicy interval="1" modulate="true" />
**
<SizeBasedTriggeringPolicy size="250 MB" />
</Policies>
</RollingFile>
</Appenders>
<Loggers>
<Root level="error">
<AppenderRef ref="RollingFile" />
</Root>
</Loggers>
</Configuration>
In the interval attribute I have set 1 which signifies 1 hour.
But still my file does not roll every 1 hour.
Please help me find any mistake.
Note : I have included beta9 of log4j2 (which is the latest)
1 here indicates 1 day and not 1 hour. I have manually tested with below configuration.
<RollingFile name="T" fileName="/data_test/log/abc.log"
filePattern="/data_test/log/abc-%d{MM-dd-yyyy}-%i.log">
<PatternLayout>
<Pattern>%d{ISO8601} %-5p [%t] (%F:%L) - %m%n</Pattern>
</PatternLayout>
<Policies>
<TimeBasedTriggeringPolicy interval="1" modulate="true"/>
<SizeBasedTriggeringPolicy size="100 KB" />
</Policies>
</RollingFile>
For manual testing, I change the system date and time.
First, try with increasing 1 hour. The log files will be generated but not as per the expectation.
Then change the system date, increase by 1 day and then see the results.
Suppose the last log file (abc.log) on day 29-Oct is of 50 KB. Configuration size is 100 KB. If we change the day (increase by 1 day) and then run.
Then, last file will be renamed 29-Oct-(some sequence number).log (50 KB file as it is copied) and new file will be created with abc.log
I have tried this with simple servlet with below configuration in web.xml
<context-param>
<param-name>log4jConfiguration</param-name>
<param-value>log4j2.xml</param-value>
</context-param>
keep log4j2.xml in src folder. log4j2.xml is not loaded if we keep it in classpath.
Log4j documentations:
interval -> (integer) How often a rollover should occur based on the most
specific time unit in the date pattern. For example, with a date
pattern with hours as the most specific item and and increment of 4
rollovers would occur every 4 hours. The default value is 1.
You should change the filename pattern if you would like to create it every hour.
As Abid mentioned, interval value is interpreted in context of pattern that is specified as part of filePattern. It starts with lowest denomination. For example,if pattern contains S, frequency will be in millisecond. It supports the date pattern as described in detail as part of SimpleDateFormat java doc http://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html
You do have a non-empty log file (otherwise there is nothing to roll over)?
Note that even though the name is "TimeBased..." It will not actually roll over at the specified time, but at the first log event that arrives after the time threshold has been exceeded. Can you try with a small test program that logs something after 61 minutes or so and see if the problem still occurs?
If it doesn't roll over with the above test program, you may have found a bug. In that case please raise it on the log4j issue tracker. (Be sure to attach the test program the team can use to reproduce the issue).
Everyday day Rolling
<RollingFile name="RollingFile" fileName="logs/app.log"
filePattern="logs/app.%d{yyyy-MM-dd}.log" ...>
...
<TimeBasedTriggeringPolicy interval="1" modulate="true" />
Everyday Hour Rolling
<RollingFile name="RollingFile" fileName="logs/app.log"
filePattern="logs/app.%d{yyyy-MM-dd-HH}.log" ...>
...
<TimeBasedTriggeringPolicy interval="1" modulate="true" />
Everyday 5 day Rolling
<RollingFile name="RollingFile" fileName="logs/app.log"
filePattern="logs/app.%d{yyyy-MM-dd}.log" ...>
...
<TimeBasedTriggeringPolicy interval="5" modulate="true" />
Everyday 5 hours Rolling
<RollingFile name="RollingFile" fileName="logs/app.log"
filePattern="logs/app.%d{yyyy-MM-dd-HH}.log" ...>
...
<TimeBasedTriggeringPolicy interval="5" modulate="true" />
Every Month Rolling
<RollingFile name="RollingFile" fileName="logs/app.log"
filePattern="logs/app.%d{yyyy-MM}.log" ...>
...
<TimeBasedTriggeringPolicy interval="1" modulate="true" />
Hope these cases will helps very well in understanding how filePattern and interval are related.
The time interval interpretation depends on file pattern you are using. The following configuration rolls file every second for me.
<?xml version="1.0" encoding="UTF-8"?>
<Configuration>
<Appenders>
<Console name="A" target="SYSTEM_OUT">
<PatternLayout pattern="%d [%t] %-5p {%F:%L} %x - %m%n" />
<ThresholdFilter level="INFO" onMatch="ACCEPT" onMismatch="DENY"/>
</Console>
<RollingFile name="R"
fileName="/home/xxx/yyy/myapp.log" filePattern="/home/xxx/yyy/myapp-%d{yyyy-MM-dd-HH-mm-ss}-%i.log">
<PatternLayout pattern="%d [%t] %-5p {%F:%L} %x - %m%n" />
<Policies>
<TimeBasedTriggeringPolicy interval="1" />
</Policies>
</RollingFile>
</Appenders>
<Loggers>
<Root level="INFO">
<AppenderRef ref="A" />
<AppenderRef ref="R" />
</Root>
</Loggers>
</Configuration>
According to your TimeBasedTriggeringPolicy configuration, logger will only populate the logs every day not every hour.
AFAIK,You can achieve the functionality by changing the filePattern from HH(Hours) to dd(Days).
I have modified your config.xml. Try This
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="warn" name="MyApp" packages="">
<Appenders>
<RollingFile name="RollingFile" fileName="logs/app.log" filePattern="logs/$${date:yyyy-MM}/app-%d{yyyy-MM-dd}-%i.log.gz">
<PatternLayout>
<Pattern>%d %p %c{1.} [%t] %m%n</Pattern>
</PatternLayout>
<Policies>
**
<TimeBasedTriggeringPolicy interval="1" modulate="true" />
**
<SizeBasedTriggeringPolicy size="250 MB" />
</Policies>
</RollingFile>
</Appenders>
<Loggers>
<Root level="error">
<AppenderRef ref="RollingFile" />
</Root>
</Loggers>
</Configuration>
For more details check this
Hope this will workout for you too.

How to add/subtract date in log4j 2?

i have following configuration file in log4j 2
<RollingFile name="RollingFile" fileName="logs/test.log"
filePattern="logs$${date:yyyyMM}/app-%d{MM-dd-yyyy}.log">
<PatternLayout pattern="%d %-5p [%t] %C{4} (%F:%L) - %m%n"/>
<Policies>
<OnStartupTriggeringPolicy />
<SizeBasedTriggeringPolicy size="20 MB"/>
</Policies>
<DefaultRolloverStrategy fileIndex="max" max="100"/>
</RollingFile>
can I perform date addition and/or subtraction in the filePattern. ? as of now the files generated upon rollover has current date. I would like to subtract single day from it . how can it be done ?
This is currently not possible, but I believe someone requested something similar earlier. I believe filing your request on the Log4j-2.0 issue tracker ( https://issues.apache.org/jira/browse/LOG4J2 ) may be the best way to proceed.

Categories