logback日志輸出格式設(shè)置方式
部分標(biāo)簽解釋
withJansi: 是否配合jansi使用
filter: 日志過濾器
layout: 布局, 配合內(nèi)置模板使用
pattern: 日志模板, 有內(nèi)置的日志模板可以直接使用, 例如: ${FILE_LOG_PATTERN}
encoder: 編碼器, 支付轉(zhuǎn)碼
charset: 支付編碼, 一般為utf-8
內(nèi)置轉(zhuǎn)換器
可以直接通過調(diào)用內(nèi)置的轉(zhuǎn)換器將日志取出來, 打印出來
部分特殊字符串解釋
%d: 表示日期
%thread: 表示線程名
%-5level: 級別從左顯示5個(gè)字符寬度
%msg: 日志消息
%n: 是換行符
更多內(nèi)置特殊字符
defaultConverterMap.put("d", DateConverter.class.getName());
defaultConverterMap.put("date", DateConverter.class.getName());
defaultConverterMap.put("r", RelativeTimeConverter.class.getName());
defaultConverterMap.put("relative", RelativeTimeConverter.class.getName());
defaultConverterMap.put("level", LevelConverter.class.getName());
defaultConverterMap.put("le", LevelConverter.class.getName());
defaultConverterMap.put("p", LevelConverter.class.getName());
defaultConverterMap.put("t", ThreadConverter.class.getName());
defaultConverterMap.put("thread", ThreadConverter.class.getName());
defaultConverterMap.put("lo", LoggerConverter.class.getName());
defaultConverterMap.put("logger", LoggerConverter.class.getName());
defaultConverterMap.put("c", LoggerConverter.class.getName());
defaultConverterMap.put("m", MessageConverter.class.getName());
defaultConverterMap.put("msg", MessageConverter.class.getName());
defaultConverterMap.put("message", MessageConverter.class.getName());
defaultConverterMap.put("C", ClassOfCallerConverter.class.getName());
defaultConverterMap.put("class", ClassOfCallerConverter.class.getName());
defaultConverterMap.put("M", MethodOfCallerConverter.class.getName());
defaultConverterMap.put("method", MethodOfCallerConverter.class.getName());
defaultConverterMap.put("L", LineOfCallerConverter.class.getName());
defaultConverterMap.put("line", LineOfCallerConverter.class.getName());
defaultConverterMap.put("F", FileOfCallerConverter.class.getName());
defaultConverterMap.put("file", FileOfCallerConverter.class.getName());
defaultConverterMap.put("X", MDCConverter.class.getName());
defaultConverterMap.put("mdc", MDCConverter.class.getName());
defaultConverterMap.put("ex", ThrowableProxyConverter.class.getName());
defaultConverterMap.put("exception", ThrowableProxyConverter.class.getName());
defaultConverterMap.put("rEx", RootCauseFirstThrowableProxyConverter.class.getName());
defaultConverterMap.put("rootException", RootCauseFirstThrowableProxyConverter.class.getName());
defaultConverterMap.put("throwable", ThrowableProxyConverter.class.getName());
defaultConverterMap.put("xEx", ExtendedThrowableProxyConverter.class.getName());
defaultConverterMap.put("xException", ExtendedThrowableProxyConverter.class.getName());
defaultConverterMap.put("xThrowable", ExtendedThrowableProxyConverter.class.getName());
defaultConverterMap.put("nopex", NopThrowableInformationConverter.class.getName());
defaultConverterMap.put("nopexception", NopThrowableInformationConverter.class.getName());
defaultConverterMap.put("cn", ContextNameConverter.class.getName());
defaultConverterMap.put("contextName", ContextNameConverter.class.getName());
defaultConverterMap.put("caller", CallerDataConverter.class.getName());
defaultConverterMap.put("marker", MarkerConverter.class.getName());
defaultConverterMap.put("property", PropertyConverter.class.getName());
defaultConverterMap.put("n", LineSeparatorConverter.class.getName());
如何自定義輸出樣式
字符顏色定義
使用%加上顏色代號, 然后內(nèi)容放在后面的括號()里面
例如:
- %white(這里放輸出內(nèi)容)
- 配合特殊字符串, 可以輸入時(shí)間, 線程名等
內(nèi)置的一些樣式

代碼示例
使用內(nèi)置模板
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<filter class="com.wang.cloud.store.common.LogFilter"/>
<layout class="ch.qos.logback.classic.PatternLayout">
<pattern>${FILE_LOG_PATTERN}</pattern>
</layout>
</appender>使用自定義模板
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<filter class="com.wang.cloud.store.common.LogFilter"/>
<withJansi>false</withJansi>
<encoder>
<pattern>%white(%d{yyyy-MM-dd HH:mm:ss}) %highlight(%lsn) %green([%thread]) %highlight(%-5level) %boldMagenta(%logger{10}) - %cyan(%msg%n)</pattern>
<charset>utf8</charset>
</encoder>
</appender>
總結(jié)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
ApplicationListenerDetector監(jiān)聽器判斷demo
這篇文章主要為大家介紹了ApplicationListenerDetector監(jiān)聽器判斷demo,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-03-03
Session過期后自動(dòng)跳轉(zhuǎn)到登錄頁面的實(shí)例代碼
這篇文章主要介紹了Session過期后自動(dòng)跳轉(zhuǎn)到登錄頁面實(shí)例代碼,非常不錯(cuò)具有參考借鑒價(jià)值,需要的朋友可以參考下2016-06-06
使用Spirng Boot Admin監(jiān)控Spring Cloud應(yīng)用項(xiàng)目
這篇文章主要介紹了使用Spirng Boot Admin監(jiān)控Spring Cloud應(yīng)用項(xiàng)目,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-05-05
SpringBoot3整合Swagger3時(shí)出現(xiàn)Type javax.servlet.http.H的ttpSe
這篇文章主要介紹了SpringBoot3整合Swagger3時(shí)出現(xiàn)Type javax.servlet.http.H的ttpServletRequest not present錯(cuò)誤解決方法,文中有詳細(xì)的解決方法,需要的朋友可以參考下2025-01-01
Spring Core核心類庫的功能與應(yīng)用實(shí)踐分析
本文詳細(xì)介紹了SpringCore核心類庫的功能、應(yīng)用實(shí)踐和底層原理,SpringCore提供了控制反轉(zhuǎn)(IOC)、依賴注入(DI)、Bean管理以及JNDI、定時(shí)任務(wù)等企業(yè)級功能,文章通過多個(gè)Java示例展示了SpringCore的應(yīng)用,感興趣的朋友跟隨小編一起看看吧2024-12-12

