Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 用于屏蔽特定日志数据的Logback配置_Java_Spring_Logging_Logback - Fatal编程技术网

Java 用于屏蔽特定日志数据的Logback配置

Java 用于屏蔽特定日志数据的Logback配置,java,spring,logging,logback,Java,Spring,Logging,Logback,我有一个SpringBootWeb应用程序,正在使用logback作为我的日志解决方案。我一直在查看文档,找不到一种简单或“正确”的方法来掩盖私人/特定数据(个人信息、信用卡等) 我所能找到的最接近的是Logback过滤器,但是这些过滤器的用例似乎更多地是关于忽略符合特定条件的日志,我只是想屏蔽所有应用程序范围的日志 这似乎是一个基本的问题,我肯定我错过了一些超基本的东西,但任何朝着正确方向的努力都是非常感谢的 我也没有被锁定在logback中,因此如果使用log4j2有一种更简单/更好的方法来

我有一个SpringBootWeb应用程序,正在使用logback作为我的日志解决方案。我一直在查看文档,找不到一种简单或“正确”的方法来掩盖私人/特定数据(个人信息、信用卡等)

我所能找到的最接近的是Logback过滤器,但是这些过滤器的用例似乎更多地是关于忽略符合特定条件的日志,我只是想屏蔽所有应用程序范围的日志

这似乎是一个基本的问题,我肯定我错过了一些超基本的东西,但任何朝着正确方向的努力都是非常感谢的


我也没有被锁定在logback中,因此如果使用log4j2有一种更简单/更好的方法来实现这一点,例如,我洗耳恭听来屏蔽可配置字段,您需要创建
屏蔽模式布局
,如下所示

import java.util.Arrays;
import java.util.List;
import java.util.Optional;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import ch.qos.logback.classic.PatternLayout;
import ch.qos.logback.classic.spi.ILoggingEvent;

public class MaskingPatternLayout extends PatternLayout {

  private String patternsProperty;
  private Optional<Pattern> pattern;

  public String getPatternsProperty() {
    return patternsProperty;
  }

  public void setPatternsProperty(String patternsProperty) {
    this.patternsProperty = patternsProperty;
    if (this.patternsProperty != null) {
      this.pattern = Optional.of(Pattern.compile(patternsProperty, Pattern.MULTILINE));
    } else {
      this.pattern = Optional.empty();
    }
  }

  @Override
  public String doLayout(ILoggingEvent event) {
    final StringBuilder message = new StringBuilder(super.doLayout(event));

    if (pattern.isPresent()) {
      Matcher matcher = pattern.get().matcher(message);
      while (matcher.find()) {

        int group = 1;
        while (group <= matcher.groupCount()) {
          if (matcher.group(group) != null) {
            final int startGrpIndex = matcher.start(group);
            final int endGrpIndex = matcher.end(group);
            final int diff = endGrpIndex - startGrpIndex + 1;
            int startIndex = startGrpIndex + diff;
            final int endIndex1 = message.indexOf(",", startIndex);
            final int endIndex2 = message.indexOf(" ", startIndex);
            final int endIndex3 = message.indexOf(")", startIndex);
            final int endIndex4 = message.indexOf("\n", startIndex);

            final Integer endIndex = getSmallestInt(
              Arrays.asList(Integer.valueOf(endIndex1), Integer.valueOf(endIndex2), Integer.valueOf(endIndex3), Integer.valueOf(endIndex4)));
            if (endIndex == null || endIndex <= 0) {
              continue;
            }

            for (int i = startIndex; i < endIndex; i++) {
              message.setCharAt(i, '*');
            }
          }
          group++;
        }
      }
    }
    return message.toString();
  }

  private Integer getSmallestInt(List<Integer> integerList) {

    return integerList.stream().filter(integer -> integer > 0).reduce((x, y) -> x < y ? x : y).get();
  }

}

在日志中,上述语句将显示为

Received sign-up request, password=*****************

相关:谢谢,这非常有帮助。掩蔽具有数组的对象如何?companyName=test private company,eventType=new,loginIds=[13345667954554549554],
log.info("Received sign-up request, password=DummyPassword@123");
Received sign-up request, password=*****************