Spring Batch MDC日志记录 [英] Spring Batch MDC Logging

查看:410
本文介绍了Spring Batch MDC日志记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道如何在Spring Batch中使用MCD记录Job NameExecution ID之类的东西.

以下是一些代码:

bootstrap.properties

此文件包含我当前记录的项目列表,并且我在此处添加了execId作为第三个元素.

logging.pattern.level=%5p [%X{X-B3-TraceId:-},%X{sessionId:-},%X{execId:-},%X{program:-},%X{mainframeId:-}]
spring.application.name=mcc
spring.profiles.active=globals,local,local-override

MCC应用程序

此文件具有我的主要方法.当我使用MDC.put("execId", "12345");在此处手动设置字段时,我会在日志中看到它,但是我不知道如何在此处放置所需的实际信息.

@SpringBootApplication(exclude = {SecurityAutoConfiguration.class, ManagementWebSecurityAutoConfiguration.class})
public class MccApplication {

    public static void main(String[] args) {
        MDC.put("execId", "12345");
        SpringApplication.run(MccApplication.class, args);
    }

}

我将不胜感激:)谢谢.

解决方案

免责声明:我对spring-batch不熟悉

MDC将适用于您记录作业名称和执行ID以及日志语句,因为MDC具有线程相似性.因此,如果您在线程中将某些内容放入MDC,那么在该线程的整个生命周期中,您将可以使用它们(并注销).因此,如果将MDC.put("execId", "12345");放入主方法中,则该方法仅在主线程中可用.

作业名称和执行ID仅在春季批处理可能产生的任何子线程中才有意义(由于我不熟悉春季批处理,因此我在这里使用的是疯狂的假设).因此,在实际Job的开始位置添加MDC.put("execId", "12345");,而不是在main()方法中添加.

I'm wanting to know how to log things such as the Job Name and Execution ID using MCD in Spring Batch.

Here's some code:

bootstrap.properties

this file has a list of items I currently log, and I've added execId as the 3rd element here.

logging.pattern.level=%5p [%X{X-B3-TraceId:-},%X{sessionId:-},%X{execId:-},%X{program:-},%X{mainframeId:-}]
spring.application.name=mcc
spring.profiles.active=globals,local,local-override

MCC Application

this file has my main method. When I manually set the field here with MDC.put("execId", "12345"); I see it in the log, but I don't understand how to put the actual information I need here.

@SpringBootApplication(exclude = {SecurityAutoConfiguration.class, ManagementWebSecurityAutoConfiguration.class})
public class MccApplication {

    public static void main(String[] args) {
        MDC.put("execId", "12345");
        SpringApplication.run(MccApplication.class, args);
    }

}

I would appreciate any insight :) Thank you.

解决方案

Disclaimer : I am not familiar with spring-batch

MDC will work for you to log Job Name and Execution ID along with the log statements because MDC has a thread affinity. So if you put something to MDC while in a thread, then that will be available to you (and to logback) through out the lifecycle of that thread. So if you put MDC.put("execId", "12345"); in your main method, that will only be available in the main thread.

The Job Name and Execution ID makes sense only in any child thread that the spring batch may spawn (I am running on wild assumptions here, as I am not familiar with spring batch). So add MDC.put("execId", "12345"); at the beginning point of your actual Job, not in the main() method.

这篇关于Spring Batch MDC日志记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆