Orest
Orest

Reputation: 6748

Spring Boot with Logback filter configuration doesn't work

I have Spring Boot 2.0.0.M3 version and a logback 1.2.3 and next configurations file:

<configuration debug="true">
    <springProfile name="local">
            <springProperty name="springAppName" source="spring.application.name"/>
    <appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender">
        <encoder>
            <pattern>%green(%d{yyyy-MM-dd HH:mm:ss}) [${springAppName},%X{X-B3-TraceId:-},%X{X-B3-SpanId:-},%X{X-Span-Export:-}] %highlight(%-5level) %cyan(%logger{15}) %m%n</pattern>
            <charset>utf8</charset>
        </encoder>
        <filter class="ch.qos.logback.core.filter.EvaluatorFilter">
            <evaluator>
                <expression>logger.contains("test")</expression>
            </evaluator>
            <onMatch>DENY</onMatch>
        </filter>
    </appender>
    <root level="info">
        <appender-ref ref="CONSOLE"/>
    </root>
    </springProfile>
</configuration>

But whenever I start my application it fails. I have next logs:

    11:35:16,891 |-WARN in Logger[org.springframework.core.env.PropertySourcesPropertyResolver] - No appenders present in context [default] for logger [org.springframework.core.env.PropertySourcesPropertyResolver].
    11:35:16,891 |-INFO in ch.qos.logback.core.joran.action.AppenderAction - About to instantiate appender of type [ch.qos.logback.core.ConsoleAppender]
    11:35:16,891 |-INFO in ch.qos.logback.core.joran.action.AppenderAction - Naming appender as [CONSOLE]
    11:35:16,896 |-INFO in ch.qos.logback.core.joran.action.NestedComplexPropertyIA - Assuming default type [ch.qos.logback.classic.encoder.PatternLayoutEncoder] for [encoder] property
    11:35:16,930 |-INFO in ch.qos.logback.core.joran.action.NestedComplexPropertyIA - Assuming default type [ch.qos.logback.classic.boolex.JaninoEventEvaluator] for [evaluator] property
    Process finished with exit code 1

I guess something is wrong with EvaluatorFilter should I choose specific one and add some additional dependencies for it?

Upvotes: 4

Views: 3756

Answers (2)

Miika Karanki
Miika Karanki

Reputation: 121

Are the required dependencies of logback in place?

See Logback dependencies page. Especially, to get JaninoEventEvaluator to work, Janino and its dependency commons-compiler are required.

I saw the same problem that when those two are not in the classpath, the application just silently exits with exit code 1.

Upvotes: 4

Hasson
Hasson

Reputation: 1914

Is it the expression you are using? try this ...

<filter class="ch.qos.logback.core.filter.EvaluatorFilter">      
  <evaluator> <!-- defaults to type ch.qos.logback.classic.boolex.JaninoEventEvaluator -->
    <expression>return formattedMessage.contains("test");</expression>
  </evaluator>
  <OnMismatch>NEUTRAL</OnMismatch>
  <OnMatch>DENY</OnMatch>
</filter>

this evaluator filter will drop all logging events whose message contains the string "test".

Upvotes: 1

Related Questions