2

Im trying to send different logs to different files using logback.

I have 2 appenders configured (Console, RollingFile) and i want all

  • INFO messages -> Console appender
  • TRACE messages -> RollingFile appender:

logback-spring.xml

<root level="error">
    <appender-ref ref="RollingFile" />
    <appender-ref ref="Console" />
</root>

<logger name="com.mypkg" level="trace" additivity="true">
    <appender-ref ref="RollingFile" />
</logger>

<logger name="com.mypkg" level="info" additivity="true">
    <appender-ref ref="Console" />
</logger>

The result of the above configuration has 2 problems :

  • all messages are duplicated (both appenders)
  • com.mypkg shows only INFO (not TRACE) ob both appenders

any idea what im doing wrong ? is there any default spring logback file the is somehow merged with this config in runtime (changing the additivity to false fix the duplication issue, but still no TRACE messages) ?

Thanks .

1 Answer 1

4

You can try logback filters. There is a filter called LevelFilter. The option to accept and ignore log level types is also available here.

Example :

<configuration>
  <appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender">
    <filter class="ch.qos.logback.classic.filter.LevelFilter">
      <level>INFO</level>
      <onMatch>ACCEPT</onMatch>
      <onMismatch>DENY</onMismatch>
    </filter>
    <encoder>
      <pattern>
        %-4relative [%thread] %-5level %logger{30} - %msg%n
      </pattern>
    </encoder>
  </appender>
  <root level="DEBUG">
    <appender-ref ref="CONSOLE" />
  </root>
</configuration>

More information is in the below logback documentation.

https://logback.qos.ch/manual/filters.html#levelFilter

Sign up to request clarification or add additional context in comments.

Comments

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.