Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 643
The Cat Avatar asked Feb 28 '13 16:02

The Cat


2 Answers

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>
like image 176
Jintian DENG Avatar answered Nov 19 '22 02:11

Jintian DENG


If you want view output in file and console use two appender-ref like 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" /> <!-- Every log will apear on screen log -->
        <appender-ref ref="FILE" />   <!-- Every log will apear on file -->
    </root>
</configuration>
like image 35
Edgard Leal Avatar answered Nov 19 '22 04:11

Edgard Leal