Example: Custom Log4j 2 Configuration for Java

This topic only applies to self-managed Intelligent Advisor edition

The following is an example of a custom Log4j 2 Java configuration for Intelligent Advisor Java Batch Processor. You will notice that the logging in this example has been configured to:

  • Set the logging threshold of the MAIN category to "info" and log messages to a file named "case-main.log"
  • Set the logging threshold of the CASE_ERROR category to "error" and log messages to a file named "case-error.log"
  • Set the logging threshold of the CASE_WARN category to "warn" and log messages to a file named "case-warn.log"
<?xml version="1.0" encoding="UTF-8"?>
<Configuration>
    <Appenders>
        <Console name="console" target="SYSTEM_OUT">
            <PatternLayout pattern="%p %t %c - %m%n"/>
        </Console>

        <File name="case-main-log" fileName="case-main.log">
            <PatternLayout>
                <Pattern>%t - %m%n</Pattern>
            </PatternLayout>
        </File>

        <File name="case-error-log" fileName="case-error.log">
            <PatternLayout>
                <Pattern>%t - %m%n</Pattern>
            </PatternLayout>
        </File>

        <File name="case-warn-log" fileName="case-warn.log">
            <PatternLayout>
                <Pattern>%t - %m%n</Pattern>
            </PatternLayout>
        </File>
    </Appenders>

    <Loggers>
        <Logger name="MAIN" additivity="false" level="info">
            <AppenderRef ref="case-main-log"/>
        </Logger>

        <Logger name="CASE_ERROR" additivity="false" level="error">
            <AppenderRef ref="case-error-log"/>
        </Logger>

        <Logger name="CASE_WARNING" additivity="false" level="warn">
            <AppenderRef ref="case-warn-log"/>
        </Logger>

        <Root level="warn">
            <AppenderRef ref="console"/>
        </Root>
    </Loggers>
</Configuration>
        

To write debug level information to a file called case-debug.log, a new file appender could be declared as follows:

<File name="case-debug-log" fileName="case-debug.log">
    <PatternLayout>
        <Pattern>%t - %m%n</Pattern>
    </PatternLayout>
</File>

To configure the application to generate DEBUG level information and to write it to the file appender defined above, the root logger would be configured as follows:

<Root level="debug">
    <AppenderRef ref="case-debug-log"/>
</Root>