SpringSleuth中怎么主动添加日志
这篇文章将为大家详细讲解有关Spring Sleuth中怎么主动添加日志,文章内容质量较高,因此小编分享给大家做个参考,希望大家阅读完这篇文章后对相关知识有一定的了解。
让客户满意是我们工作的目标,不断超越客户的期望值来自于我们对这个行业的热爱。我们立志把好的技术通过有效、简单的方式提供给客户,将通过不懈努力成为客户在信息化领域值得信任、有价值的长期合作伙伴,公司提供的服务项目有:域名申请、虚拟主机、营销软件、网站建设、翁牛特网站维护、网站推广。
Spring Sleuth 是通过 logging.pattern.level 变量来实现主动日志添加的。
添加 在 TraceEnvironmentPostProcessor 中
@Override public void postProcessEnvironment(ConfigurableEnvironment environment, SpringApplication application) { Mapmap = new HashMap (); // 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()); return stringBuilder.toString(); } private void compileNode(Node inputNode, StringBuilder stringBuilder, Stack 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, StackcycleCheckStack) 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中怎么主动添加日志就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。
本文题目:SpringSleuth中怎么主动添加日志
文章路径:http://lswzjz.com/article/jojpes.html