我正在使用这些依赖项:
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 的回答,我将其实现如下:
应用程序.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)
}
日志配置.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]
这已经很好了,但还不是我想要的。我想从头开始添加配置文件->即使“启动的应用程序”也应该包含配置文件-如果可能的话。其次,我想在profiles
两者之间移动INFO
在and22481
移动。
在实现过程中又出现了一个问题:在链接的实现中有这样的声明:
if (this.log.isTraceEnabled()) {
this.log.trace(text, span);
}
这是否意味着您仅在日志级别设置为 TRACE 时才发送跟踪信息?如果是这样,我如何使用这种方法改进对标准输出的日志记录(给定日志级别的调试/信息/警告)?我认为日志模式在导入依赖项时被 Sleuth/Zipkin 覆盖,因此,本地日志记录看起来与跟踪相同。最终,我有兴趣在本地标准输出和 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)