本文介绍了如何关闭一个URL log4j的日志记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个关于dropwizrd的旧版本的项目(0.9.3,其中没有任何日志过滤器( https://www.dropwizard.io/en/release-0.9.x/manual/core.html#logging )),我想禁用一个网址的日志

I have project on old version of dropwizrd (0.9.3 where is no any log filter(https://www.dropwizard.io/en/release-0.9.x/manual/core.html#logging)) and I want to disable log for one url.

我尝试使用它:

package com.pack.service.services;

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


public class LogFilter extends Filter<ILoggingEvent> {

  final static String SPAM_URL = "spam";

  @Override
  public FilterReply decide(ILoggingEvent event) {
    if (!event.getMessage().contains(SPAM_URL)) {
      return FilterReply.ACCEPT;
    } else {
      return FilterReply.NEUTRAL;
    }
  }
}

在log4j2.xml文件的文件夹资源中,我添加了它:

And in folder resources in log4j2.xml file I added it:

<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="ERROR" >
    <Appenders>

        <Filter class="com.pack.service.services.LogFilter"/>

        <Console name="console" target="SYSTEM_OUT">
            <PatternLayout pattern="[%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %c{1} - %msg%n" />
        </Console>
    </Appenders>
    <Loggers>
        <Root level="error" additivity="false">
            <AppenderRef ref="console" />
        </Root>
    </Loggers>
</Configuration>

但是我得到了错误:

2021-04-01 16:26:33,473主要错误Appenders包含无效的元素或属性"filter"

2021-04-01 16:26:33,473 main ERROR Appenders contains an invalid element or attribute "filter"

推荐答案

您创建了Logback过滤器,并尝试在Log4j 2中使用它.它们是完全不同的框架.您可以查看 MarkerFilter 作为如何为Log4j 2创建过滤器的示例.

You created a Filter for Logback and tried to use it in Log4j 2. They are completely different frameworks. You can look at the MarkerFilter as an example of how to create a Filter for Log4j 2.

我还应该指出,您对过滤器的配置是错误的.Log4j使用插件系统,因此您永远不必在配置中指定类名.

I should also point out that your configuration of the filter would be wrong. Log4j uses a Plugin system so you never specify class names in the configuration.

话虽如此,您无需创建自己的过滤器.只需使用Log4j的 RegexFilter .或者,如果您想复制代码,则可以使用 ScriptFilter 并用任何支持JSR 223的脚本语言(例如Groovy)编写等效代码.

Having said that, you don't need to create your own filter. Just use Log4j's RegexFilter. Or if you want to duplicate your code you could use the ScriptFilter and write the equivalent code in any Scripting language that supports JSR 223 such as Groovy.

这篇关于如何关闭一个URL log4j的日志记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-25 00:47