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?

2

1 Answer 1

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

1 Comment

oh, may you give a simple example, please?

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.