[英]Spring batch exception remove file content
我正在使用 Spring 批处理作业将文件加载到数据库中。 该文件包含敏感的客户数据。 在任何解析错误时,Spring 批处理会记录异常以及文件行内容。
行解析错误:1 in resource=[file [/path/file.txt]], input=[sensitive data]
我想避免那些有权复制粘贴日志的人与未经授权使用此特定数据的各方的通信。 因此,我使用application.properties
设置禁用了 Spring 批处理日志
logging.level.org.springframework.batch: OFF
但是我仍然需要记录文件名、行号和错误原因。 在StepExecutionListener::afterStep
中想到了类似下面的内容,但这可能不是万无一失/面向未来的。
stepExecution.getFailureExceptions()
.forEach(e -> log.error(e.getMessage().replaceAll("[, ]*input=\\[.*?]", "") + " - " + e.getCause()));
我可以尝试哪些其他方法? 是否有设置指示 Spring 批处理屏蔽/排除异常消息中的文件内容?
我不会在全局级别禁用日志,因为应用程序的其他地方可能需要这样做。
您正在寻找的扩展点是ExceptionHandler API。 这是您可以在块处理的任何阶段自定义异常处理的地方。 在您的情况下,您可以捕获平面文件解析异常并对它们进行任何您需要的操作(将敏感数据记录到安全源,通过删除敏感数据来自定义日志等)。
这是一个简单的例子:
class MyCustomExceptionHandler implements ExceptionHandler {
@Override
public void handleException(RepeatContext repeatContext, Throwable throwable) {
if (throwable instanceof FlatFileParseException) {
FlatFileParseException exception = (FlatFileParseException) throwable;
// customize the exception handling as needed
System.err.println("Error while parsing line " + exception.getLineNumber());
}
}
}
一旦到位,您可以在步骤中使用org.springframework.batch.core.step.builder.AbstractTaskletStepBuilder#exceptionHandler
注册您的异常处理程序,例如:
@Bean
public Step myStep(StepBuilderFactory stepBuilderFactory) {
return stepBuilderFactory.get("myStep")
.<Person, Person>chunk(2)
.reader(itemReader())
.writer(itemWriter())
.exceptionHandler(new MyCustomExceptionHandler())
.build();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.