为了减少我们应用程序中存在的日志记录量,我们决定了 根据每个客户端启用/禁用某些方法的记录。
public class LoggingFilter extends Filter<ILoggingEvent> {
@Autowired
private MethodNameValidator validator;
@Override
public FilterReply decide(ILoggingEvent event) {
Map<String, String> mdcProperties = event.getMDCPropertyMap();
if (mdcProperties.isEmpty()) {
return FilterReply.ACCEPT;
}
String accountId = mdcProperties.get(MDCUtil.ACCOUNT_ID);
String methodName = mdcProperties.get(MDCUtil.METHOD_NAME);
if (validator.isValidMethodName(accountId, methodName)) {
return FilterReply.ACCEPT;
}
return FilterReply.DENY;
}
}
上面定义的自定义过滤器有一个验证组件,其中实现了方法验证逻辑。
验证器是一个Spring Bean(通过JMX公开外部配置)。
我无法将MethodNameValidator bean注入过滤器。
过滤器也是一个bean。
有没有办法实现这个目标?
如果我可以动态设置一个logback过滤器 然后我可以将我的过滤器初始化为bean,获得所需的记录器 按名称并应用过滤器。
无法通过提供的Logback API了解如何操作。
答案 0 :(得分:1)
这适合我。
@Component
public class DiscoveringPostProcessor implements BeanPostProcessor, ApplicationContextAware {
ApplicationContext applicationContext;
@Override
public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
// TODO Auto-generated method stub
return bean;
}
@Override
public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
if(bean instanceof CustomLoggingFilter){
Map<String, TurboFilter> filterBeans = applicationContext.getBeansOfType(TurboFilter.class);
for (TurboFilter filter : filterBeans.values()) {
LoggerContext loggerContext = (LoggerContext) LoggerFactory.getILoggerFactory();
loggerContext.addTurboFilter(filter);
}
}
return bean;
}
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
this.applicationContext = applicationContext;
}
}
2
@Named("customLoggingFilter")
public class CustomLoggingFilter extends TurboFilter {
@Autowired
private TestService ts;
@Override
public FilterReply decide(Marker marker, Logger logger, Level level, String format, Object[] params, Throwable t) {
System.out.println(ts.doTest());
return FilterReply.ACCEPT;
}
}
答案 1 :(得分:0)
这就是我们如何钉它。 Turbo过滤器而不是事件过滤器。谢谢;)
//filter definition
public class MethodLoggingFilter extends TurboFilter {
private MethodNameValidator methodNameValidator ;
public MethodLoggingFilter(MethodNameValidator methodNameValidator) {
this.methodNameValidator = methodNameValidator;
}
@Override
public FilterReply decide(Marker marker, Logger logger, Level level, String format, Object[] params, Throwable t) {
return shouldApplyFilter(level, MDCUtils.getSmsChannelId(), MDCUtils.getMethodName()) ? FilterReply.DENY : FilterReply.NEUTRAL;
}
private boolean shouldApplyFilter(Level level, String accountId, String methodName) {
if (level == Level.WARN || level == Level.ERROR) {
return false;//warn and error are logged for any method
}
if (methodName == null) {
return false;
}
Integer accountId = accountId != null ? Integer.valueOf(accountId) : null;
return !methodNameValidator .isLoggingAllowed(accountId, methodName);
}
}
//configuration
@Bean
public MethodLoggingFilter methodLoggingFilter(MethodNameValidator methodNameValidator) {
MethodLoggingFilter filter = new MethodLoggingFilter(methodNameValidator);
LoggerContext loggerContext = (LoggerContext) LoggerFactory.getILoggerFactory();
loggerContext.addTurboFilter(filter);
return filter;
}