這篇文章將為大家詳細(xì)講解有關(guān)Spring Sleuth中怎么主動(dòng)添加日志,文章內(nèi)容質(zhì)量較高,因此小編分享給大家做個(gè)參考,希望大家閱讀完這篇文章后對(duì)相關(guān)知識(shí)有一定的了解。
讓客戶滿意是我們工作的目標(biāo),不斷超越客戶的期望值來(lái)自于我們對(duì)這個(gè)行業(yè)的熱愛。我們立志把好的技術(shù)通過(guò)有效、簡(jiǎn)單的方式提供給客戶,將通過(guò)不懈努力成為客戶在信息化領(lǐng)域值得信任、有價(jià)值的長(zhǎng)期合作伙伴,公司提供的服務(wù)項(xiàng)目有:域名申請(qǐng)、虛擬主機(jī)、營(yíng)銷軟件、網(wǎng)站建設(shè)、翁牛特網(wǎng)站維護(hù)、網(wǎng)站推廣。
Spring Sleuth 是通過(guò) logging.pattern.level 變量來(lái)實(shí)現(xiàn)主動(dòng)日志添加的。
添加 在 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 的值設(shè)置為 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" 設(shè)置到環(huán)境變量 setSystemProperty(propertyResolver, LOG_LEVEL_PATTERN, "pattern.level"); if (logFile != null) { logFile.applyToSystemProperties(); } }
在 PropertyAction#begin 解析配置 在 InterpretationContext 解析當(dāng)前的字符串
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; } }
關(guān)于Spring Sleuth中怎么主動(dòng)添加日志就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,可以學(xué)到更多知識(shí)。如果覺得文章不錯(cuò),可以把它分享出去讓更多的人看到。