3

I need to log all info to info.log, all warn to warn.log etc and all my logs to all.log. I've been create a log4j2.xml configuration.

<?xml version="1.0" encoding="UTF-8"?>
    <configuration monitorInterval = "3" status = "info">
        <appenders>
            <File name="ALL_LOG" fileName="logs/all.log">
                <PatternLayout pattern="%d{ISO8601} [%-5p] (%F:%L) - %m%n"/>
            </File>
            <File name="FILE_ERROR" fileName="logs/error.log">
                <PatternLayout pattern="%d{ISO8601} [%-5p] (%F:%L) - %m%n"/>
            </File>
            <File name="FILE_INFO" fileName="logs/info.log">
                <PatternLayout pattern="%d{ISO8601} [%-5p] (%F:%L) - %m%n"/>
            </File>
            <File name="FILE_DEBUG" fileName="logs/debug.log">
                <PatternLayout pattern="%d{ISO8601} [%-5p] (%F:%L) - %m%n"/>
            </File>
            <File name="FILE_WARN" fileName="logs/warn.log">
                <PatternLayout pattern="%d{ISO8601} [%-5p] (%F:%L) - %m%n"/>
            </File>
            <Console name="STDOUT" target="SYSTEM_OUT.log">
                <PatternLayout pattern="%d{ABSOLUTE} [%-5p] (%F:%L) - %m%n"/>
            </Console>
        </appenders>
        <loggers>
            <root>
                <appender-ref ref="ALL_LOG"/>
            </root>
            <root level="error">
                <appender-ref ref="STDOUT"/>
                <appender-ref ref="FILE_ERROR"/>
            </root>
            <root level="info">
                <appender-ref ref="FILE_INFO"/>
            </root>
            <root level="debug">
                <appender-ref ref="FILE_DEBUG"/>
            </root>
            <root level="warn">
                <appender-ref ref="FILE_WARN"/>
            </root>
        </loggers>
    </configuration>

In my programm i define logger:

public class Main {

public static Logger LOG = Logger.getLogger(Main.class);

public static void main(String[] args) {
    LOG.debug("Debug message");
    LOG.warn("Warning message");
    LOG.info("Info message");
}

But only in file warn.log i have Warning message, other files create, but empty. What i doing wrong?

Remko Popma
  • 35,130
  • 11
  • 92
  • 114
Arr
  • 749
  • 3
  • 6
  • 15
  • This question is duplicated. Please have a look : http://stackoverflow.com/questions/1839647/how-to-configure-log4j-to-log-different-log-levels-to-different-files-for-the-sa http://stackoverflow.com/questions/728295/creating-multiple-log-files-of-different-content-with-log4j – Vargan Apr 27 '16 at 10:51
  • Vargan, the answers you specified are for Log4j 1.x. This question is for Log4j 2. – Remko Popma Apr 28 '16 at 03:37

1 Answers1

3

You can only have one logger in the configuration. On that root logger, define multiple Appender-Refs, one for each File appender.

  1. For each Appender-Ref, specify the level, to filter out too detailed events:

<Appender-ref ref="InfoFile" level="info" />

  1. Then, on each File Appender, have a filter that DENIES events that are not exactly the desired level. This filters out events that are too general. See this question for an example.
Community
  • 1
  • 1
Remko Popma
  • 35,130
  • 11
  • 92
  • 114