Reputation: 1
I'm new to using log4j 2
. I just started, and prepared the following log4j2.xml
configuration file:
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="INFO">
<Appenders>
<Console name="Console" target="SYSTEM_OUT">
<PatternLayout pattern="%msg%n" />
</Console>
<File name="MyFile" fileName="manager.log" immediateFlush="true" append="false">
<PatternLayout pattern="%d{yyy-MM-dd HH:mm:ss} [%t] %-5level %logger{36} - %msg%n"/>
</File>
</Appenders>
<Loggers>
<Root level="debug">
<AppenderRef ref="Console" />
<AppenderRef ref="MyFile"/>
</Root>
</Loggers>
</Configuration>
What is the default logging behavior and file size in my xml? Is it rolling file, or once per day, or just a single huge file that grows a default size?!
If not, how can I change it to 2 rolling files with max of 10mb?
Upvotes: 2
Views: 1432
Reputation: 36844
The File appender doesn’t have rollover behavior, it just appends to the specified file. When append = "false"
, it will overwrite any existing file when the application is restarted.
The Rolling File Appender is probably what you’re looking for.
The manual has many examples, but this may be close to what you have in mind:
1 <?xml version="1.0" encoding="UTF-8"?>
2 <Configuration status="warn" name="MyApp" packages="">
3 <Appenders>
4 <RollingFile name="RollingFile" fileName="logs/app.log"
5 filePattern="logs/$${date:yyyy-MM}/app-%d{MM-dd-yyyy}-%i.log.gz">
6 <PatternLayout>
7 <Pattern>%d %p %c{1.} [%t] %m%n</Pattern>
8 </PatternLayout>
9 <Policies>
10 <TimeBasedTriggeringPolicy />
11 <SizeBasedTriggeringPolicy size="10 MB"/>
12 </Policies>
13 <DefaultRolloverStrategy max="2"/>
14 </RollingFile>
15 </Appenders>
16 <Loggers>
17 <Root level="error">
18 <AppenderRef ref="RollingFile"/>
19 </Root>
20 </Loggers>
21 </Configuration>
Upvotes: 1