具有Logback过滤器配置的Spring Boot不起作用

时间:2017-09-14 08:40:18

标签: spring logback

我有Spring Boot 2.0.0.M3版本和logback 1.2.3以及下一个配置文件:

<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>

但每当我启动我的应用程序时,它都会失败。我有下一个日志:

    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

我觉得EvaluatorFilter有什么问题我应该选择具体的并为它添加一些额外的依赖项吗?

2 个答案:

答案 0 :(得分:1)

这是你正在使用的表达方式吗?试试这个...

<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>

此评估程序过滤器将删除其消息包含字符串&#34; test&#34;的所有日志记录事件。

答案 1 :(得分:0)

是否需要使用logback的依赖项?

Logback dependencies page。特别是要让Bifunctor[Either].bimap(Right(1): Either[String, Int])(_.toUpperCase, _ + 1).println 工作,JaninoEventEvaluator及其依赖关系Janino是必需的。

我看到同样的问题,当这两个不在类路径中时,应用程序只是以退出代码1静默退出。