Logback过滤器不会过滤消息

时间:2018-01-28 00:32:17

标签: filter netty logback

我对以下过滤器进行了以下设置,希望过滤某些消息。然而,无论如何,这些消息都会找到通往日志的路径 实际上,错误日志仅包含过滤器中指定的WARN或ERROR消息,但其他过滤不起作用。

我无法弄清楚发生了什么事?

myFunction(e){
var Name = e.values[1];
DocumentApp.create("Test");
DocumentApp.appendParagraph("hello" + Name);
DocumentApp.saveAndClose;
}

日志过滤器类:

DocumentApp....appendParagragh("dear " + Name + "\n\n I like your " + Product + ". Are you interested in selling?")

无论如何,日志已满#34;连接由同行重置"和#34;破管"消息,如:

<appender name="ROOT_ERROR_FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
    <file>${LOG_HOME}/main-error.log</file>
    <filter class="logging.LogbackCustomFilter" />
    <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
      <!-- daily rollover -->
      <fileNamePattern>${LOG_HOME}/main-error-%d{yyyy-MM-dd}.%i.log</fileNamePattern>
      <timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
          <!-- or whenever the file size reaches 1GB -->
        <maxFileSize>1GB</maxFileSize>
      </timeBasedFileNamingAndTriggeringPolicy>
      <!-- keep 7 days' worth of history -->
      <maxHistory>7</maxHistory>
    </rollingPolicy>
    <encoder>
        <pattern>%date %-5level [%thread] - [%logger] - %msg%n</pattern>
    </encoder>
</appender>

<root level="DEBUG">
    <appender-ref ref="ROOT_FILE" />
    <appender-ref ref="ROOT_ERROR_FILE" />
</root>

或者这个:

package logging;

import ch.qos.logback.classic.Level;
import ch.qos.logback.classic.spi.ILoggingEvent;
import ch.qos.logback.core.filter.Filter;
import ch.qos.logback.core.spi.FilterReply;

public class LogbackCustomFilter extends Filter<ILoggingEvent> {

    private static final String closedChannelException = "ClosedChannelException";

    private static final String establishedConnectionWasAborted = "An established connection was aborted";

    private static final String connectionResetByPeer = "Connection reset by peer";

    private static final String brokenPipe = "Broken pipe";

    @Override
    public FilterReply decide(ILoggingEvent event) {
        if (event.getLevel().isGreaterOrEqual(Level.WARN)) {
            if (event.getMessage().contains(closedChannelException) ||
                    event.getMessage().contains(establishedConnectionWasAborted) ||
                    event.getMessage().contains(connectionResetByPeer) ||
                    event.getMessage().contains(brokenPipe)) {
                return FilterReply.DENY;
            } else {
                return FilterReply.NEUTRAL;
            }
        } else {
            return FilterReply.DENY;
        }
    }
}

1 个答案:

答案 0 :(得分:4)

在示例消息中,event.getMessage()将仅返回第一个记录的行(例如"An exceptionCaught() event was fired...""channel unhandled exception")。

要深入查看有关以下异常的消息,请尝试使用event.getThrowableProxy().getMessage()

当然通常也没有相关的例外 - 您还需要允许event.getThrowableProxy()为空。

相关问题