logback: Two appenders, multiple loggers, different levels

前端 未结 6 846
小蘑菇
小蘑菇 2020-12-02 07:46

I want to have two log files in my application (Spring Integration), debug.log and main.log. I want to run main.log at an INFO level and debug.log at a DEBUG level. This i

6条回答
  •  一整个雨季
    2020-12-02 08:20

    Create a ThresholdLoggerFilter class which can be put on an appender like:

    
        
            INFO
        
        
            org.springframework
            ERROR
        
        
    

    The following code works

    package com.myapp;
    
    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 ThresholdLoggerFilter extends Filter {
        private Level level;
        private String logger;
    
        @Override
        public FilterReply decide(ILoggingEvent event) {
            if (!isStarted()) {
                return FilterReply.NEUTRAL;
            }
    
            if (!event.getLoggerName().startsWith(logger))
                return FilterReply.NEUTRAL;
    
            if (event.getLevel().isGreaterOrEqual(level)) {
                return FilterReply.NEUTRAL;
            } else {
                return FilterReply.DENY;
            }
        }
    
        public void setLevel(Level level) {
            this.level = level;
        }
    
        public void setLogger(String logger) {
            this.logger = logger;
        }
    
        public void start() {
            if (this.level != null && this.logger != null) {
                super.start();
            }
        }
    }
    

提交回复
热议问题