log4j2(二) 自定义filter-ThreadFilter根据线程名过滤日志

x33g5p2x  于2021-12-25 转载在 其他  
字(3.2k)|赞(0)|评价(0)|浏览(677)

背景

项目最新接入了一个sdk,但是这个SDK打印了很多无关紧要的DEBUG日志,造成了严重的信息干扰,现在想去掉这个SDK中指定线程的DEBUG日志

解决方案

使用Log4j2的插件扩展功能自定义一个插件-ThreadFilter, 参考链接中的Filter部分

代码示例

相关的解释都已经放在了代码和配置中, 注意查看注释

Java代码

  1. package com.test.filter;
  2. import org.apache.commons.lang3.StringUtils;
  3. import org.apache.logging.log4j.Level;
  4. import org.apache.logging.log4j.Marker;
  5. import org.apache.logging.log4j.core.Filter;
  6. import org.apache.logging.log4j.core.LogEvent;
  7. import org.apache.logging.log4j.core.Logger;
  8. import org.apache.logging.log4j.core.config.Node;
  9. import org.apache.logging.log4j.core.config.plugins.Plugin;
  10. import org.apache.logging.log4j.core.config.plugins.PluginAttribute;
  11. import org.apache.logging.log4j.core.config.plugins.PluginFactory;
  12. import org.apache.logging.log4j.core.filter.AbstractFilter;
  13. /** * filter log by thread name and log level * * @author Keven * @version 1.0 * date: 2020-02-26 */
  14. //声明插件的类型,名字等信息
  15. @Plugin(name = "ThreadFilter", category = Node.CATEGORY, elementType = Filter.ELEMENT_TYPE, printObject = true)
  16. public class ThreadFilter extends AbstractFilter {
  17. private String threadName;
  18. private Result onMatch;
  19. private Result onMisMatch;
  20. private Level level;
  21. public ThreadFilter(String threadName, Result onMatch, Result onMisMatch, Level level) {
  22. super();
  23. this.threadName = threadName;
  24. this.onMatch = onMatch;
  25. this.onMisMatch = onMisMatch;
  26. this.level = level;
  27. }
  28. /** * Create a ThreadFilter instance. * * @param threadName The thread name * @param match The action to take on a match. * @param mismatch The action to take on a mismatch. * @param level log level * @return The created MyCustomFilter. */
  29. //@PluginFactory注解对应的必须使用一个静态的方法,传入的参数用@PluginAttribute修饰
  30. @PluginFactory
  31. public static ThreadFilter createFilter(@PluginAttribute("threadName") final String threadName,
  32. @PluginAttribute("onMatch") final Result match,
  33. @PluginAttribute("onMismatch") final Result mismatch, @PluginAttribute("level") final Level level) {
  34. assert StringUtils.isNotBlank(threadName);
  35. final Result onMatch = match == null ? Result.NEUTRAL : match;
  36. final Result onMismatch = mismatch == null ? Result.DENY : mismatch;
  37. return new ThreadFilter(threadName, onMatch, onMismatch, level);
  38. }
  39. @Override
  40. public Result filter(LogEvent event) {
  41. //你的业务逻辑,这里是根据线程前缀名,以及日志等级来过滤
  42. if (event.getLevel().name().equals(this.level.name()) && event.getThreadName().startsWith(threadName)) {
  43. return this.onMatch;
  44. }
  45. return this.onMisMatch;
  46. }
  47. @Override
  48. public Result filter(Logger logger, Level level, Marker marker, Object msg, Throwable t) {
  49. return super.filter(logger, level, marker, msg, t);
  50. }
  51. }

使用

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!--注意这里packages填写的是filter所对应的包名,必不可少-->
  3. <Configuration status="warn" name="MyApp" packages="com.test.filter">
  4. <Appenders>
  5. <RollingFile name="RollingFile" fileName="logs/app.log" filePattern="logs/app-%d{MM-dd-yyyy}.log.gz">
  6. <!--此处的参数名称于@PluginFactory所对应的方法中的参数名称对应-->
  7. <ThreadFilter threadName="XXX-Thread" onMatch="DENY" onMismatch="ACCEPT" level="DEBUG"/>
  8. <PatternLayout>
  9. <pattern>%d %p %c{1.} [%t] %m%n</pattern>
  10. </PatternLayout>
  11. <TimeBasedTriggeringPolicy />
  12. </RollingFile>
  13. </Appenders>
  14. <Loggers>
  15. <Root level="error">
  16. <AppenderRef ref="RollingFile"/>
  17. </Root>
  18. </Loggers>
  19. </Configuration>

相关文章