我使用SiftingAppender进行Logback配置,其中动态MDC值是在动态生成的main()方法中的应用程序加载上设置的。如何在任何类中启动所有日志记录之前,确保加载从Java代码生成的MDC值?因为当我开始登录SingletonUUID类时,它会记录到目录“default”(SiftAppender鉴别器的默认值)。
这是我的logback.xml:
<property name="LocalDevLogLevel" value="INFO" />
<property name="log-dir" value="simple-service-log" />
<property name="COMMON_LOG_PATTERN" value="%-30([%thread]) %-5level - %logger{32} - %msg%n"/>
<appender name="simpleConsole" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>%d{"yyyy-MM-dd HH:mm:ss,SSS 'UTC'", UTC} ${COMMON_LOG_PATTERN}</pattern>
</encoder>
</appender>
<appender name="SIFTSYSLOG" class="ch.qos.logback.classic.sift.SiftingAppender">
<discriminator>
<key>serviceInstanceId</key>
<defaultValue>default</defaultValue>
</discriminator>
<sift>
<appender name="FILE-${serviceInstanceId}" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>./${log-dir}/${serviceInstanceId}/sys.log</file>
<append>false</append>
<layout class="ch.qos.logback.classic.PatternLayout">
<pattern>%d{"yyyy-MM-dd HH:mm:ss,SSS 'UTC'", UTC} ${COMMON_LOG_PATTERN}</pattern>
</layout>
<rollingPolicy class="ch.qos.logback.core.rolling.FixedWindowRollingPolicy">
<FileNamePattern>./${log-dir}/${serviceInstanceId}/sys.%i.log.zip</FileNamePattern>
<MinIndex>1</MinIndex>
<MaxIndex>10</MaxIndex>
</rollingPolicy>
<triggeringPolicy class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy">
<MaxFileSize>10MB</MaxFileSize>
</triggeringPolicy>
</appender>
</sift>
</appender>
<!--DEVELOPMENT logger-->
<springProfile name="local-dev">
<logger name="com.simple.service.app" additivity="false">
<appender-ref ref="SIFTSYSLOG" />
<appender-ref ref="simpleConsole" />
</logger>
<root level="${LocalDevLogLevel}" additivity="false">
<appender-ref ref="SIFTSYSLOG" />
<appender-ref ref="simpleConsole" />
</root>
</springProfile>
主类:
package com.simple.service.app;
import org.slf4j.MDC;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.context.annotation.Import;
import com.simple.service.config.MyAppConfig;
@SpringBootApplication
@EnableDiscoveryClient
public class MyApp {
private static SingletonUUID singletonUUID = SingletonUUID.getInstance();
public static void main(String[] args) {
MDC.put("serviceInstanceId", singletonUUID.getUUID().toString()); // set UUID for log directory
SpringApplication.run(MyApp.class, args);
}
}
SingletonUUID类:
package com.simple.service.app;
import java.util.UUID;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class SingletonUUID {
private static Logger log = LoggerFactory.getLogger(SingletonUUID.class);
private static SingletonUUID instance = new SingletonUUID();
private UUID uuid = UUID.randomUUID();
private SingletonUUID() {
log.info("SingletonUUID : Initializing instance with UUID : " + uuid.toString());
}
public static SingletonUUID getInstance() {
return instance;
}
public UUID getUUID() {
return this.uuid;
}
}
答案 0 :(得分:0)
我没有在main()MDC.put()中使用,而是扩展了Discriminator在logback-spring.xml中使用了这个类,并删除了在singletonUUID类中的日志记录。
logback-spring.xml的摘录:
<appender name="SIFTSYSLOG" class="ch.qos.logback.classic.sift.SiftingAppender">
<discriminator class="com.simple.entity.app.UniqueIdDiscriminator" />
<sift>
<appender name="FILE-${serviceInstanceId}" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>./${log-dir}/${serviceInstanceId}/sys.log</file>
<append>false</append>
<layout class="ch.qos.logback.classic.PatternLayout">
<pattern>%d{"yyyy-MM-dd HH:mm:ss,SSS 'UTC'", UTC} ${COMMON_LOG_PATTERN}</pattern>
</layout>
<rollingPolicy class="ch.qos.logback.core.rolling.FixedWindowRollingPolicy">
<FileNamePattern>./${log-dir}/${serviceInstanceId}/sys.%i.log.zip</FileNamePattern>
<MinIndex>1</MinIndex>
<MaxIndex>10</MaxIndex>
</rollingPolicy>
<triggeringPolicy class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy">
<MaxFileSize>10MB</MaxFileSize>
</triggeringPolicy>
</appender>
</sift>
</appender>
UniqueIdDiscriminator类:
package com.simple.entity.app;
import ch.qos.logback.classic.spi.ILoggingEvent;
import ch.qos.logback.core.sift.Discriminator;
public class UniqueIdDiscriminator implements Discriminator<ILoggingEvent> {
private static SingletonUUID singletonUUID = SingletonUUID.getInstance();
private static final String KEY = "serviceInstanceId";
private boolean started;
public String getDiscriminatingValue(ILoggingEvent iLoggingEvent) {
return singletonUUID.getUUID().toString();
}
public String getKey() {
return KEY;
}
public void start() {
started = true;
}
public void stop() {
started = false;
}
public boolean isStarted() {
return started;
}
}