Log4j2 keeps same time for renaming of files - java

I am using log4j2 2.11 to create log files. I have these log files rotated based on time and size. The rotating based on time works great however, I am having a weird issue when I rotate based on size.
When I rotated based on size I compress the log file into the following named gzip file ${logdir}/${eventfilename}-%d{yyyyMMddHHmmss}{GMT+0}.log.gz, however, the log.gz file keeps ending up with the same time and thus overwrites the previous zipped log file endlessly. Where my understanding is that the current time will be used for the gzip file and not the first time grabbed to create the gzip file.
What could be causing this issue where my rotated file based on size keeps ending up with the same name and does not update to the current time to be used in the file name?
As of note here is my complete configuration file:
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="ALL" shutdownHook="disable">
<Properties>
<Property name="logdir">${env:DIR}/log</Property>
<Property name="eventfilename">foo_event</Property>
<Property name="errorfilename">foo_error</Property>
<Property name="msgformat">%d{yyyy-MM-dd HH:mm:ss.SSS}{GMT+0}Z [%t] %-5level %logger{1} - %msg%n</Property>
</Properties>
<Appenders>
<Console name="Console" target="SYSTEM_OUT">
<PatternLayout pattern="${msgformat}"/>
</Console>
<RollingFile name="ERROR_FILE" fileName="${logdir}/${errorfilename}.log"
filePattern="${logdir}/${errorfilename}-%d{yyyyMMddHHmmss}{GMT+0}.log.gz">
<PatternLayout charset="UTF-8" pattern="${msgformat}"/>
<Policies>
<SizeBasedTriggeringPolicy size="10 MB"/>
</Policies>
<DefaultRolloverStrategy>
<Delete basePath="${logdir}">
<IfFileName glob="${errorfilename}-*">
<IfAccumulatedFileSize exceeds="100 MB"/>
</IfFileName>
</Delete>
</DefaultRolloverStrategy>
<ThresholdFilter level="WARN"/>
</RollingFile>
<RollingFile name="EVENT_FILE" fileName="${logdir}/${eventfilename}.log"
filePattern="${logdir}/${eventfilename}-%d{yyyyMMddHHmmss}{GMT+0}.log.gz">
<PatternLayout charset="UTF-8" pattern="${msgformat}"/>
<Policies>
<TimeBasedTriggeringPolicy interval="43200" modulate="false"/>
<SizeBasedTriggeringPolicy size="1 MB"/>
</Policies>
<DefaultRolloverStrategy>
<Delete basePath="${logdir}">
<IfFileName glob="${eventfilename}-*">
<IfAccumulatedFileSize exceeds="100 MB"/>
</IfFileName>
</Delete>
</DefaultRolloverStrategy>
<ThresholdFilter level="TRACE"/>
</RollingFile>
</Appenders>
<Loggers>
<Logger name="foo.utils.FooLogger" level="TRACE"
additivity="false">
<AppenderRef ref="ERROR_FILE"/>
<AppenderRef ref="EVENT_FILE"/>
</Logger>
<Logger name="foo.utils.foo.FooPathUtils" level="INFO"
additivity="false">
<!-- Change level to debug to log java path info at startup -->
<AppenderRef ref="ERROR_FILE"/>
<AppenderRef ref="EVENT_FILE"/>
</Logger>
<Logger name="foo" level="INFO" additivity="false">
<AppenderRef ref="ERROR_FILE"/>
<AppenderRef ref="EVENT_FILE"/>
</Logger>
<Root level="TRACE">
<AppenderRef ref="Console" />
</Root>
</Loggers>
</Configuration>

Related

Log4j2 : Changing log level for a specific user and specific loggers : not working as expected

Requirement is to change the log level for a specific user . Using DynamicThresholdFilter at the context level does work but applies to all the loggers . My requirement is to only apply it for specific loggers and not impact the others. With this configuration file all the appenders are logging at debug levell.
This is how my file looks like . Any recommendations on applying the same filter or any other filter which can enable debug logging for specific loggers ?
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="INFO" monitorInterval="30">
<Properties>
---------------------
</Properties>
<DynamicThresholdFilter
key="customLogging-enabled" onMatch="ACCEPT" onMismatch="DENY">
<KeyValuePair key="true" value="DEBUG" />
</DynamicThresholdFilter>
<Appenders>
<Console name="consoleAppender" target="SYSTEM_OUT"
follow="true">
<PatternLayout pattern="${CONSOLE_LOG_PATTERN}" />
</Console>
<RollingFile name="customLoggingAppender"
fileName="${sys:log_dir}/customLogging.log"
filePattern="customLogging-%d{yyyy-MM-dd}-%i.log" append="true">
<ThreadContextMapFilter onMatch="DENY"
onMismatch="NEUTRAL">
<KeyValuePair key="customLogging-enabled" value="true" />
</ThreadContextMapFilter>
<PatternLayout>
.......................
</PatternLayout>
<Policies>
<SizeBasedTriggeringPolicy size="5000KB" />
</Policies>
<DefaultRolloverStrategy max="20" />
</RollingFile>
<RollingFile name="standardAppender"
fileName="${sys:log_dir}/standard.log"
filePattern="standard-%d{yyyy-MM-dd}-%i.log" append="true">
<PatternLayout>
....................
</PatternLayout>
<Policies>
<SizeBasedTriggeringPolicy size="5000KB" />
</Policies>
<DefaultRolloverStrategy max="20" />
</RollingFile>
.......More appenders here
</Appenders>
<Loggers>
<Root level="ERROR">
<AppenderRef ref="consoleAppender" />
<AppenderRef ref="standardAppender" />
</Root>
<!-- DEFAULTED TO ROOT LOGGER - START -->
<Logger name="com.abc" level="ERROR">
<AppenderRef ref="customLoggingAppender" />
</Logger>
<Logger name="com.abc.helpers" level="ERROR">
<AppenderRef ref="customLoggingAppender" />
</Logger>
<Logger name="com.abc.business.persist.utils" level="ERROR" />
<Logger
name="com.abc.business.persist.ManagerFactory"
level="ERROR" />
<Logger name="com.da.handlers" level="ERROR" additivity="false">
<AppenderRef ref="abcAppender" />
<AppenderRef ref="customLoggingAppender" />
</Logger>
<Logger name="com.handler.audit" level="ERROR"
additivity="false">
<AppenderRef ref="xyzAppender" />
</Logger>
</Loggers>
</Configuration>
I tried applying the same filter at the appender level but it did not work
<RollingFile name="customLoggingAppender" fileName="${sys:log_dir}/customLogging.log" filePattern="customLogging-%d{yyyy-MM-dd}-%i.log" append="true">
<DynamicThresholdFilter key="customLogging-enabled" onMatch="ACCEPT" onMismatch="DENY">
<KeyValuePair key="true" value="DEBUG"/>
</DynamicThresholdFilter>
<PatternLayout>
....
</PatternLayout>
<Policies>
<SizeBasedTriggeringPolicy size="5000KB" />
</Policies>
<DefaultRolloverStrategy max="20" />
</RollingFile>
My expectation is that ONLY the loggers referring to this appender will be logging at debug level

Log4j2 doesn't log into logfile

I changed the path of log4j2.xml configuration file and now the log files will create but application doesn't log into the files. I get no error messages.
My order structur:
Log4j2.xml:
<Properties>
<Property name="LOG_PATTERN">
%d{yyyy-MM-dd HH:mm:ss.SSS} %5level ${hostName} --- [%15.15thread] %-100.100class : %message%n%exception
</Property>
</Properties>
<Appenders>
<Console name="ConsoleAppender" target="SYSTEM_OUT" follow="true">
<PatternLayout pattern="${LOG_PATTERN}"/>
</Console>
<RollingFile name="RootAppender" fileName="./log/log.log" filePattern="./log/log-%d{yyyy-MM-dd}-%i.log">
<PatternLayout>
<Pattern>${LOG_PATTERN}</Pattern>
</PatternLayout>
<Policies>
<SizeBasedTriggeringPolicy size="100MB" />
<TimeBasedTriggeringPolicy interval="1"/>
</Policies>
<DefaultRolloverStrategy max="7"/>
</RollingFile>
<RollingFile name="CurlAppender" fileName="./log/curl.log" filePattern="./log/curl-%d{yyyy-MM-dd}-%i.log">
<PatternLayout>
<Pattern>${LOG_PATTERN}</Pattern>
</PatternLayout>
<Policies>
<SizeBasedTriggeringPolicy size="500MB" />
<TimeBasedTriggeringPolicy interval="1"/>
</Policies>
<DefaultRolloverStrategy max="7"/>
</RollingFile>
</Appenders>
<Loggers>
<Root level="info">
<AppenderRef ref="ConsoleAppender" />
<AppenderRef ref="RootAppender"/>
</Root>
<Logger name="CurlLogger" level="trace" additivity="false">
<AppenderRef ref="CurlAppender"/>
</Logger>
</Loggers>
I've found the issue.
In the application.properties file there must be set the "logger.config" attribute.
After setting this attribute, the file logging works.
I think there is some issue in Loggers tag.
In the Logger name please add your package name and also change your AppenderRef in Logger.
Please change your xml code to below:
<Loggers>
<Root level="info">
<AppenderRef ref="ConsoleAppender" />
<AppenderRef ref="RootAppender"/>
</Root>
<Logger name="com.example.springbackend.controller" level="trace" additivity="false">
<AppenderRef ref="ConsoleAppender" />
<AppenderRef ref="RootAppender"/>
</Logger>
</Loggers>
Here <Logger name="com.example.springbackend.controller" level="trace" additivity="false"> Logger name is project package name.
I think the issue is in root level.
Please change the root level logger to debug or trace.

How to use log4j2.component.properties file for org.apache.logging.log4j.core.util.Clock

According to this log4j2 configuration I am trying to set the "log4j2.component.properties" file in my NetBeans project. I have already configured my log4j2 with ASYNC_LOGGERS and one thing I particularly need is the "log4j.Clock" property. Because I get the data at a very high speed from the Servers and we expect that the logging timestamp needs to be perfect.
How can I use the "log4j2.component.properties" file in my project. Currently I created the "log4j2.component.properties" file in my default package of the project and set the values as below:
My log4j2.component.properties file
Log4jContextSelector=org.apache.logging.log4j.core.async.AsyncLoggerContextSelector
AsyncLogger.RingBufferSize=512*1024
-DAsyncLoggerConfig.RingBufferSize=512*1024
log4j.Clock=SystemClock
My log4j2.xml file
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="warn">
<Appenders>
<RollingRandomAccessFile name="Messages-log" fileName="Log4J/Messages-${date:yyyy-MM-dd}.log"
immediateflush="true" filePattern="Log4J/Messages-%d{MM-dd-yyyy-HH}-%i.log.gz">
<PatternLayout>
<Pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} %p %m%n</Pattern>
</PatternLayout>
<Policies>
<TimeBasedTriggeringPolicy />
<SizeBasedTriggeringPolicy size="500 MB"/>
</Policies>
<DefaultRolloverStrategy max="50"/>
</RollingRandomAccessFile>
<RollingRandomAccessFile name="Error-log" fileName="Log4J/Errors-${date:yyyy-MM-dd}.log"
immediateflush="false" filePattern="Log4J/Errors-%d{MM-dd-yyyy-HH}-%i.log.gz">
<PatternLayout>
<Pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} %p %m%n</Pattern>
</PatternLayout>
<Policies>
<TimeBasedTriggeringPolicy />
<SizeBasedTriggeringPolicy size="500 MB"/>
</Policies>
<DefaultRolloverStrategy max="50"/>
</RollingRandomAccessFile>
</Appenders>
<Loggers>
<Logger name="Messages-log" level="info" additivity="false">
<appender-ref ref="Messages-log" level="info"/>
</Logger>
<Logger name="Error-log" level="info" additivity="false">
<appender-ref ref="Error-log" level="info"/>
</Logger>
<root level="info">
<appender-ref ref="Messages-log"/>
<appender-ref ref="Error-log"/>
</root>
</Loggers>
</Configuration>
Is this correct way of using it? I am highly confused in how to use it. I want the timestamp to be perfect and so I want to use the "log4j2.clock" parameter set to "org.apache.logging.log4j.core.util.Clock". All I want is asynchronous logging with the exact time when the record has reached. But I am getting the following exception:
ERROR StatusLogger Could not create org.apache.logging.log4j.core.util.Clock: java.lang.InstantiationException: org.apache.logging.log4j.core.util.Clock, using default SystemClock for timestamps.
Please help me.
Thanks in advance

How to log LoggerMarker to specific file only?

I'm trying to achive the following using log4j2:
Log all general content to console, but log MarkerFilter.FILTERED only to a specific file, not to console.
<Configuration>
<Appenders>
<Console name="CONSOLE" target="SYSTEM_OUT">
<ThresholdFilter level="INFO" onMatch="ACCEPT" onMismatch="DENY"/>
</Console>
<RollingFile name="FILTERED" fileName="filtered.txt" />
<MarkerFilter marker="FILTERED" onMatch="ACCEPT" onMismatch="DENY"/>
</RollingFile>
</Appenders>
<Loggers>
<Root level="all">
<AppenderRef ref="CONSOLE" />
<AppenderRef ref="FILTERED" />
</Root>
</Loggers>
</Configuration>
The following works in general, BUT logs any FILTERED content to console additionally.
But when I remove the <AppenderRef ref="FILTERED" /> form <root> loggers, nothing is logged to the file anymore!
How about using the marker filter on the console appender with a DENY action?
You can set the level on the appender-ref, so you don't need to use a ThresholdFilter for that.
<Configuration status="warn">
<Appenders>
<Console name="CONSOLE" target="SYSTEM_OUT">
<MarkerFilter marker="FILTERED" onMatch="DENY" onMismatch="ACCEPT"/>
</Console>
<RollingFile name="FILTERED" fileName="filtered.txt" />
<MarkerFilter marker="FILTERED" onMatch="ACCEPT" onMismatch="DENY"/>
</RollingFile>
</Appenders>
<Loggers>
<Root level="all">
<AppenderRef ref="CONSOLE" level="info" />
<AppenderRef ref="FILTERED" />
</Root>
</Loggers>
</Configuration>

log4j2 file not writing to file

I've been searching the web for days now but somehow nothing seems to help.
I try to migrate from log4j 1.x to log4j 2 but something is not right here and I hope one of you can help.
I have a JSF application running on Websphere 8 and this is is my configuration so far:
<?xml version="1.0" encoding="UTF-8"?>
<Configuration>
<Filter type="ThresholdFilter" level="trace"/>
<Appenders>
<RollingFile name="DEBUG" filePattern="C:/Files/varm_debug.%d{yyyy-MM-dd}.log" fileName="C:/Files/varm_debug.log" append="true">
<PatternLayout pattern="%d{yyyy-MM-dd HH:mm:ss,SSS} ;%-5p; [%c{1}.%M]; %m%n"/>
<Policies>
<SizeBasedTriggeringPolicy size="1500KB"/>
</Policies>
<DefaultRolloverStrategy max="20"/>
<ThresholdFilter level="DEBUG" onMatch="ACCEPT" onMismatch="DENY"/>
</RollingFile>
<RollingFile name="INFO" filePattern="C:/Files/varm_info.%d{yyyy-MM-dd}.log" fileName="C:/Files/varm_info.log" append="true">
<PatternLayout pattern="%d{yyyy-MM-dd HH:mm:ss,SSS} ;%-5p; [%c{1}.%M]; %m%n"/>
<Policies>
<SizeBasedTriggeringPolicy size="1500KB"/>
</Policies>
<DefaultRolloverStrategy max="20"/>
<ThresholdFilter level="INFO" onMatch="ACCEPT" onMismatch="DENY"/>
</RollingFile>
<RollingFile name="WARN" filePattern="C:/Files/varm_warn.%d{yyyy-MM-dd}.log" fileName="C:/Files/varm_warn.log" append="true">
<PatternLayout pattern="%d{yyyy-MM-dd HH:mm:ss,SSS} ;%-5p; [%c{1}.%M]; %m%n"/>
<Policies>
<SizeBasedTriggeringPolicy size="500KB"/>
</Policies>
<DefaultRolloverStrategy max="20"/>
<ThresholdFilter level="WARN" onMatch="ACCEPT" onMismatch="DENY"/>
</RollingFile>
<RollingFile name="ERROR" filePattern="C:/Files/varm_error.%d{yyyy-MM-dd}.log" fileName="C:/Files/varm_error.log" append="true">
<PatternLayout pattern="%d{yyyy-MM-dd HH:mm:ss,SSS} ;%-5p; [%c{1}.%M]; %m%n"/>
<Policies>
<SizeBasedTriggeringPolicy size="500KB"/>
</Policies>
<DefaultRolloverStrategy max="20"/>
<ThresholdFilter level="ERROR" onMatch="ACCEPT" onMismatch="DENY"/>
</RollingFile>
<File name="comparisonLogAppender" fileName="C:/Files/VARM-output.log" append="true">
<PatternLayout pattern="%d{ISO8601}; %m%n"/>
</File>
</Appenders>
<Loggers>
<!-- Logger to compare results -->
<!-- Logger to log infos/errors throughout comparison -->
<Logger name="comparisonLogger" level="trace" additivity="true" immediateFlush="true">
<AppenderRef ref="comparisonLogAppender"/>
</Logger>
<Logger name="de.saltsolutions.varm" level="info" >
<AppenderRef ref="DEBUG" level="debug"/>
<AppenderRef ref="INFO" level="info"/>
<AppenderRef ref="WARN" level="warn"/>
<AppenderRef ref="ERROR" level="error"/>
</Logger>
<Logger name="org.hibernate" level="warn">
<AppenderRef ref="DEBUG" level="debug"/>
<AppenderRef ref="INFO" level="info"/>
<AppenderRef ref="WARN" level="warn"/>
<AppenderRef ref="ERROR" level="error"/>
</Logger>
<Root level="debug">
<AppenderRef ref="DEBUG" level="debug"/>
</Root>
<Root level="info">
<AppenderRef ref="INFO" level="info"/>
</Root>
<Root level="warn">
<AppenderRef ref="WARN" level="warn"/>
</Root>
<Root level="error">
<AppenderRef ref="ERROR" level="error"/>
</Root>
</Loggers>
</Configuration>
All the files are generated okay but they stay empty.
My main problem is the comparisonLogger. If I use it (LogManager.getLogger("comparisonLogger") in my main class, it writes the log file perfectly.
But when I try using the exact same logger in my ManagedBean (ViewScoped) nothing gets logged.
Does anybody know why it doesn't work in my ManagedBean? Does this have anything to do with the Websphere?
I hope anybody can help because I have absolutely no idea what I could possibly do to make this work.
Thanks so much in advance
I finally figured out my problems.
I use Log4j2 in a web application which means that I needed to add some stuff in my web.xml (further information here: http://logging.apache.org/log4j/2.x/manual/webapp.html)
You can only configure ONE root logger
Once I adjusted that, everything worked just as expected.
My Log4j2 Jar-Files are now in my client and my ear and my XML config is in my client but it also works if you split the config files (one part in my web project the other for the client on C:\temp or some other directory)
You may need to put the log4j2 jar files and log4j2.xml config in a place so that they are always in the classpath.

Categories