logbackspring-logbackrollbar

Filter event by message in Logback Spring Rollbar Appender


I have configured my Rollbar Appender in logback-spring.xml:

<appender name="Rollbar" class="com.rollbar.logback.RollbarAppender">
    <accessToken>${ROLLBAR_TOKEN}</accessToken>
    <environment>${SPRING_PROFILES_ACTIVE}</environment>
    <filter class="ch.qos.logback.classic.filter.ThresholdFilter">
        <level>ERROR</level>
    </filter>
</appender>

and activated it for required profile:

<!-- debug level for dev -->
<springProfile name="dev">
    <root level="debug">
        <appender-ref ref="Console"/>
        <appender-ref ref="Rollbar"/>
    </root>
</springProfile>

everything works fine, and I can receive errors in Rollbar except one issue. There are some errors that I do not want to see in Rollbar but I still may see them in console, e.g.:

io.netty.channel.unix.Errors$NativeIoException: readAddress(..) failed: Connection reset by peer

How I can filter such messages in the most convenient way?


Solution

  • The solution is to create you own implementation of ch.qos.logback.core.filter.Filter:

    public class RegexRollbarFilter extends Filter<ILoggingEvent> {
    
        private String regex;
        private boolean includeThrowableMessage = true;
    
        @Override
        public FilterReply decide(ILoggingEvent event) {
            if (!isStarted()) {
                return FilterReply.NEUTRAL;
            }
    
            IThrowableProxy throwableProxy = event.getThrowableProxy();
            if (event.getMessage()
                     .matches(regex) || includeThrowableMessage && throwableProxy != null && throwableProxy.getMessage()
                                                                                                           .matches(regex)) {
                return FilterReply.DENY;
            } else {
                return FilterReply.NEUTRAL;
            }
        }
    
        public void setRegex(String regex) {
            this.regex = regex;
        }
    
        public void setIncludeThrowableMessage(boolean includeThrowableMessage) {
            this.includeThrowableMessage = includeThrowableMessage;
        }
    
        public void start() {
            if (this.regex != null) {
                super.start();
            }
        }
    }
    

    and use it in the follwoing way:

     <appender name="Rollbar" class="com.rollbar.logback.RollbarAppender">
            <accessToken>${ROLLBAR_TOKEN}</accessToken>
            <environment>${SPRING_PROFILES_ACTIVE}</environment>
            <filter class="ch.qos.logback.classic.filter.ThresholdFilter">
                <level>ERROR</level>
            </filter>
            <filter class="your.package.RegexRollbarFilter">
                <regex>^.*An existing connection was forcibly closed by the remote host.*$</regex>
            </filter>
            <filter class="your.package.RegexRollbarFilter">
                <regex>(.|\n)*readAddress\(..\) failed: Connection reset by peer(.|\n)*$</regex>
            </filter>
        </appender>