抑制 spring 引导打印错误到 stderr
Suppress spring boot printing error to stderr
我的服务入口点有一个 try/catch/finally 块(从 SQS 轮询项目的 class)。
我想捕获从我的服务中抛出的每个异常并将其丢弃,以便 SQS 可以重新驱动它。
但是,这也会导致 spring 引导容器将堆栈跟踪打印到控制台,控制台又将其记录到我的日志聚合器中。
我已经有一个用于有意义日志记录的基础设施,它使用 MDC 将重要的元数据添加到每条日志消息中,因此我想在记录异常时使用它。
这是我的代码:
@SqsListener(value = "${src.queue.name}", deletionPolicy = SqsMessageDeletionPolicy.NO_REDRIVE)
public void consume(String msgJson) {
String awsKeyJson = JsonUtils.fromJson(msgJson).get("Message").asText();
String awsKey = JsonUtils.fromJson(awsKeyJson, AwsKey.class).getKey();
ScanResponseContainer src = s3.getResourceAsObject(SRC_BUCKET_NAME, awsKey, ScanResponseContainer.class);
mdcService.initializeMDC(src, awsKey);
logger.info("Received Scan Response Container from SQS");
try {
ScannedMerchant sm = scanResponseContainerService.handleScanResponseContainer(src);
producer.produce(sm);
logger.info("Successfully processed Scan Response Container");
} catch (Exception e) {
throw new RuntimeException("Error thrown when processing Scan Response Container", e);
} finally {
mdcService.clearMDC();
eventCollector.clear();
}
}
我的问题是 - 我可以添加到 catch 块:
logger.error(e);
它会用有意义的日志消息记录错误。
但是,它仍会记录“无意义”的消息,因为 spring 仍会将其打印到控制台,然后我会在我的日志聚合器中得到重复的消息。
是否有某种我可以使用的拦截器仍然会抛出异常但禁止将堆栈跟踪打印到 stderr?
您可以为 spring boot
包设置日志级别。对于属性文件,您可以将其设置为 OFF 还是 FATAL。
logging.level.org.springframework=OFF
如果您有 xml
记录器配置
<logger name="org.springframework" level="OFF" additivity="false">
<appender-ref ref="RollingFile" />
<appender-ref ref="Console" />
</logger>
Logback 更新:Spring Documentation
登录 Level
:Logback 级别关闭问题。
我的服务入口点有一个 try/catch/finally 块(从 SQS 轮询项目的 class)。
我想捕获从我的服务中抛出的每个异常并将其丢弃,以便 SQS 可以重新驱动它。
但是,这也会导致 spring 引导容器将堆栈跟踪打印到控制台,控制台又将其记录到我的日志聚合器中。
我已经有一个用于有意义日志记录的基础设施,它使用 MDC 将重要的元数据添加到每条日志消息中,因此我想在记录异常时使用它。
这是我的代码:
@SqsListener(value = "${src.queue.name}", deletionPolicy = SqsMessageDeletionPolicy.NO_REDRIVE)
public void consume(String msgJson) {
String awsKeyJson = JsonUtils.fromJson(msgJson).get("Message").asText();
String awsKey = JsonUtils.fromJson(awsKeyJson, AwsKey.class).getKey();
ScanResponseContainer src = s3.getResourceAsObject(SRC_BUCKET_NAME, awsKey, ScanResponseContainer.class);
mdcService.initializeMDC(src, awsKey);
logger.info("Received Scan Response Container from SQS");
try {
ScannedMerchant sm = scanResponseContainerService.handleScanResponseContainer(src);
producer.produce(sm);
logger.info("Successfully processed Scan Response Container");
} catch (Exception e) {
throw new RuntimeException("Error thrown when processing Scan Response Container", e);
} finally {
mdcService.clearMDC();
eventCollector.clear();
}
}
我的问题是 - 我可以添加到 catch 块:
logger.error(e);
它会用有意义的日志消息记录错误。
但是,它仍会记录“无意义”的消息,因为 spring 仍会将其打印到控制台,然后我会在我的日志聚合器中得到重复的消息。
是否有某种我可以使用的拦截器仍然会抛出异常但禁止将堆栈跟踪打印到 stderr?
您可以为 spring boot
包设置日志级别。对于属性文件,您可以将其设置为 OFF 还是 FATAL。
logging.level.org.springframework=OFF
如果您有 xml
记录器配置
<logger name="org.springframework" level="OFF" additivity="false">
<appender-ref ref="RollingFile" />
<appender-ref ref="Console" />
</logger>
Logback 更新:Spring Documentation
登录 Level