I just moved to log4j2. It works totally OK, but rolling file is not created. I've searched around, but haven't found any clues ( there are some similar topics on stackoverflow, but they don't seem to help me ).
Here is my configuration:
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="warn">
<Appenders>
<RollingFile name="RollingFile" fileName="logging-file.log" filePattern="logging-file-%d{yyyy-MM-dd}-%i.log">
<PatternLayout>
<pattern>%d{ISO8601} %X %5p %c{1}: - %m%n</pattern>
</PatternLayout>
<Policies>
<SizeBasedTriggeringPolicy size="100 MB" />
</Policies>
<DefaultRolloverStrategy max="10"/>
</RollingFile>
<Console name="Stdout" target="SYSTEM_OUT">
<PatternLayout pattern="%d{ISO8601} %X %5p %c{1}: - %m%n"/>
</Console>
</Appenders>
<Loggers>
<Root level="info">
<AppenderRef ref="Stdout"/>
<AppenderRef ref="RollingFile"/>
</Root>
</Loggers>
</Configuration>
Any help would be highly appreciated!
Thank you in advance!
Your filePattern contains a %d (date conversation pattern) and a %i (index within the specified date pattern, in your case day). However, you only specified a SizeBasedTriggeringPolicy in the rollover Policies section. You also need to add a TimeBasedTriggeringPolicy. That way you will get the desired combination of time based and size based rollover.
For details see https://logging.apache.org/log4j/2.x/manual/appenders.html#RollingFileAppender
Related
I would like to rotate the logs of the service when the service restarts even before the size limit is reached, tried the onstartup policy but its not working
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="DEBUG">
<Appenders>
<RollingFile name="FileAppender" fileName="logs/service.log" filePattern="logs/service.%d{MM-dd-yyyy}.log_%i">
<PatternLayout>
<pattern>%d{ISO8601} %p [%t] %c - %m%n</pattern>
</PatternLayout>
<Policies>
<OnStartupTriggeringPolicy />
<SizeBasedTriggeringPolicy size="100 MB" />
<TimeBasedTriggeringPolicy interval="1" modulate="true"/>
</Policies>
<DefaultRolloverStrategy max="5"/>
</RollingFile>
</Appenders>
<Loggers>
<Root level="info">
<AppenderRef ref="FileAppender"/>
</Root>
</Loggers>
</Configuration>
I would also like to rotate logs based on size and time and those policies work fine
In log4j2, I wanted to reconfigure automatically when file size exceeds. I have seen the attribute monitorinterval but it is defined for time based triggering policy. But I am looking for size based triggering policy.
I am going to assume that you want to know how to roll over a file when its size exceeds some threshold since reconfiguring automatically when a file exceeds some size is not something I have ever been asked before.
To roll over the file you would use the RollingFileAppender with the SizeBasedTriggeringPolicy. The configuration below will roll over the file once it hits 250 MB.
<?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-%i.log">
<PatternLayout>
<Pattern>%d %p %c{1.} [%t] %m%n</Pattern>
</PatternLayout>
<Policies>
<SizeBasedTriggeringPolicy size="250 MB"/>
</Policies>
</RollingFile>
</Appenders>
<Loggers>
<Root level="error">
<AppenderRef ref="RollingFile"/>
</Root>
</Loggers>
</Configuration>
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.
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.
I have defined a RollingFile Appender in log4j2
<RollingFile name="Locserver" append="true" fileName="locserver.log" filePattern="locserver-%i.log">
<PatternLayout>
<pattern>%d{ISO8601} [%t] %p %c %L - %m%n</pattern>
</PatternLayout>
<Policies>
<SizeBasedTriggeringPolicy size="50 MB"></SizeBasedTriggeringPolicy>
<DefaultRolloverStrategy>10</DefaultRolloverStrategy>
</Policies>
</RollingFile>
However when I try to run this I get an error
IllegalStateException : Pattern does not contain a date at
org.apache.logging.log4j.core.appender.rolling.PatternProcessor.getNExtTime(PatternProcessor.java:91)
This goes away as soon as I put a date pattern in filePattern for example, locserver-%d{MM-dd-yyyy}-%i.log. But I dont want the date in the log names. Is a bug or something wrong with my config?
In RollingFile appender,TimeBasedTrigerringPolicy checks for datepattern in filepattern attribute of tag. Specifing datepattern is then mandatory when using TimeBasedTrigerringPolicy.
Place %d{...} containing a SimpleDate Pattern in filePattern, e.g.:
<Appenders>
<RollingFile name="RollingFile" fileName="logs/rpi_gpio_latest.log"
filePattern="logs/**%d{yyyy-MM-dd_HH}**_rpi_gpio_%i.log">
<PatternLayout>
<Pattern>%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n</Pattern>
</PatternLayout>
<Policies>
<TimeBasedTriggeringPolicy />
<SizeBasedTriggeringPolicy size="4 MB"/>
</Policies>
<DefaultRolloverStrategy fileIndex="max"/>
</RollingFile>
</Appenders>
Thanks Joe.
I finally figured it out.
I had an empty TimeBasedTriggeringPolicy tag within my Policies list which was forcing the date check in the filePattern. Once I removed it, it started working fine.
For me, that works like described here (using log4j 2.0-beta9 & Java 1.7).
My log4j.xml (size-policy set to 1kB just for testing):
<?xml version="1.0" encoding="UTF-8"?>
<Configuration>
<Appenders>
<Console name="STDOUT" target="SYSTEM_OUT">
<PatternLayout pattern="%d %-5p [%t] %C{2} (%F:%L) - %m%n"/>
</Console>
<RollingFile name="Logfile"
fileName="Log/App.log"
filePattern="Log/App-%i.log">
<PatternLayout pattern="%d %-5p [%t] %C{2} (%F:%L) - %m%n"/>
<Policies>
<SizeBasedTriggeringPolicy size="1 KB"/>
</Policies>
<DefaultRolloverStrategy max="4"/>
</RollingFile>
</Appenders>
<Loggers>
<Root level="info">
<AppenderRef ref="STDOUT"/>
<AppenderRef ref="Logfile"/>
</Root>
</Loggers>
</Configuration>
And it also works when using your config fragment (I'd just corrected the DefaultRolloverStrategy-setting):
<?xml version="1.0" encoding="UTF-8"?>
<Configuration>
<Appenders>
<Console name="STDOUT" target="SYSTEM_OUT">
<PatternLayout pattern="%d %-5p [%t] %C{2} (%F:%L) - %m%n"/>
</Console>
<RollingFile name="Locserver"
append="true"
fileName="locserver.log"
filePattern="locserver-%i.log">
<PatternLayout>
<pattern>%d{ISO8601} [%t] %p %c %L - %m%n</pattern>
</PatternLayout>
<Policies>
<SizeBasedTriggeringPolicy size="50 MB"></SizeBasedTriggeringPolicy>
</Policies>
<DefaultRolloverStrategy max="4"/>
</RollingFile>
</Appenders>
<Loggers>
<Logger name="eeo.tts" level="debug"/>
<Root level="info">
<AppenderRef ref="STDOUT"/>
<AppenderRef ref="Locserver"/>
</Root>
</Loggers>
</Configuration>
Which Version of log4j2 are you using?