migrating log4j.xml to log4j2.xml jdk8 - java

Hello i am migrating to latest log4j version and migrated from 1.x to 2.x log4j. So here is my log4j2 config file. I am stuck with debug and error logs, maybe someone could see that if there should be something different.
<?xml version="1.0" encoding="UTF-8" ?>
<Configuration status="debug" packages="">
<Appenders>
<!-- System.out log -->
<Console name="console" target="SYSTEM_OUT">
<PatternLayout pattern="%d{ISO8601} [%t] %-5p %c{1} - %m%n"/>
</Console>
<!-- debug log -->
<RollingFile name="debug" fileName="/var/logs/debug.log"
filePattern="/var/logs/%d{ddMMyyyy}_debug.log.gz">
<PatternLayout pattern="%d{ISO8601} [%t] %-5p %c{1} - %m%n"/>
<Policies>
<SizeBasedTriggeringPolicy size="100 MB"/>
</Policies>
<LevelRangeFilter minLevel="debug" maxLevel="info" onMatch="accept"/>
</RollingFile>
<!-- error log -->
<RollingFile name="error" fileName="/var/logs/error.log"
filePattern="/var/logs/%d{ddMMyyyy}_error.log.gz">>
<PatternLayout pattern="%d{ISO8601} [%t] %-5p %c{1} - %m%n"/>
<Policies>
<SizeBasedTriggeringPolicy size="100 MB"/>
</Policies>
<LevelRangeFilter minLevel="warn" maxLevel="fatal" onMatch="accept"/>
</RollingFile>
<!-- info log -->
<RollingFile name="info" fileName="/var/logs/info.log"
filePattern="/var/logs/%d{ddMMyyyy}_info.log.gz">>
<PatternLayout pattern="%d{ISO8601} [%t] %-5p %c{1} - %m%n"/>
<Policies>
<SizeBasedTriggeringPolicy size="100 MB"/>
</Policies>
<LevelRangeFilter minLevel="info" maxLevel="info" onMatch="accept"/>
</RollingFile>
<!-- Special daily time log. Logib kui palju aega iga Request vytab -->
<RollingFile name="timelog" fileName="/var/logs/input_timing.log"
filePattern="'.'yyyy-MM-dd'.log'">
<PatternLayout pattern="%d{ISO8601} [%t] %-5p %c{1} - %m%n"/>
<Policies>
<SizeBasedTriggeringPolicy size="20 MB"/>
</Policies>
</RollingFile>
<!-- accesslog log appender -->
<RollingFile name="accesslog" fileName="/var/logs/access_log.log"
filePattern="'_'yy_ww'.txt'">
<PatternLayout pattern="%d{ISO8601} [%t] %-5p %c{1} - %m%n"/>
<Policies>
<SizeBasedTriggeringPolicy size="20 MB"/>
</Policies>
</RollingFile>
<RollingFile name="todolog" fileName="/var/logs/todo.log"
filePattern="'.'yyyy-MM-dd'.log'" ignoreExceptions="false">
<PatternLayout pattern="%d{ISO8601} [%t] %-5p %c{1} - %m%n"/>
<Policies>
<SizeBasedTriggeringPolicy size="20 MB"/>
</Policies>
</RollingFile>
<!-- Special redirections servlet log. Logib online_report servlet'i teated -->
<RollingFile name="redirectlog" fileName="/var/logs/redirect.log"
filePattern="'.'yyyy-MM-dd'.log'" ignoreExceptions="false">
<PatternLayout pattern="%d{ISO8601} [%t] %-5p %c{1} - %m%n"/>
<Policies>
<SizeBasedTriggeringPolicy size="20 MB"/>
</Policies>
</RollingFile>
<!-- Asynchronous appender. Writes the log, when it has the time. -->
<Async name="asyncRoot" bufferSize="10" includeLocation="true">
<AppenderRef ref="info"/>
<AppenderRef ref="error" level="error" />
<AppenderRef ref="debug" level="debug" />
</Async>
</Appenders>
<Loggers>
<!-- additivity false - do not log to other appenders as defined here -->
<Logger name="TIMING" additivity="false" level="info">
<AppenderRef ref="timelog"/>
</Logger>
<!-- additivity false - do not log to other appenders as defined here -->
<Logger name="TODO" additivity="false" level="info">
<AppenderRef ref="todolog"/>
</Logger>
<!-- additivity false - do not log to other appenders as defined here -->
<Logger name="ACCESS" additivity="false" level="info">
<AppenderRef ref="accesslog"/>
</Logger>
<!-- additivity false - do not log to other appenders as defined here -->
<Logger name="REDIRECT" additivity="false" level="info">
<AppenderRef ref="redirectlog"/>
</Logger>
<!-- root logger -->
<Root level="debug">
<AppenderRef ref="asyncRoot"/>
</Root>
</Loggers>
</Configuration>
public class EffencyTests {
private static final Logger log = LogManager.getLogger(EffencyTests.class);
#Before
public void cos() {
log.info("hey");
log.debug("czesc");
log.error("smth");
}
#Test
public void cosata() {
log.info("hey");
log.info("hey");
log.info("hey");
log.info("hey");
log.info("hey");
log.info("hey");
log.debug("hey");
}
#Test
public void cosatae() {
int numberOne = 1;
int numberTwo = 2;
log.info("test");
try {
if(numberOne != numberTwo) {
throw new IllegalArgumentException();
}
}catch (Exception e){
log.error("error");
}
}
here is some output on info log :
error.log and debug.log are empty, i am really stuck for a long time can anyone help me with that ? I am struggling for over month and i feel like there is single letter in config is missing. Looking for any ideas and help thanks

if anyone would struggle it helped at my case :
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="debug">
<Appenders>
<!-- System.out log -->
<Console name="console" target="SYSTEM_OUT">
<PatternLayout pattern="%d{ISO8601} [%t] %-5p %c{1} - %m%n"/>
</Console>
<!-- error log -->
<RollingFile name="error" fileName="./logs/errors.log"
filePattern="./logs/${date:yyyy-MM}/errors-%d{yyyy-MM-dd}-%i.log.gz"
filePermissions="rw-rw-r--">
<PatternLayout>
<Pattern>%d{ISO8601} [%t] %-5p %c{1} - %m%n</Pattern>
</PatternLayout>
<Policies>
<TimeBasedTriggeringPolicy interval="6" modulate="true"/>
<SizeBasedTriggeringPolicy size="10 MB"/>
</Policies>
</RollingFile>
<!-- info log -->
<RollingFile name="info" fileName="./logs/info.log"
filePattern="./logs/${date:yyyy-MM}/info-%d{yyyy-MM-dd}-%i.log.gz"
filePermissions="rw-rw-r--">
<PatternLayout>
<Pattern>%d{ISO8601} [%t] %-5p %c{1} - %m%n</Pattern>
</PatternLayout>
<Policies>
<TimeBasedTriggeringPolicy interval="6" modulate="true"/>
<SizeBasedTriggeringPolicy size="10 MB"/>
</Policies>
<LevelRangeFilter minLevel="info" maxLevel="info" onMatch="accept"/>
</RollingFile>
<!-- debug log -->
<RollingFile name="debug" fileName="./logs/debug.log"
filePattern="./logs/${date:yyyy-MM}/debug-%d{yyyy-MM-dd}-%i.log.gz"
filePermissions="rw-rw-r--">
<PatternLayout>
<Pattern>%d{ISO8601} [%t] %-5p %c{1} - %m%n</Pattern>
</PatternLayout>
<Policies>
<TimeBasedTriggeringPolicy interval="6" modulate="true"/>
<SizeBasedTriggeringPolicy size="10 MB"/>
</Policies>
<LevelRangeFilter minLevel="debug" maxLevel="debug" onMatch="accept"/>
</RollingFile>
<!-- Special daily time log. Logib kui palju aega iga Request vytab -->
<RollingFile name="timelog" fileName="./logs/input_timing.log"
filePattern="'.'yyyy-MM-dd'.log'"
filePermissions="rw-rw-r--">
<PatternLayout pattern="%d{ISO8601} [%t] %-5p %c{1} - %m%n"/>
<Policies>
<SizeBasedTriggeringPolicy size="20 MB"/>
</Policies>
</RollingFile>
<!-- accesslog log appender -->
<RollingFile name="accesslog" fileName="./logs/access_log.log"
filePattern="'_'yy_ww'.txt'"
filePermissions="rw-rw-r--">
<PatternLayout pattern="%d{ISO8601} [%t] %-5p %c{1} - %m%n"/>
<Policies>
<SizeBasedTriggeringPolicy size="20 MB"/>
</Policies>
</RollingFile>
<!-- Special todo jobs log. Logib kõik genereeritud todo sõnumid -->
<RollingFile name="todolog" fileName="./logs/todo.log"
filePattern="'.'yyyy-MM-dd'.log'" ignoreExceptions="false"
filePermissions="rw-rw-r--">
<PatternLayout pattern="%d{ISO8601} [%t] %-5p %c{1} - %m%n"/>
<Policies>
<SizeBasedTriggeringPolicy size="20 MB"/>
</Policies>
</RollingFile>
<!-- Special redirections servlet log. Logib online_report servlet'i teated -->
<RollingFile name="redirectlog" fileName="./logs/redirect.log"
filePattern="'.'yyyy-MM-dd'.log'" ignoreExceptions="false"
filePermissions="rw-rw-r--">
<PatternLayout pattern="%d{ISO8601} [%t] %-5p %c{1} - %m%n"/>
<Policies>
<SizeBasedTriggeringPolicy size="20 MB"/>
</Policies>
</RollingFile>
<!-- Asynchronous appender. Writes the log, when it has the time. -->
<Async name="asyncRoot" bufferSize="10" includeLocation="true">
<AppenderRef ref="info"/>
<AppenderRef ref="error" level="error"/>
</Async>
</Appenders>
<Loggers>
<!-- additivity false - do not log to other appenders as defined here -->
<Logger name="TIMING" additivity="false" level="info">
<AppenderRef ref="timelog"/>
</Logger>
<!-- additivity false - do not log to other appenders as defined here -->
<Logger name="TODO" additivity="false" level="info">
<AppenderRef ref="todolog"/>
</Logger>
<!-- additivity false - do not log to other appenders as defined here -->
<Logger name="ACCESS" additivity="false" level="info">
<AppenderRef ref="accesslog"/>
</Logger>
<!-- additivity false - do not log to other appenders as defined here -->
<Logger name="REDIRECT" additivity="false" level="info">
<AppenderRef ref="redirectlog"/>
</Logger>
<!-- Level order (low to high): TRACE < DEBUG < INFO < WARN < ERROR < FATAL -->
<Root level="DEBUG" includeLocation="true">
<!--AppenderRef Medium ref Values must be defined earlier appenders-->
<AppenderRef ref="asyncRoot"/>
<!-- <AppenderRef ref="debug" level="debug"/>-->
</Root>
</Loggers>
</Configuration>

Related

Logs are added in old rotated file instead of new file

Below is my log4j2.xml file:
<?xml version="1.0" encoding="UTF-8"?>
<Configuration xmln="http //logging.apache.org/log4j/2.0/config">
<Appenders>
<RollingFile name="File" fileName="${path}/server.log"
filePattern="${path}/server.log.%d{yyyy-MM-dd-HH}-%i">
<PatternLayout>
<Pattern>%d %p %c [%t] %m%n</Pattern>
</PatternLayout>
<Policies>
<SizeBasedTriggeringPolicy size="1 MB"/>
</Policies>
<DefaultRolloverStrategy fileIndex="nomax" />
</RollingFile>
</Appenders>
<RollingFile name="ExceptionLogs" fileName="${path}/ExceptionLogs.log"
filePattern="${path}/ExceptionLogs.log.%d{yyyy-MM-dd-HH}-%i">
<PatternLayout>
<Pattern>%d %p %c [%t] %m%n</Pattern>
</PatternLayout>
<Policies>
<SizeBasedTriggeringPolicy size="1 MB"/>
</Policies>
<DefaultRolloverStrategy fileIndex="nomax" />
</RollingFile>
</Appenders>
<Loggers>
<Logger name="com.x.aop.exception">
<AppenderRef ref="ExceptionLogs"/>
</Logger>
<Root level="ERROR">
<AppenderRef ref="File"/>
</Root>
</Loggers>
</Configuration>
Let's say my server started at 3:00 PM and I have 5 log files
server.log
server.log.2022-01-19-15-1 (1 MB)
server.log.2022-01-19-15-2 (1 MB)
server.log.2022-01-19-15-3 (1 MB)
server.log.2022-01-19-15-4 (1 MB)
server.log.2022-01-19-15-5 (1 MB)
Some time it will add in older file created (like server.log.2022-01-19-15-2, server.log.2022-01-19-15-1) instead of latest file
Why logs are added in already rotated file instead of latest highest number log file?

ImmediateFlush not working in Log4j2

I am getting this error:
ERROR asyncRoot contains an invalid element or attribute "immediateFlush"
When I use immediateFlush attribute in appender in log4j2.xml.
<Appenders>
<Console name="STDOUT" target="SYSTEM_OUT">
<PatternLayout pattern="%d{dd-MM-yy HH:mm:ss} [$${ctx:instId} - $${ctx:userId}] %c.%M(%L)- %m%n" />
</Console>
<RollingFile name="csroot"
fileName="${cslogs}/cslog.log" filePattern="${cslogs}/$${date:yyyy-MM}/cslog-%d{yyyy-MM-dd}-%i.log">
<PatternLayout pattern="%d{dd-MM-yy HH:mm:ss} [$${ctx:instId} - $${ctx:userId}] %c.%M(%L)- %m%n" immediateFlush="false"/>
<Policies>
<SizeBasedTriggeringPolicy size="100 MB" />
</Policies>
<DefaultRolloverStrategy max="1000">
</DefaultRolloverStrategy>
</RollingFile>
</Appenders>
Try with this modification:
<RollingFile name="csroot"
fileName="${cslogs}/cslog.log" filePattern="${cslogs}/$${date:yyyy-MM}/cslog-%d{yyyy-MM-dd}-%i.log" immediateFlush="false">
<PatternLayout pattern="%d{dd-MM-yy HH:mm:ss} [$${ctx:instId} - $${ctx:userId}] %c.%M(%L)- %m%n"/>
<Policies>
<SizeBasedTriggeringPolicy size="100 MB" />
</Policies>
<DefaultRolloverStrategy max="1000">
</DefaultRolloverStrategy>
</RollingFile>
immediateFlush is not an attribute of PatternLayout.
I know its quite an old question but might help somebody.
RollingFile Appender Parameters points out the immediateFlush is the property of the appender. But you have it declared as an attribute for PatternLayout which is what is causing the error.
Sample RollingFile appender configuration:
<?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"
immediateFlush="false">
<PatternLayout>
<Pattern>%d %p %c{1.} [%t] %m%n</Pattern>
</PatternLayout>
<Policies>
<TimeBasedTriggeringPolicy/>
<SizeBasedTriggeringPolicy size="250 MB"/>
</Policies>
</RollingFile>
</Appenders>
<Loggers>
<Root level="error">
<AppenderRef ref="RollingFile"/>
</Root>
</Loggers>
</Configuration>

how to configure log4j2 webapplication

Am little new to web applications, recently I was in need to employ a logging mechanism and for that I choose Log4J2, I went through there guide, and downloaded required libraries. This is what so far I did.
1. Added following jars to web-inf/lib
-- log4j-core2.1.jar
-- log4j-api-2.1.jar
-- log4j-web-2.1.jar
2. Added below xml as, log4j2.xml in java/src directory
<?xml version="1.0" encoding="UTF-8"?>
<configuration name="NONPROD" status="OFF">
<Properties>
<Property name="log-path">logs</Property>
</Properties>
<Appenders>
<Console name="console-log" target="SYSTEM_OUT">
<!-- <pattern>[%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %c{1} - %msg%n -->
<!-- </pattern> -->
<PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n" />
</Console>
<RollingFile name="info-log" fileName="${log-path}/web-info.log"
filePattern="${log-path}/web-info-%d{yyyy-MM-dd}.log">
<PatternLayout>
<pattern>[%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %c{1} - %msg%n
</pattern>
</PatternLayout>
<Policies>
<TimeBasedTriggeringPolicy interval="1"
modulate="true" />
<OnStartupTriggeringPolicy />
<SizeBasedTriggeringPolicy size="20 MB" />
</Policies>
</RollingFile>
<RollingFile name="error-log" fileName="${log-path}/web-error.log"
filePattern="${log-path}/web-error-%d{yyyy-MM-dd}.log">
<PatternLayout>
<pattern>[%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %c{1} - %msg%n
</pattern>
</PatternLayout>
<Policies>
<TimeBasedTriggeringPolicy interval="1"
modulate="true" />
<OnStartupTriggeringPolicy />
<SizeBasedTriggeringPolicy size="20 MB" />
</Policies>
</RollingFile>
<RollingFile name="debug-log" fileName="${log-path}/web-debug.log"
filePattern="${log-path}/web-debug-%d{yyyy-MM-dd}.log">
<PatternLayout>
<pattern>[%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %c{1} - %msg%n
</pattern>
</PatternLayout>
<Policies>
<TimeBasedTriggeringPolicy interval="1"
modulate="true" />
<OnStartupTriggeringPolicy />
<SizeBasedTriggeringPolicy size="20 MB" />
</Policies>
</RollingFile>
<RollingFile name="trace-log" fileName="${log-path}/web-trace.log"
filePattern="${log-path}/web-trace-%d{yyyy-MM-dd}.log">
<PatternLayout>
<pattern>[%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %c{1} - %msg%n
</pattern>
</PatternLayout>
<Policies>
<TimeBasedTriggeringPolicy interval="1"
modulate="true" />
<OnStartupTriggeringPolicy />
<SizeBasedTriggeringPolicy size="20 MB" />
</Policies>
</RollingFile>
</Appenders>
<Loggers>
<Logger name="com.demo.web.log4j2.file" level="debug"
additivity="false">
<appender-ref ref="trace-log" level="trace" />
<appender-ref ref="error-log" level="error" />
<appender-ref ref="debug-log" level="debug" />
<appender-ref ref="info-log" level="info" />
</Logger>
<Logger name="com.demo.web.log4j2.console" level="all"
additivity="false">
<AppenderRef ref="console-log" />
</Logger>
<Root level="info" additivity="true">
<AppenderRef ref="console-log" />
</Root>
</Loggers>
</configuration>
3. third and last i wrote an small web service to test logging
#Path("/register")
public class Register {
private DataManager mManager;
private static final Logger LOG = LogManager.getLogger(Register.class);
public Register(){
mManager = DataManager.getInstance();
}
#GET
#Path("/{param}")
public Response getMsg(#PathParam("param") String msg) {
LOG.error("Not supported operations");
return Response.status(Response.Status.BAD_REQUEST)
.entity("Not supported").build();
}
But in console, upon trigger get request this is all i got printed.
INFO: Server startup in 35959 ms
Not supported operations // this is not in pattern i supplied in log4j2.xml
// this is my pattern
<PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n" />
I figure out this is probably due to some thing gone wrong, and am struggling to find the actual cause, being totally new to web programming, please help me out to configure my logger.
Thanks,
Techfist
Your servlet container is using some other logging framework which logs to console. Figure out which logging framework that is and use one of log4j2's bridge libraries to redirect output to your log4j2 setup.
Alright, I found the cause behind this, Issue was primarily happening due to two reasons.
First I was suppose to exclude log4j* pattern from jarsToSkip attribute in catilina property
Second, i had kept two log4j2.xml one inside web-inf other inside java/src, it was supposed to be only present at java/src. two files were not required.
Third, but not mandatory, before deployment just check if log4j2.xml is included inside web-inf/classes or not, if not then simply add it.
that's it, after following this issue was resolved now am getting proper logs.

Logging data to different log files using log4j2?

I am currently working on an application which will generate 2 different log files for different purposes. Since I am new to log4j2, I am unable to achieve it. Here is my configuration file (log4j2.xml) :
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="WARN">
<Properties>
<Property name="log-path">C:/Users/460681/Desktop/SourceFiles</Property>
</Properties>
<Appenders>
<Console name="console-log" target="SYSTEM_OUT">
<PatternLayout
pattern="[%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %c{1} - %msg%n" />
</Console>
<RollingFile name="info-log" fileName="${log-path}/SplunkOADC.log"
filePattern="${log-path}/SplunkOADC-%d{yyyy-MM-dd}.log">
<PatternLayout>
<pattern> %d{yyyy-MM-dd HH:mm:ss.SSS} - %msg%n
</pattern>
</PatternLayout>
<Policies>
<TimeBasedTriggeringPolicy interval="1"
modulate="true" />
</Policies>
<Filters>
<ThresholdFilter level="error" onMatch="DENY" onMismatch="NEUTRAL"/>
<ThresholdFilter level="trace" onMatch="DENY" onMismatch="NEUTRAL"/>
<ThresholdFilter level="info" onMatch="ACCEPT" onMismatch="DENY"/>
</Filters>
</RollingFile>
<RollingFile name="error-log" fileName="${log-path}/SplunkOADC-error.log"
filePattern="${log-path}/SplunkOADC-error-%d{yyyy-MM-dd}.log">
<PatternLayout>
<pattern>[%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %c{1} - %msg%n
</pattern>
</PatternLayout>
<Policies>
<TimeBasedTriggeringPolicy interval="1"
modulate="true" />
</Policies>
<Filters>
<ThresholdFilter level="error" onMatch="ACCEPT" onMismatch="DENY"/>
<ThresholdFilter level="trace" onMatch="DENY" onMismatch="NEUTRAL"/>
<ThresholdFilter level="info" onMatch="DENY" onMismatch="NEUTRAL"/>
</Filters>
</RollingFile>
<RollingFile name="trace-log" fileName="${log-path}/SplunkOADC-trace.log"
filePattern="${log-path}/SplunkOADC-trace-%d{yyyy-MM-dd}.log">
<PatternLayout>
<pattern>[%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %c{1} - %msg%n
</pattern>
</PatternLayout>
<Policies>
<TimeBasedTriggeringPolicy interval="1"
modulate="true" />
</Policies>
<Filters>
<ThresholdFilter level="error" onMatch="DENY" onMismatch="NEUTRAL"/>
<ThresholdFilter level="trace" onMatch="ACCEPT" onMismatch="DENY"/>
<ThresholdFilter level="info" onMatch="DENY" onMismatch="NEUTRAL"/>
</Filters>
</RollingFile>
</Appenders>
<Loggers>
<Root level="debug">
<AppenderRef ref="info-log" level="info"/>
<AppenderRef ref="trace-log" level="trace"/>
</Root>
</Loggers>
</Configuration>
I have tried filters but I'm not sure if it is the correct way. Here is my java method which is trying to log using log4j2
logger.entry("Enter The app");
String report_index_data =
"select REPORT_MODE, INDEX_ID from TABLE_NAME";
ResultSet rs = db.selectQuery(report_index_data, conn);
while(rs.next()){
logger.info("report_index_data =" +rs.getString("report_index_data"));
}
logger.exit();
Thanks!
Add a new logger to configuration.
<root level="debug">
<appender-ref ref="info-log" level="info"/>
</root>
<logger name="logger2">
<AppenderRef ref="trace-log" level="trace"/>
</logger>
First, note that if your root logger level is debug, then it will not send trace-level log events to any of its appenders; it will only debug level or higher log events to its appenders.
Second, your code uses logger.entry() and logger.exit(). For these methods to be useful you need to use a pattern layout with location patterns like %location or %method in the pattern. (E.g. [%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %c{1}.%M - %msg%n). This will display the name of the method you entered/exited. Be aware that calculating location information has some performance impact.

How does RollingFileAppender work with log4j2?

I'm use to RollingFileAppender on normal log4j. Now I'm switching to log4j2, and cannot get the appender to work.
The File appender below works as expected. But the logging file for RollingFile is never created. Why?
<?xml version="1.0" encoding="UTF-8"?>
<Configuration>
<Appenders>
<File name="FILE" fileName="c:/logs.log">
<PatternLayout pattern="%d %p %c: %m%n" />
</File>
<RollingFile name="ROLLING" fileName="c:/logsroll.log">
<PatternLayout pattern="%d %p %c: %m%n"/>
<Policies>
<TimeBasedTriggeringPolicy />
<SizeBasedTriggeringPolicy size="0.001 MB"/>
</Policies>
<DefaultRolloverStrategy max="10"/>
</RollingFile>
</Appenders>
<Loggers>
<Root level="info">
<AppenderRef ref="FILE" />
<AppenderRef ref="ROLLING" />
</Root>
</Loggers>
</Configuration>
The RollingFile tag is missing a filePattern attribute.
<RollingFile name="ROLLING"
fileName="c:/logsroll.log"
filePattern="c:/logsroll-%i.log">
I used log4j2 version 2.0, in some cases it throws error if you do not set any date in file pattern, in this case you can use some thing like below:
<RollingFile name="MyFile" fileName="d:/log/bsi/admin/total/totalLog.log"
filePattern="d:/log/totalLog-%d{MM-dd-yyyy}-%i.log">
<PatternLayout>
<Pattern>%d %p %c [%t] %m%n</Pattern>
</PatternLayout>
<Policies>
<TimeBasedTriggeringPolicy />
<SizeBasedTriggeringPolicy size="1 MB"/>
</Policies>
<DefaultRolloverStrategy max="2000"/>
</RollingFile>

Categories