1 概述
logback.xml配置文件的詳細配置,很多地方都說得比較細,本文主要從幾個重點來看一下原理,了解原理能夠幫助確定哪些應該配置,以及如何配置。
logback.xml是為打印日志服務的,打印的內容一般打印到控制臺(Console)和文件(file)里,在生產環境中主要是打印到文件里,然后用掃描工具匯總到某個地方方便查詢(如ELK)。打印的內容要符合一定的格式,提供足夠的信息,方便進行日志查詢和分析;如果所有日志都打印到一個文件里,就有可能文件過大而難以查看,還可能大到一個磁盤裝不下,也很難把早期的日志刪除掉僅保留一定期限內的日志,所以需要對日志文件進行拆分,每個文件確定在一定大小之內,并控制總體僅保留一定量的日志,避免日志總量過多把磁盤占滿了宕機等。日志配置文件也不是一成不變的,當修改了配置文件,希望能夠不需要重啟Java進程而能夠生效,比如修改日志級別。
2 原理
2.1 打印內容的格式
<appender name="console" class="ch.qos.logback.core.ConsoleAppender"><encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder"><Pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} %highlight(%-5level) [%thread] %logger{50} - %msg%n</Pattern><charset>UTF-8</charset></encoder>
</appender>
上面就是一個<appender>的配置,里面的<Pattern>就是日志內容的格式,還可以設置高亮顏色。如果要詳細看logback支持的格式,那么可以參考官方文檔:https://logback.qos.ch/manual/layouts.html
如果想了解原理也容易,上面就指明了對應的類ch.qos.logback.classic.encoder.PatternLayoutEncoder,這也代表著甚至可以通過它來自定義。
?
// 當加載logback.xml文件時,解析到<encoder>節點的時候,會調PatternLayoutEncoder.start()
// 源碼位置:ch.qos.logback.classic.encoder.PatternLayoutEncoder
public class PatternLayoutEncoder extends PatternLayoutEncoderBase<ILoggingEvent> {public PatternLayoutEncoder() {}public void start() {// 1. 初始化PatternLayout,里面會初始化一些ConverterPatternLayout patternLayout = new PatternLayout();patternLayout.setContext(this.context);patternLayout.setPattern(this.getPattern());patternLayout.setOutputPatternAsHeader(this.outputPatternAsHeader);patternLayout.start();this.layout = patternLayout;super.start();}
}// 源碼位置:ch.qos.logback.classic.PatternLayout
// 2. 靜態代碼塊初始化converter,它們決定了<Pattern>節點里能夠配置的變量,用%來指示變量,比如%d表示時間
public static final Map<String, String> DEFAULT_CONVERTER_MAP = new HashMap<String, String>();
static {DEFAULT_CONVERTER_MAP.putAll(Parser.DEFAULT_COMPOSITE_CONVERTER_MAP);DEFAULT_CONVERTER_MAP.put("d", DateConverter.class.getName());DEFAULT_CONVERTER_MAP.put("date", DateConverter.class.getName());DEFAULT_CONVERTER_MAP.put("r", RelativeTimeConverter.class.getName());DEFAULT_CONVERTER_MAP.put("relative", RelativeTimeConverter.class.getName());DEFAULT_CONVERTER_MAP.put("level", LevelConverter.class.getName());DEFAULT_CONVERTER_MAP.put("le", LevelConverter.class.getName());DEFAULT_CONVERTER_MAP.put("p", LevelConverter.class.getName());DEFAULT_CONVERTER_MAP.put("t", ThreadConverter.class.getName());DEFAULT_CONVERTER_MAP.put("thread", ThreadConverter.class.getName());DEFAULT_CONVERTER_MAP.put("lo", LoggerConverter.class.getName());DEFAULT_CONVERTER_MAP.put("logger", LoggerConverter.class.getName());DEFAULT_CONVERTER_MAP.put("c", LoggerConverter.class.getName());DEFAULT_CONVERTER_MAP.put("m", MessageConverter.class.getName());DEFAULT_CONVERTER_MAP.put("msg", MessageConverter.class.getName());DEFAULT_CONVERTER_MAP.put("message", MessageConverter.class.getName());DEFAULT_CONVERTER_MAP.put("C", ClassOfCallerConverter.class.getName());DEFAULT_CONVERTER_MAP.put("class", ClassOfCallerConverter.class.getName());DEFAULT_CONVERTER_MAP.put("M", MethodOfCallerConverter.class.getName());DEFAULT_CONVERTER_MAP.put("method", MethodOfCallerConverter.class.getName());DEFAULT_CONVERTER_MAP.put("L", LineOfCallerConverter.class.getName());DEFAULT_CONVERTER_MAP.put("line", LineOfCallerConverter.class.getName());DEFAULT_CONVERTER_MAP.put("F", FileOfCallerConverter.class.getName());DEFAULT_CONVERTER_MAP.put("file", FileOfCallerConverter.class.getName());DEFAULT_CONVERTER_MAP.put("X", MDCConverter.class.getName());DEFAULT_CONVERTER_MAP.put("mdc", MDCConverter.class.getName());DEFAULT_CONVERTER_MAP.put("ex", ThrowableProxyConverter.class.getName());DEFAULT_CONVERTER_MAP.put("exception", ThrowableProxyConverter.class.getName());DEFAULT_CONVERTER_MAP.put("rEx", RootCauseFirstThrowableProxyConverter.class.getName());DEFAULT_CONVERTER_MAP.put("rootException", RootCauseFirstThrowableProxyConverter.class.getName());DEFAULT_CONVERTER_MAP.put("throwable", ThrowableProxyConverter.class.getName());DEFAULT_CONVERTER_MAP.put("xEx", ExtendedThrowableProxyConverter.class.getName());DEFAULT_CONVERTER_MAP.put("xException", ExtendedThrowableProxyConverter.class.getName());DEFAULT_CONVERTER_MAP.put("xThrowable", ExtendedThrowableProxyConverter.class.getName());DEFAULT_CONVERTER_MAP.put("nopex", NopThrowableInformationConverter.class.getName());DEFAULT_CONVERTER_MAP.put("nopexception", NopThrowableInformationConverter.class.getName());DEFAULT_CONVERTER_MAP.put("cn", ContextNameConverter.class.getName());DEFAULT_CONVERTER_MAP.put("contextName", ContextNameConverter.class.getName());DEFAULT_CONVERTER_MAP.put("caller", CallerDataConverter.class.getName());DEFAULT_CONVERTER_MAP.put("marker", MarkerConverter.class.getName());DEFAULT_CONVERTER_MAP.put("property", PropertyConverter.class.getName());DEFAULT_CONVERTER_MAP.put("n", LineSeparatorConverter.class.getName());DEFAULT_CONVERTER_MAP.put("black", BlackCompositeConverter.class.getName());DEFAULT_CONVERTER_MAP.put("red", RedCompositeConverter.class.getName());DEFAULT_CONVERTER_MAP.put("green", GreenCompositeConverter.class.getName());DEFAULT_CONVERTER_MAP.put("yellow", YellowCompositeConverter.class.getName());DEFAULT_CONVERTER_MAP.put("blue", BlueCompositeConverter.class.getName());DEFAULT_CONVERTER_MAP.put("magenta", MagentaCompositeConverter.class.getName());DEFAULT_CONVERTER_MAP.put("cyan", CyanCompositeConverter.class.getName());DEFAULT_CONVERTER_MAP.put("white", WhiteCompositeConverter.class.getName());DEFAULT_CONVERTER_MAP.put("gray", GrayCompositeConverter.class.getName());DEFAULT_CONVERTER_MAP.put("boldRed", BoldRedCompositeConverter.class.getName());DEFAULT_CONVERTER_MAP.put("boldGreen", BoldGreenCompositeConverter.class.getName());DEFAULT_CONVERTER_MAP.put("boldYellow", BoldYellowCompositeConverter.class.getName());DEFAULT_CONVERTER_MAP.put("boldBlue", BoldBlueCompositeConverter.class.getName());DEFAULT_CONVERTER_MAP.put("boldMagenta", BoldMagentaCompositeConverter.class.getName());DEFAULT_CONVERTER_MAP.put("boldCyan", BoldCyanCompositeConverter.class.getName());DEFAULT_CONVERTER_MAP.put("boldWhite", BoldWhiteCompositeConverter.class.getName());DEFAULT_CONVERTER_MAP.put("highlight", HighlightingCompositeConverter.class.getName());DEFAULT_CONVERTER_MAP.put("lsn", LocalSequenceNumberConverter.class.getName());DEFAULT_CONVERTER_MAP.put("prefix", PrefixCompositeConverter.class.getName());
}
public Map<String, String> getDefaultConverterMap() {return DEFAULT_CONVERTER_MAP;
}// 回到PatternLayoutEncoder,繼續初始化
// 源碼位置:ch.qos.logback.classic.encoder.PatternLayoutEncoder
public class PatternLayoutEncoder extends PatternLayoutEncoderBase<ILoggingEvent> {public PatternLayoutEncoder() {}public void start() {// 1. 初始化PatternLayout,里面會初始化一些Converter,細節看1.1PatternLayout patternLayout = new PatternLayout();patternLayout.setContext(this.context);patternLayout.setPattern(this.getPattern());patternLayout.setOutputPatternAsHeader(this.outputPatternAsHeader);// 3. 開始解析pattern,實際調用的是PatternLayout父類PatternLayoutBase的start()方法patternLayout.start();this.layout = patternLayout;super.start();}
}// 繼承關系:PatternLayoutEncoder < PatternLayoutBase
// 源碼位置:ch.qos.logback.core.pattern.PatternLayoutBase
public void start() {if (pattern == null || pattern.length() == 0) {addError("Empty or null pattern.");return;}try {Parser<E> p = new Parser<E>(pattern);if (getContext() != null) {p.setContext(getContext());}Node t = p.parse();// 4. 解析pattern,getEffectiveConverterMap()里面調了步驟2的getDefaultConverterMap()得到了初始化的converterthis.head = p.compile(t, getEffectiveConverterMap());if (postCompileProcessor != null) {postCompileProcessor.process(context, head);}ConverterUtil.setContextForConverters(getContext(), head);ConverterUtil.startConverters(this.head);super.start();} catch (ScanException sce) {StatusManager sm = getContext().getStatusManager();sm.add(new ErrorStatus("Failed to parse pattern \"" + getPattern() + "\".", this, sce));}
}// 源碼位置:ch.qos.logback.core.pattern.parser.Parser
public Converter<E> compile(final Node top, Map converterMap) {Compiler<E> compiler = new Compiler<E>(top, converterMap);compiler.setContext(context);// compiler.setStatusManager(statusManager);// 5. 實際解析patternreturn compiler.compile();
}// 源碼位置:ch.qos.logback.core.pattern.parser.Compiler
// 實際解析pattern,整個pattern已經被分成一個個Node,Node的格式如下:
// 以pattern=%d{yyyy-MM-dd HH:mm:ss.SSS}a b c%highlight(%-5level) [%thread] %logger{50} - %msg%n 為例:
// 1) 以%作為開始字符、以})和空格等作為結束字符作為一段進行分割成多段,開始字符和結束字符之間的字符作為一個Keyword Node,段之間如果還有非空字符則作為Literal Node;
// 上例中%d{yyyy-MM-dd HH:mm:ss.SSS}作為Keyword Node,a b c為Literal Node;
// 2) 如果只有一個關鍵字的則是Simple Keyword Node,如%d{yyyy-MM-dd HH:mm:ss.SSS}
// 如果有多個關鍵字嵌套的則是Composite Keyword Node,如%highlight(%-5levelx)
// 3) %后面的關鍵字要能夠從converterMap取到,通過它能夠從里面找到對應的Converter(參考步驟2),如果找不到就報錯了;
// 4) 關鍵字前面的是format信息,后面的是option信息,
// 比如:%d{yyyy-MM-dd HH:mm:ss.SSS}中,yyyy-MM-dd HH:mm:ss.SSS是option信息,無format信息;
// 比如:%-5level中,5是format信息,表示最短5字符,無option信息;
// 5) 嵌套結構用childNode表示,如%highlight(%-5level)中,%-5level是一個childNode,格式和Node一樣;
// 6) 每個Node都有個next,指向下一個節點,即鏈式結構,通過一層層的next可以遍歷整條鏈;
// 下面最終都是把Converter放到Node里,實際上pattern里的信息都已經分解到Converter里了,打印日志的時候可以直接使用
Converter<E> compile() {head = tail = null;// 當n = n.next為null時,鏈條結束for (Node n = top; n != null; n = n.next) {switch (n.type) {case Node.LITERAL: // Literal NodeaddToList(new LiteralConverter<E>((String) n.getValue()));break;case Node.COMPOSITE_KEYWORD: // Composite Keyword NodeCompositeNode cn = (CompositeNode) n;// 根據keyword找到Converter,并進行實例化CompositeConverter<E> compositeConverter = createCompositeConverter(cn);if (compositeConverter == null) {addError("Failed to create converter for [%" + cn.getValue() + "] keyword");addToList(new LiteralConverter<E>("%PARSER_ERROR[" + cn.getValue() + "]"));break;}// 把信息設置到Converter里compositeConverter.setFormattingInfo(cn.getFormatInfo());compositeConverter.setOptionList(cn.getOptions());// 再處理childNode的ConverterCompiler<E> childCompiler = new Compiler<E>(cn.getChildNode(), converterMap);childCompiler.setContext(context);Converter<E> childConverter = childCompiler.compile();compositeConverter.setChildConverter(childConverter); // 記錄childNode的ConverteraddToList(compositeConverter); // 記錄當前node的Converterbreak;case Node.SIMPLE_KEYWORD: // Simple Keyword NodeSimpleKeywordNode kn = (SimpleKeywordNode) n;// 根據keyword找到Converter,并進行實例化,主要是Convert的具體類不一樣,所以需要多寫一個DynamicConverter<E> dynaConverter = createConverter(kn);if (dynaConverter != null) {// 把信息設置到Converter里dynaConverter.setFormattingInfo(kn.getFormatInfo());dynaConverter.setOptionList(kn.getOptions());addToList(dynaConverter); // 記錄當前node的Converter} else {// 沒有對應的Converter則報錯Converter<E> errConveter = new LiteralConverter<E>("%PARSER_ERROR[" + kn.getValue() + "]");addStatus(new ErrorStatus("[" + kn.getValue() + "] is not a valid conversion word", this));addToList(errConveter); // 記錄當前node的Converter}}}return head;
}
CompositeConverter<E> createCompositeConverter(CompositeNode cn) {String keyword = (String) cn.getValue();// 只是從Map里取到Converter的類名,然后實例化String converterClassStr = (String) converterMap.get(keyword);if (converterClassStr != null) {try {// 實例化converter對象return (CompositeConverter) OptionHelper.instantiateByClassName(converterClassStr, CompositeConverter.class, context);} catch (Exception e) {addError("Failed to instantiate converter class [" + converterClassStr + "] as a composite converter for keyword [" + keyword + "]", e);return null;}} else {addError("There is no conversion class registered for composite conversion word [" + keyword + "]");return null;}
}
DynamicConverter<E> createConverter(SimpleKeywordNode kn) {String keyword = (String) kn.getValue();// 只是從Map里取到Converter的類名,然后實例化String converterClassStr = (String) converterMap.get(keyword);if (converterClassStr != null) {try {return (DynamicConverter) OptionHelper.instantiateByClassName(converterClassStr, DynamicConverter.class, context);} catch (Exception e) {addError("Failed to instantiate converter class [" + converterClassStr + "] for keyword [" + keyword + "]", e);return null;}} else {addError("There is no conversion class registered for conversion word [" + keyword + "]");return null;}
}
3 架構一小步
3.1 pattern選擇
- 日志的主要目的是為了定位問題,所以應該選擇一些對定位問題比較有幫助的。從日志看出問題后,最好是能夠定位到代碼的位置,也就是哪個類的哪個方法,甚至是哪一行代碼。
- 打印日志不能影響業務的正常運行,比如打印日志不能消耗掉很多性能。
關鍵字 | 說明 | 備注 |
%d{yyyy-MM-dd HH:mm:ss.SSS} | 打印日志的時間,要到毫秒 | |
%-5level | 日志級別,主要有TRACE、DEBUG、INFO、WARN、ERROR,最多5個字符,為了對齊則定為最少5個字符 | |
%thread | 線程標識,用于表示代碼執行在哪個線程里 | |
%logger{50} | logger名稱,等同于節點的name屬性。一般獲取logger的時候使用的是含包名的類名,所以logger的名稱實際上指的是包名+類名,可能很長,需要限制長度,比如50字符。也可以使用%class{50}來代替。 | 帶包名的類名一般比較長,所以它和%class一般只用一個。用logger的好處是能兼容類名和一些特殊的logger名。 |
%method | 方法名,打印代碼所在的方法 | |
%line | 行號,打印代碼的行號位置 | 這個配置對定位問題是比較有用的,但其比較耗費性能,一般不配置,除非性能不是問題。 |
%msg | 日志內容 | 最好在一個方法內能夠唯一指示是哪個日志內容 |
%n | 換行符 |
Pattern=%d{yyyy-MM-dd HH:mm:ss.SSS} %-5level [%thread] %logger{50}#%method - %msg%n202x-xx-xx xx:xx:xx.xxx DEBUG [http-nio-8080-exec-2] com.qqian.stepfmk.srvpro.hello.HelloController#say - Access saying hello, message=zhangsan
202x-xx-xx xx:xx:xx.xxx INFO [http-nio-8080-exec-2] com.qqian.stepfmk.srvpro.hello.HelloController#say - Access saying hello, message=zhangsan
202x-xx-xx xx:xx:xx.xxx WARN [http-nio-8080-exec-2] com.qqian.stepfmk.srvpro.hello.HelloController#say - Access saying hello, message=zhangsan
202x-xx-xx xx:xx:xx.xxx ERROR [http-nio-8080-exec-2] com.qqian.stepfmk.srvpro.hello.HelloController#say - Access saying hello, message=zhangsan
3.2 日志內容規范
- 日志內容必要用一句簡短的話說明日志出現的結果,這句話最好唯一。
- 強調結果是為了幫忙定位問題,而不是說明代碼做了啥。代碼做了啥是可以通過看代碼了解到的。
- 實際日志一般打印不了行號,這句話若能夠唯一,就能夠快速定位到具體的代碼,相當于有了代碼行號。
- 比如判斷一個中間變量為不合法l時打印日志,這句話應該說明該變量哪里不合法。通過這個說明就大致能夠判斷問題的所在,即使不能直接判斷問題,也應該盡可能提供最多的信息量。
- 日志內容要包含上下文,沒有上下文的日志不要打印。
- 上下文一般是指出現日志所指示結果的一些相關變量。
- 這些變量可能是從前面傳過來的,也可能是中間產生的。
- 打印哪些變量衡量的標準是:當出現日志內容指示的結果時,需要哪些變量才能確定問題。比如一個從數據庫讀取數據的結果為null時,需要知道組裝查詢SQL的重點變量。
- 對于異常信息必須打印堆棧。
- 使用logger.error("xxxx", e)打印異常時,注意不要給e提供占位符,有占位符只打印了e.getMessage(),沒有打印堆棧。
- 有堆棧才能詳細看到是哪段代碼發生異常,也就是幫助定位到具體的代碼。