The Cat The Cat - 16 days ago 5
Java Question

file and stdout appenders in logback.xml

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

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>