file and stdout appenders in logback.xml

The Cat picture The Cat · Feb 28, 2013 · Viewed 52.8k times · Source

I have 2 logback.xml files in separate projects. One logs JDBC SQL to a file using a FileAppender and the other logs errors to stdout using a ConsoleAppender.

Here are my logback.xml files for each.

File logging

<configuration>
    <appender name="FILE" class="ch.qos.logback.core.FileAppender">
        <file>${user.home}/database.log</file>
        <append>false</append>
        <encoder>
            <pattern>%-5relative %-5level %logger{35} - %msg%n</pattern>
        </encoder>
    </appender>

    <logger name="jdbc" level="OFF" />
    <logger name="jdbc.sqlonly" level="debug" />

    <root level="info">
        <appender-ref ref="FILE" />
    </root>
</configuration>

Console logging

<configuration>
    <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
        <encoder>
            <pattern>%-5relative %-5level %logger{35} - %msg%n</pattern>
        </encoder>
    </appender>

    <logger name="jdbc" level="OFF" />

    <root level="ERROR">
        <appender-ref ref="STDOUT" />
    </root>
</configuration>

I am merging these two projects and I want to be able to continue logging the info level SQL to the file and the errors to STDOUT.

I have added the two appenders to the logback.xml but I am not sure what to do with loggers and the root tags in my new logback.xml.

Answer

Jintian DENG picture Jintian DENG · Mar 1, 2013

Try this:

    <configuration>
        <appender name="FILE" class="ch.qos.logback.core.FileAppender">
            <file>${user.home}/database.log</file>
            <append>false</append>
            <encoder>
                <pattern>%-5relative %-5level %logger{35} - %msg%n</pattern>
            </encoder>
        </appender>

        <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
            <encoder>
                <pattern>%-5relative %-5level %logger{35} - %msg%n</pattern>
            </encoder>
        </appender>


        <logger name="jdbc" level="OFF" />
        <logger name="jdbc.sqlonly" level="INFO" additivity="false">
            <appender-ref ref="FILE" />
        </logger>

        <root level="ERROR">
            <appender-ref ref="STDOUT" />
        </root>
    </configuration>