这篇文章将为大家详细讲解有关Spring Sleuth中怎么主动添加日志,文章内容质量较高,因此小编分享给大家做个参考,希望大家阅读完这篇文章后对相关知识有一定的了解。
Spring Sleuth 是通过 logging.pattern.level 变量来实现主动日志添加的。
添加 在 TraceEnvironmentPostProcessor 中
@Override
public void postProcessEnvironment(ConfigurableEnvironment environment,
SpringApplication application) {
Map<String, Object> map = new HashMap<String, Object>();
// This doesn't work with all logging systems but it's a useful default so you see
// traces in logs without having to configure it.
// 在这里判断开启 sleuth 则修改 logging.pattern.level 的值
if (Boolean.parseBoolean(environment.getProperty("spring.sleuth.enabled", "true"))) {
map.put("logging.pattern.level",
"%5p [${spring.zipkin.service.name:${spring.application.name:-}},%X{X-B3-TraceId:-},%X{X-B3-SpanId:-},%X{X-Span-Export:-}]");
}
// TODO: Remove this in 2.0.x. For compatibility we always set to true
if (!environment.containsProperty(SPRING_AOP_PROXY_TARGET_CLASS)) {
map.put(SPRING_AOP_PROXY_TARGET_CLASS, "true");
}
addOrReplace(environment.getPropertySources(), map);
}
在 LoggingSystemProperties 将 LOG_LEVEL_PATTERN 的值设置为 logging.pattern.level 的值
public void apply(LogFile logFile) {
RelaxedPropertyResolver propertyResolver = RelaxedPropertyResolver
.ignoringUnresolvableNestedPlaceholders(this.environment, "logging.");
setSystemProperty(propertyResolver, EXCEPTION_CONVERSION_WORD,
"exception-conversion-word");
setSystemProperty(PID_KEY, new ApplicationPid().toString());
setSystemProperty(propertyResolver, CONSOLE_LOG_PATTERN, "pattern.console");
setSystemProperty(propertyResolver, FILE_LOG_PATTERN, "pattern.file");
// LOG_LEVEL_PATTERN = "LOG_LEVEL_PATTERN" 设置到环境变量
setSystemProperty(propertyResolver, LOG_LEVEL_PATTERN, "pattern.level");
if (logFile != null) {
logFile.applyToSystemProperties();
}
}
在 PropertyAction#begin 解析配置 在 InterpretationContext 解析当前的字符串
public String subst(String value) {
if (value == null) {
return null;
}
return OptionHelper.substVars(value, this, context);
}
public static String substVars(String input, PropertyContainer pc0, PropertyContainer pc1) {
try {
return NodeToStringTransformer.substituteVariable(input, pc0, pc1);
} catch (ScanException e) {
throw new IllegalArgumentException("Failed to parse input [" + input + "]", e);
}
}
public static String substituteVariable(String input, PropertyContainer pc0, PropertyContainer pc1) throws ScanException {
Node node = tokenizeAndParseString(input);
NodeToStringTransformer nodeToStringTransformer = new NodeToStringTransformer(node, pc0, pc1);
return nodeToStringTransformer.transform();
}
public String transform() throws ScanException {
StringBuilder stringBuilder = new StringBuilder();
compileNode(node, stringBuilder, new Stack<Node>());
return stringBuilder.toString();
}
private void compileNode(Node inputNode, StringBuilder stringBuilder, Stack<Node> cycleCheckStack) throws ScanException {
Node n = inputNode;
while (n != null) {
switch (n.type) {
case LITERAL:
handleLiteral(n, stringBuilder);
break;
case VARIABLE:
handleVariable(n, stringBuilder, cycleCheckStack);
break;
}
n = n.next;
}
}
private void handleVariable(Node n, StringBuilder stringBuilder, Stack<Node> cycleCheckStack) throws ScanException {
// ...
String key = keyBuffer.toString();
String value = lookupKey(key);
// ...
}
private String lookupKey(String key) {
String value = propertyContainer0.getProperty(key);
if (value != null)
return value;
if (propertyContainer1 != null) {
value = propertyContainer1.getProperty(key);
if (value != null)
return value;
}
// 在这里找到值
value = OptionHelper.getSystemProperty(key, null);
if (value != null)
return value;
value = OptionHelper.getEnv(key);
if (value != null) {
return value;
}
return null;
}
public static String getSystemProperty(String key, String def) {
try {
return System.getProperty(key, def);
} catch (SecurityException e) {
return def;
}
}
关于Spring Sleuth中怎么主动添加日志就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。
原文链接:https://my.oschina.net/seal90/blog/3086404