当前位置: 首页 > 知识库问答 >
问题:

将spring boot个人资料添加到Sleuth/Zipkin日志

刘建中
2023-03-14

我正在使用这些依赖项:

compile 'org.springframework.cloud:spring-cloud-starter-zipkin'
compile 'org.springframework.cloud:spring-cloud-starter-sleuth'
compile 'org.springframework.cloud:spring-cloud-sleuth-zipkin'

是否有可能将当前活动配置文件添加到每条日志行?这样就可以根据splunk/elk/...

所以不是

2017-03-13 13:38:30.465  INFO [app,,,] 19220 --- [           main] com.company.app.Application    : Started Application in 20.682 seconds (JVM running for 22.166)

它应该记录下来

2017-03-13 13:38:30.465  INFO [app,,,] [dev] 19220 --- [           main] com.company.app.Application    : Started Application in 20.682 seconds (JVM running for 22.166)

编辑:基于Marcin的回答,我实现如下:

application.yml

logging:
  pattern:
    level: "%X{profiles} %5p"

ProfileLogger.java

public class ProfileLogger implements SpanLogger {

    private final Environment environment;
    private final Logger log;
    private final Pattern nameSkipPattern;

    @Autowired
    public ProfileLogger(String nameSkipPattern, final Environment environment) {
        this.nameSkipPattern = Pattern.compile(nameSkipPattern);
        this.environment = environment;
        this.log = org.slf4j.LoggerFactory.getLogger(this.getClass());
    }

    private void setProfiles() {
        MDC.put("profiles", Arrays.toString(environment.getActiveProfiles()));
    }

    @Override
    public void logStartedSpan(Span parent, Span span) {
        setProfiles();
        ...
    }
    ... // (as https://github.com/spring-cloud/spring-cloud-sleuth/blob/master/spring-cloud-sleuth-core/src/main/java/org/springframework/cloud/sleuth/log/Slf4jSpanLogger.java)
}

logconfig.java

@Configuration
public class LogConfig {

    private final Environment environment;

    @Autowired
    public LogConfig(final Environment environment) {
        this.environment = environment;
    }

    @Bean
    SpanLogger getLogger() {
        return new ProfileLogger("", environment);
    }
}

这将打印如下所示的日志:

2017-03-13 14:47:02.796   INFO 22481 --- [           main] com.company.app.Application    : Started Application in 16.115 seconds (JVM running for 16.792)
2017-03-13 14:47:32.684 [localhost, swagger] TRACE 22481 --- [pool-2-thread-1] c.c.app.config.ProfileLogger    : Starting span: [Trace: bfcdd2ce866efbff, Span: bfcdd2ce866efbff, Parent: null, exportable:true]

这已经很好了,但还不完全是我想要的。我想从一开始就添加概要文件->即使是“启动的应用程序”也应该包含概要文件-如果可能的话。其次,我想在info22481之间移动profiles

在实施过程中又出现了一个问题:在链接实施中有这样的说法:

if (this.log.isTraceEnabled()) {
    this.log.trace(text, span);
}

这是否意味着只有在log-level设置为trace时才发送跟踪?如果是这样的话,我如何用这种方法(给定debug/info/warn的日志级别)改进对stdout的日志记录呢?我认为sleuth/zipkin在导入依赖项时会覆盖log-pattern,因此,本地日志记录看起来与跟踪相同。最终,我对在本地stdout和zipkin中显示配置文件感兴趣。

编辑2:在Marcin的帮助下,我通过引入包含以下行的resources/logback-spring.xml文件改变了模式:

<springProperty scope="context" name="activeSpringProfiles" source="spring.profiles.active"/>
<!-- Example for logging into the build folder of your project -->
<property name="LOG_FILE" value="${BUILD_FOLDER:-build}/${activeSpringProfiles:-}"/>​

<!-- You can override this to have a custom pattern -->
<property name="CONSOLE_LOG_PATTERN"
value="%clr(%d{yyyy-MM-dd HH:mm:ss.SSS}){faint} %clr(${LOG_LEVEL_PATTERN:-%5p}) [${activeSpringProfiles:-}] %clr(${PID:- }){magenta} %clr(---){faint} %clr([%15.15t]){faint} %clr(%-40.40logger{39}){cyan} %clr(:){faint} %m%n${LOG_EXCEPTION_CONVERSION_WORD:-%wEx}"/>

注意,您还必须添加bootstrap.yml文件,以便正确显示应用程序名称。如果没有bootstrap.yml文件,上面的日志模式只打印“bootstrap”作为应用程序名。

bootstrap.yml只包含

spring:
  application:
    name: app

在我的情况下。其他所有内容都在application-[profile].yml中配置

现在一切都如愿以偿:

2017-03-13 15:58:21.291  INFO [app,,,] [localhost,swagger] 27519 --- [           main] com.company.app.keyserver.Application    : Started Application in 17.565 seconds (JVM running for 18.232)

共有1个答案

石正信
2023-03-14

当然,您只需要提供自己的日志格式(例如,通过logging.pattern.level-查看https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-logging.html以获取更多信息)。然后,您必须注册自己的spanLoggerbean实现,在该实现中,您将负责通过MDC添加spring配置文件的值(您可以将其作为示例https://github.com/spring-cloud/spring-cloud-sleuth/blob/master/spring-cloud-sleuth-core/src/main/Java/org/springframework/cloud/sleuth/log/slf4jspanLogger.)

更新:

对于更复杂的方法,还有另一种解决方案,似乎比重写类侦探要容易得多。您可以尝试logback-spring.xml方式,如下所示-https://github.com/spring-cloud-samples/sleuth-documentation-apps/blob/master/service1/src/main/resources/logback-spring.xml#l5-l11。我正在解析那里的应用程序名称,所以也许您可以对活动配置文件做同样的操作,而不需要编写任何代码?

 类似资料:
  • 从现有检测的Spring Boot应用程序向honeycomb-opentracing-proxy发送跟踪失败,代理控制台中出现以下错误: Spring Boot版本:2.1.3.发布Spring Cloud Sleuth版本:2.1.1.发布 应用程序.属性 如有任何帮助,我将不胜感激

  • 个人资料        展示、管理个人的基本资料 点击编辑按钮,进入编辑模式,输入新的用户信息,点击保存,完成资料修改。

  • 问题内容: 我想获取我朋友列表中的所有生日。我已经尝试了好几天。我的代码如下。 每次我使用此代码都会产生错误。令牌都显示为空,响应都显示为问题“必须使用活动访问令牌来查询有关当前用户的信息” 问题答案: 创建一个方法: 在方法主体中,您必须编写: } 上面的函数将从facebook获取json数据。您需要解析json以获得单独的配置文件数据。 来自Facebook的样本配置文件json将如下所示:

  • 问题内容: 在将其他字段(例如位置,性别,雇主等)添加到用户个人资料时,是否应该在其中添加其他列并将其保存在其中?还是应该创建一个新表来保存用户个人资料信息? 另外,当用户上传个人资料图片时,是否应该将其保存在同一张表中?(请注意,这不是生产服务器,我只是在本地运行服务器上执行此操作以弄清楚问题)。谢谢 问题答案: 你必须为用户个人资料建立模型: 然后在中配置

  • 我将使用Zipkin在现有代码中添加Spring Cloud Sleuth,以收集跟踪信息,并最终记录任意消息。正常的请求跨度被正确地发送到Zipkin: 检查Zipkin中的跟踪,可以正确地找到span,但是却看不到行中使用的消息--这表明我在这里做了一些错误的事情,或者它不应该以这种方式工作。我的抽样百分比设置为100%。 使用slf4j接口会很方便,因为现有的代码已经以这种方式检测了。有可能

  • 我最近将我的项目从spring boot 1.4.1、spring cloud Sleuth 1.1.0、spring cloud Zipkin 1.1.0升级到spring boot 1.5.3、spring cloud Sleuth 1.2.0、spring cloud Zipkin 1.2.0。 在最新版本的spring cloud Sleuth中,他们添加了“错误”标签,一旦出现任何异常,