您好,登錄后才能下訂單哦!
這篇文章將為大家詳細講解有關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中怎么主動添加日志就分享到這里了,希望以上內容可以對大家有一定的幫助,可以學到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。