log4j2 RollingFile Appender requires date pattern - log4j2

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?

Related

log4j2 OnStartupTriggeringPolicy doesnt rotate logs at service restart

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

Personalize RegexFilter in log4j2 for an xml file

despite the various posts found with the various solutions I can't solve a problem.
I am using Eclipse for a project and I would like only logs containing a certain expression / word to appear in a given log file.
For the configuration of log4j2 I am using an xml file
appenders files are different
below the xml file
<Properties>
<Property name="path">./LOGS</Property>
</Properties>
<Appenders>
<Console name="Console" target="SYSTEM_OUT">
<PatternLayout>
<pattern> %d{yyy-MM-dd HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n </pattern>
</PatternLayout>
</Console>
<RollingFile name="Log"
fileName="${path}/log.txt" immediateFlush="false" append="true"
filePattern="${path}/log-%d{yyyy-MM-dd}-%i.txt">
fileIndex = min
<PatternLayout>
<pattern> %d{yyy-MM-dd HH:mm:ss} [%t] %-5level %logger{36} - %msg%n </pattern>
</PatternLayout>
<Policies>
<OnStartupTriggeringPolicy />
<TimeBasedTriggeringPolicy interval="1" modulate="true"/>
<SizeBasedTriggeringPolicy size="10 MB" />
</Policies>
<DefaultRolloverStrategy max="40" />
</RollingFile>
<RollingFile name="LogScarti"
fileName="${path}/log_scarti.txt" immediateFlush="false" append="true"
filePattern="${path}/log_scarti-%d{yyyy-MM-dd}-%i.txt">
<PatternLayout>
<pattern> %d{yyy-MM-dd HH:mm:ss} [%t] %-5level %logger{36} - %msg%n </pattern>
</PatternLayout>
<Policies>
<OnStartupTriggeringPolicy />
<TimeBasedTriggeringPolicy interval="1" modulate="true"/>
<SizeBasedTriggeringPolicy size="10 MB" />
</Policies>
<DefaultRolloverStrategy max="40" />
</RollingFile>
<RollingFile name="Performance"
fileName="${path}/Performance.txt" immediateFlush="false" append="true"
filePattern="${path}/Performance-%d{yyyy-MM-dd}-%i.txt">
<Filters>
<RegexFilter regex=".*common.utils.PerformanceMonitor.*" onMatch="DENY" onMismatch="ACCEPT"/>
</Filters>
<PatternLayout>
<pattern> %d{yyy-MM-dd HH:mm:ss} [%t] %-5level %logger{36} - %msg%n </pattern>
</PatternLayout>
<Policies>
<OnStartupTriggeringPolicy />
<TimeBasedTriggeringPolicy interval="1" modulate="true"/>
<SizeBasedTriggeringPolicy size="10 MB" />
</Policies>
<DefaultRolloverStrategy max="40" />
</RollingFile>
</Appenders>
<Loggers>
<Root level="info">
<AppenderRef ref="Console"/>
<AppenderRef ref="Log" level="Info"/>
<AppenderRef ref="LogScarti" level="Error" />
<AppenderRef ref="Performance" level="Info" />
</Root>
</Loggers>
Nothing is recorded in the Performance file and if I play with the accept / deny parameters, it writes everything to me as in the logs file.
I hope I have correctly illustrated the problem
thanks to those who will dedicate even just a minute of their time
The RegexFilter only filters the content of the log message (the %m pattern converter), whereas you are apparently trying to filter by logger name.
To filter by logger name you just need to attach your appender to the correct logger configuration:
<Loggers>
<Root level="info">
<AppenderRef ref="Console"/>
<AppenderRef ref="Log" level="Info"/>
<AppenderRef ref="LogScarti" level="Error" />
</Root>
<Logger name="com.accenture.etl.bollini.common.utils.PerformanceMonitor"
level=info>
<AppenderRef ref="Performance" />
</Logger>
</Loggers>

How to write different log levels into different rolling files in log4j2

I have a log4j2.xml as follows:
<?xml version="1.0" encoding="UTF-8"?>
<Configuration>
<Properties>
<Property name="dir" value="/var/log/sunverge" />
<Property name="file" value="maggie.log" />
<Property name="eatonemcbfile" value="eaton.log" />
</Properties>
<Appenders>
<Console name="STDOUT" target="SYSTEM_OUT">
<PatternLayout pattern="%d{ISO8601} %5p [%t] %c - %m%n" />
</Console>
<RollingFile name="file" fileName="${dir}/${file}"
filePattern="${dir}/${file}.%d{yyyy-MM-dd}">
<PatternLayout>
<Pattern>%d{ISO8601} %5p [%t] %c - %m%n</Pattern>
</PatternLayout>
<Policies>
<TimeBasedTriggeringPolicy interval="1" />
</Policies>
</RollingFile>
<RollingFile name="eaton"
fileName="${dir}/${eatonemcbfile}"
filePattern="${dir}/${eatonemcbfile}.%d{yyyy-MM-dd}">
<PatternLayout>
<Pattern name="ConversionPattern"> %d{ISO8601} %5p [%t] %c - %m%n</Pattern>
</PatternLayout>
<ThresholdFilter level="debug"/>
<Policies>
<TimeBasedTriggeringPolicy interval="1" />
</Policies>
</RollingFile>
</Appenders>
<Loggers>
<Root level="warn">
<AppenderRef ref="file" />
</Root>
<logger name="com.sunverge.devices.eaton.emcb" level="info">
<AppenderRef ref="eaton" />
<AppenderRef ref="file" />
</logger>
</Loggers>
</Configuration>
So however, I set the threshold for the eaton.log to be debug but both of them log the debug level. For some reason I don't want to add add the ThresholdFilter tag for the other file. Any help appreciated.
You should not be getting debug logs to any of your files as none of your loggers is configured to accept debug messages. If you are seeing them in the file then this configuration is not being used. You can verify whether it is being used or not by adding status="debug" to the configuration element and looking at what Log4j is configuring. If you don't see log4j debug output then that is a clear sign the configuration file is not being used. You can also add -Dlog4j2.debug=true to the command line which will cause the log4j logs to be output. You can inspect that to see where the configuration is coming from.
I would recommend changing your configuration to:
<Appenders>
<Console name="STDOUT" target="SYSTEM_OUT">
<PatternLayout pattern="%d{ISO8601} %5p [%t] %c - %m%n" />
</Console>
<RollingFile name="file" fileName="${dir}/${file}"
filePattern="${dir}/${file}.%d{yyyy-MM-dd}">
<PatternLayout>
<Pattern>%d{ISO8601} %5p [%t] %c - %m%n</Pattern>
</PatternLayout>
<Policies>
<TimeBasedTriggeringPolicy interval="1" />
</Policies>
</RollingFile>
<RollingFile name="eaton"
fileName="${dir}/${eatonemcbfile}"
filePattern="${dir}/${eatonemcbfile}.%d{yyyy-MM-dd}">
<PatternLayout>
<Pattern name="ConversionPattern"> %d{ISO8601} %5p [%t] %c - %m%n</Pattern>
</PatternLayout>
<Policies>
<TimeBasedTriggeringPolicy interval="1" />
</Policies>
</RollingFile>
</Appenders>
<Loggers>
<Root level="warn">
<AppenderRef ref="file" />
</Root>
<logger name="com.sunverge.devices.eaton.emcb" level="debug" additivity="false">
<AppenderRef ref="eaton" level="debug"/>
<AppenderRef ref="file" level="info"/>
</logger>
</Loggers>
</Configuration>

log4j2 - rolling file not created

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

log4j2 configuration empty log files

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.

Resources