Log4j2 - 2 appenderFile. I use only one ref, but two files are generated (One of them are empty) - log4j2

I have two appenders "RollingFile" like this :
<RollingFile name="fileDebug" fileName="${REP}/debug.log" filePattern="${REP}/debug-%d{yyyy-MM-dd}-%i.log">
<Filters>
<ThresholdFilter level="debug" onMatch="ACCEPT" onMismatch="DENY"/>
</Filters>
<PatternLayout pattern="${LOG_PATTERN}"/>
<Policies>
<TimeBasedTriggeringPolicy/>
</Policies>
<DefaultRolloverStrategy max="2"/>
</RollingFile>
<RollingFile name="fileInfo" fileName="${REP}/info.log" filePattern="${REP}/info-%d{yyyy-MM-dd}-%i.log">
<Filters>
<ThresholdFilter level="info" onMatch="ACCEPT" onMismatch="DENY"/>
</Filters>
<PatternLayout pattern="${LOG_PATTERN}"/>
<Policies>
<TimeBasedTriggeringPolicy/>
</Policies>
<DefaultRolloverStrategy max="2"/>
</RollingFile>
And I call only one of them. For example
<Loggers>
<Root level="all">
<appender-ref ref="fileDebug"/>
</Root>
</Loggers>
But, when I run my program, 2 files are generated : debug.log with logs and an empty info.log
How can I have only one file (debug.log) in this configuration ?
Many thanks

Finally, I make this configuration with the logging's level adjusted to ${LEVEL}
<RollingFile name="file" fileName="${REP}/file-${LEVEL}.log" filePattern="${REP}/file-${LEVEL}-%d{yyyy-MM-dd}-%i.log">
<Filters>
<ThresholdFilter level="${LEVEL}" onMatch="ACCEPT" onMismatch="DENY"/>
</Filters>
<PatternLayout pattern="${LOG_PATTERN}"/>
<Policies>
<TimeBasedTriggeringPolicy/>
</Policies>
<DefaultRolloverStrategy max="2"/>
</RollingFile>
<Loggers>
<Root level="all">
<appender-ref ref="file"/>
</Root>
</Loggers>

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

Log File is not creating for RegexFilter in Log4j2.xml

I am using below config file to generate log files .
details are given below, I can see only GoCreditDL.log is generating in Unix server after deployment.
there is no error regarding Data.log.
I have tested it my local machine its working fine, but on Unix server I am getting only one logs.
Not sure Regex filter is not working in Unix Env.
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<Properties>
<Property name="LOG_PATTERN">%d{dd MMM yyyy HH:mm:ss.SSS} %5p [%t] (%F:%L) - %m%n</Property>
<Property name="FILE_PATTERN">%d{yyyy-MM-dd-HH}-%i.log</Property>
<Property name="APP_LOG_ROOT">/home/gocredit2/gocredit_DL/log</Property>
<Property name="FILE_PATTERN_PATH">logs/$${date:yyyy-MM-dd}</Property>
<Property name="INTERVAL">1</Property>
<Property name="SIZE10MB">10 MB</Property>
<Property name="SIZE20MB">20 MB</Property>
<Property name="RolloverStrategy">10</Property>
</Properties>
<Appenders>
<RollingFile name="EJBGoCreditLog" fileName="${APP_LOG_ROOT}/GoCreditDL.log"
filePattern="${FILE_PATTERN_PATH}/GoCreditDL-${FILE_PATTERN}">
<PatternLayout>
<Pattern>${LOG_PATTERN}</Pattern>
</PatternLayout>
<Policies>
<TimeBasedTriggeringPolicy interval="${INTERVAL}" modulate="true" />
<SizeBasedTriggeringPolicy size="${SIZE20MB}"/>
</Policies>
<DefaultRolloverStrategy max="${RolloverStrategy}"/>
</RollingFile>
<RollingFile name="Stored" fileName="${APP_LOG_ROOT}/Data.log"
filePattern="${FILE_PATTERN_PATH}/Data-${FILE_PATTERN}">
<PatternLayout>
<Pattern>${LOG_PATTERN} </Pattern>
</PatternLayout>
<Policies>
<TimeBasedTriggeringPolicy interval="${INTERVAL}" modulate="true" />
<SizeBasedTriggeringPolicy size="${SIZE20MB}"/>
</Policies>
<DefaultRolloverStrategy max="${RolloverStrategy}"/>
<RegexFilter regex=".*Stored.*" onMatch="ACCEPT" onMismatch="DENY"/>
</RollingFile>
</Appenders>
<Loggers>
<root level="INFO">
<appender-ref ref="Stored" level="INFO"/>
<appender-ref ref="EJBGoCreditLog" level="INFO"/>
</root>
</Loggers>
</configuration>

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 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