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.
Related
Currently, my logs are generating 1 file per day.
But I want to compress the log files older than 5 days and delete them after 10 days.
how can I achieve this?
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="debug" name="MyApp" packages="">
<Appenders>
<RollingFile name="RollingFile" fileName="logs/server.log"
filePattern="logs/server_%d{date:yyyy-MM-dd}.log">
<PatternLayout>
<Pattern>%d %p %c{1.} [%t] %m%n</Pattern>
</PatternLayout>
<Policies>
<TimeBasedTriggeringPolicy interval="1" modulate="true" />
</Policies>
</RollingFile>
</Appenders>
<Loggers>
<Root level="info">
<AppenderRef ref="RollingFile"/>
</Root>
</Loggers>
</Configuration>
I am in the process of migrating my application from log4j 1.2 to log4j2-2.8.1 version.
Following is the existing 1.x configuration in log4j.properties file.
log4j.appender.JSERRORFILE=org.apache.log4j.DailyRollingFileAppender
log4j.appender.JSERRORFILE.File=${log4j.loglocation}/jserror.log
log4j.appender.JSERRORFILE.layout=org.apache.log4j.PatternLayout
log4j.appender.JSERRORFILE.layout.ConversionPattern=%d %-5p %c - %m%n
log4j.logger.com.app.JavascriptLogger=ERROR,JSERRORFILE
log4j.additivity.com.app.JavascriptLogger=false
Converted this to equivalent xml configuration log4j2.xml :
<RollingFile name="JSERRORFILE" fileName="${log-path}/jserror.log">
<PatternLayout pattern="%d %-5p %c - %m%n" />
</RollingFile>
<Logger name="com.app.JavascriptLogger" level="ERROR" additivity="false">
<AppenderRef ref="JSERRORFILE"/>
</Logger>
After conversion,I keep getting the following error :
org.apache.logging.log4j.core.config.ConfigurationException: Arguments given for element RollingFile are invalid
Any help would be appreciated.
You need to tell the RollingFile appender when to rollover (trigger policy) and what the result of a rollover should look like.
If you want to rollover at some regular interval (TimeBasedTriggeringPolicy or CronTriggeringPolicy) you need to specify a filePattern containing a SimpleDateFormat-like string. If you want to rollover to prevent large files (SizeBasedTriggeringPolicy), you need to specify a filePattern containing %i.
The filePattern is the relative or absolute path of the location you want the old (rolled over) file to be moved to.
Example:
<?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">
<PatternLayout>
<Pattern>%d %p %c{1.} [%t] %m%n</Pattern>
</PatternLayout>
<Policies>
<CronTriggeringPolicy schedule="0 0 0 * * ?"/>
<SizeBasedTriggeringPolicy size="250 MB"/>
</Policies>
</RollingFile>
</Appenders>
<Loggers>
<Root level="error">
<AppenderRef ref="RollingFile"/>
</Root>
</Loggers>
</Configuration>
The cron expression above fires once a day.
For details and more examples see the RollingFile appender section of the user manual.
<?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
My routing log4j2.xml should role log file based on user login.
Assume a user logs in the application for consecutive 3 days and do his stuff for around half an hour and then logs out. So as per requirement 3 log files should be created for the logged user (one file per day basis in separate file)
like e.g.
john-20-11-2015.log,
john-21-11-2015.log,
john-22-11-2015.log
below is the Log4j2.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE xml>
<Configuration status="WARN" name="MySite" monitorInterval="30">
<Appenders>
<Console name="Console" target="SYSTEM_OUT">
<PatternLayout>
<pattern>[%-5level] [%d{yyyy-MM-dd HH:mm:ss.SSS}] [%c{1}] - %msg%n</pattern>
</PatternLayout>
</Console>
<RollingFile name="error-log"
fileName="D:/myLogs/error [${date:yyyy-MM-dd}].log" filePattern="D:/myLogs/error-%d{yyyy-MM-dd}.log">
<PatternLayout>
<pattern>[%-5level] [%d{yyyy-MM-dd HH:mm:ss.SSS}] [%c{1}] - %msg%n</pattern>
</PatternLayout>
<Policies>
<TimeBasedTriggeringPolicy interval="1" modulate="true"/>
</Policies>
</RollingFile>
<RollingFile name="trace-log"
fileName="D:/myLogs/trace [${date:yyyy-MM-dd}].log" filePattern="D:/myLogs/trace-%d{yyyy-MM-dd}.log">
<PatternLayout>
<pattern>[%-5level] [%d{yyyy-MM-dd HH:mm:ss.SSS}] [%c{1}] - %msg%n</pattern>
</PatternLayout>
<Policies>
<TimeBasedTriggeringPolicy interval="1" modulate="true"/>
</Policies>
</RollingFile>
<Routing name="RoutingAppender">
<Routes pattern="$${ctx:logFileName}">
<Route>
<RollingFile name="Rolling-${ctx:logFileName}" append="true"
fileName="D:/myLogs/${ctx:logFileName}~${date:yyyy-MM-dd}.log"
filePattern="D:/myLogs/%d{yyyy-MM-dd}-%i.log">
<PatternLayout
pattern="SERVER-LOG: [%-5level] [%d{yyyy-MM-dd HH:mm:ss.SSS}] [%logger{1}] - %msg%n" />
<Policies>
<TimeBasedTriggeringPolicy interval="1" modulate="true" />
</Policies>
</RollingFile>
</Route>
<Route ref="Console" key="$${ctx:logFileName}" />
</Routes>
</Routing>
</Appenders>
<Loggers>
<Root level="trace" additivity="false">
<Appender-Ref ref="Console"/>
<appender-ref ref="error-log" level="error"/>
<appender-ref ref="trace-log" level="trace"/>
<Appender-Ref ref="RoutingAppender" />
</Root>
</Loggers>
</Configuration>
I am calling
ThreadContext.put("logFileName", userName);
to append the log in the routing appender, the log were appending correctly in the respective logs
and I am clearing the threadcontext during logout method
ThreadContext.remove("logFileName");
ThreadContext.clearAll();
I hosted my application in tomcat. log files were generated based on logged user for every user based on the file name pattern, but log is not generated on daily basis, instead of that its appended the current day log of users in previous day log
eg: john-20-11-2015.log contains the log of of 21st and 22nd
its roles a new log file only when tomcat is stop started .
guys help me out.. is there anything wrong
I think you want DailyRollingFileAppender here is an example
Also please look at the similar question
example:
<appender name="FILE" class="org.apache.log4j.DailyRollingFileAppender">
...
<param name="DatePattern" value="'.'yyyy-MM-dd" />
...
</appender>
The filename attribute is only evaluated when the appender is created. It does not change on each rollover. You should get a new file on each rollover that does have the correct date in it.
Is that not what you are seeing?
This is my log4j2.xml
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="OFF">
<Appenders>
<!-- Generate STDOUT in console -->
<Console name="CONSOLE" target="SYSTEM_OUT">
<PatternLayout pattern="%d %-5p [%t] %C{2} (%F:%L) - %m%n" />
</Console>
<!-- Generate rolling log for router with per hour interval policy -->
<RollingFile name="RouterRollingFile" fileName="/apps/bea/mb-logs/router.log"
immediateFlush="false" filePattern="/apps/bea/mb-logs/router.%d{yyyy-MM-dd-HH}-%i.log">
<PatternLayout>
<pattern>%d{yyyy-MM-dd HH:mm:ss} %5p [%t] (%F:%L) - %m%n</pattern>
</PatternLayout>
<Policies>
<TimeBasedTriggeringPolicy interval="1" modulate="true"/>
</Policies>
<!-- <DefaultRolloverStrategy fileIndex="max" max="24" /> -->
</RollingFile>
</Appenders>
<Loggers>
<AsyncLogger name="com.tritronik.mb.router" level="info"
additivity="false" includeLocation="true">
<AppenderRef ref="RouterRollingFile" />
</AsyncLogger>
<!-- <Root level="info">
<appender-ref ref="CONSOLE" />
</Root> -->
</Loggers>
I want to achieve daily rolling file with per hour rolling, so far I haven't been able to produce the log with proper format, and as I remember, the interval parameter seems like to increment by day not hour.
I want to achieve this:
router.log --> currently written file
router.log.2014-06-20-00
router.log.2014-06-20-01
...
router.log.2014-06-20-23
router.log.2014-06-21-00
...
Instead I achieve this:
router.log
router.log.2014-06-20-1 --> One day worth of logs
I've been able to do this using usual log4j, but the io performance goes down and force me to use log4j2, but I stumble into this problem.
Where do I have it wrong? Or is it true that log4j2 doesn't support this yet?
Thank you
You may have found a bug.
Is this only happening with Async Loggers or also when you configure a plain (synchronous) logger?
Also, have you tried using a filePattern that looks like this:
filePattern="/apps/bea/mb-logs/$${date:yyyy-MM-dd}/router.%d{yyyy-MM-dd-HH}.log"?
I have a (admittedly vague) suspicion that the $${date:...} part might be related.
If neither of the above makes any difference, could you please file a Jira ticket on the log4j2 issue tracker? https://issues.apache.org/jira/browse/LOG4J2