如何在 Dropwizard 中添加请求日志过滤器?

时间:2021-04-02 16:10:21

标签: java logging dropwizard

我想对日志请求进行过滤。我有很多这样的日志:

todoList.map(todo => 
    (<FormControlLabel
        control={
            <Checkbox
               onChange={(e) => changeStatus(e.target.checked,index)}
               color="primary"
               value={todo.status}
            />
        }
    />)
)

如何隐藏? Dropwizard 版本为 0.9.3。

1 个答案:

答案 0 :(得分:0)

从 1.2 版开始,您可以使用 logging filters

在旧版本中,您必须执行以下操作:

  1. RequestLogFactory 扩展它创建一个新类:

    package com.service;
    
    import ch.qos.logback.classic.Logger;
    import ch.qos.logback.classic.LoggerContext;
    import ch.qos.logback.classic.spi.ILoggingEvent;
    import ch.qos.logback.core.CoreConstants;
    import ch.qos.logback.core.LayoutBase;
    import ch.qos.logback.core.spi.AppenderAttachableImpl;
    import com.google.common.collect.ImmutableList;
    import io.dropwizard.jetty.RequestLogFactory;
    import io.dropwizard.jetty.Slf4jRequestLog;
    import io.dropwizard.logging.AppenderFactory;
    import io.dropwizard.logging.ConsoleAppenderFactory;
    import java.util.TimeZone;
    import javax.validation.Valid;
    import javax.validation.constraints.NotNull;
    import org.eclipse.jetty.server.Request;
    import org.eclipse.jetty.server.RequestLog;
    import org.eclipse.jetty.server.Response;
    import org.slf4j.LoggerFactory;
    
    public class CustomRequestLogFactory extends RequestLogFactory {
    
        private static final String SPAM_URI = "healthcheck";
    
        @NotNull
        private final TimeZone timeZone = TimeZone.getTimeZone("UTC");
    
        @Valid
        @NotNull
        private final ImmutableList<AppenderFactory> appenders = ImmutableList.of(
            new ConsoleAppenderFactory()
        );
    
        private static class RequestLogLayout extends LayoutBase<ILoggingEvent> {
    
            @Override
            public String doLayout(ILoggingEvent event) {
                return event.getFormattedMessage() + CoreConstants.LINE_SEPARATOR;
            }
        }
    
        private static class CustomRequestLog extends Slf4jRequestLog {
    
            public CustomRequestLog(AppenderAttachableImpl<ILoggingEvent> appenders, TimeZone timeZone) {
                super(appenders, timeZone);
            }
    
            @Override
            public void log(Request request, Response response) {
                if (request.getUri().getCompletePath().contains(SPAM_URI)) {
                    return;
                }
    
                super.log(request, response);
            }
        }
    
        public RequestLog build(String name) {
            final Logger logger = (Logger) LoggerFactory.getLogger("http.request");
            logger.setAdditive(false);
            final LoggerContext context = logger.getLoggerContext();
            final RequestLogLayout layout = new RequestLogLayout();
            layout.start();
            final AppenderAttachableImpl<ILoggingEvent> attachable =
         new AppenderAttachableImpl<>();
            for (AppenderFactory output : this.appenders) {
                attachable.addAppender(output.build(context, name, layout));
            }
            return new CustomRequestLog(attachable, timeZone);
        }
    }
    
  2. Create 创建新类并从 ServiceApplication 扩展:

    import com.vaultcouture.service.services.CustomRequestLogFactory;
    import io.dropwizard.server.DefaultServerFactory;
    import io.dropwizard.setup.Environment;
    
    /**
     * This class is necessary for filtering logging
     */
    public class ServiceApplicationCustom extends ServiceApplication {
    
        @Override
        public void run(ServiceConfiguration configuration, Environment environment) throws Exception {
            ((DefaultServerFactory) configuration.getServerFactory())
                    .setRequestLogFactory(new CustomRequestLogFactory());
    
            super.run(configuration, environment);
        }
    }
    
  3. 修复 main 方法:

    public static void main(String[] args) throws Exception {
        new ServiceApplicationCustom().run(args);
    }