2

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?

2
  • What research have you done? Have you read the log4j2 manual? Commented Jan 9, 2019 at 5:24
  • Yes. This code above is my research outcome! Commented Jan 9, 2019 at 15:38

1 Answer 1

1

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>
Sign up to request clarification or add additional context in comments.

6 Comments

So how big in size will the default file (my code) get?
There’s no limit, depends on how much your application logs. If the application is never restarted it may run out of disk space eventually.
oh I see! Then I have to switch to your code! Thanks.
One question: Why there is still a parent app.log?! I get this as well as the two rolling files.
The app.log file is what Log4j is currently writing to. If a rollover condition is met, Log4j will “roll over” the current file (rename, move to other directory and compress it). It will then create a new “current log file” to append log events to.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.