golang第三方日志包seelog配置文件详解
開發(fā)任何項(xiàng)目,都離不開日志,配好自己的項(xiàng)目日志輸出,往往是開發(fā)項(xiàng)目的前提。在golang中,seelog應(yīng)該是比較有名的日志處理包了,功能非常強(qiáng)大,seelog官方文檔
一、seelog主要功能
下面我們看看seelog有啥強(qiáng)大
設(shè)置不同級(jí)別的日志;
輸出到終端或文件;
過濾指定級(jí)別日志;
定義多種不同的日志輸出格式;
根據(jù)觸發(fā)日志的文件名或者函數(shù)名來區(qū)別輸出日志;
通過 SMTP 或 TCP 轉(zhuǎn)發(fā)日志(網(wǎng)絡(luò)轉(zhuǎn)發(fā)日志);
滾動(dòng)日志文件(過期日志自動(dòng)清除)。
二、安裝seelog
$ go get github.com/cihub/seelog
?
三、使用seelog
我們先看看一個(gè)官方的demo
package main
import (
log "github.com/cihub/seelog"
)
func main() {
defer log.Flush()
log.Info("Hello from Seelog!")
}
很簡(jiǎn)單是吧,導(dǎo)入包,就直接可以用了,下面我們看看如何通過配置文件高度定制我們的日志。
import (
log "github.com/cihub/seelog"
)
func SetupLogger() {
logger, err := log.LoggerFromConfigAsFile("seelog.xml")
if err != nil {
return
}
log.ReplaceLogger(logger)
}
seelog的配置文件一般用xml,下面我們看seelog.xml配置文件的大體框架
<seelog>
<exceptions>
<exception ... />
<exception ... />
...
</exceptions>
<outputs>
<splitter>
<console/>
<file ... />
<rollingfile ... />
<smtp>
...
</smtp>
<buffered>
...
</buffered>
<conn ... />
</splitter>
<filter>
...
</filter>
</outputs>
<formats>
<format ... />
<format ... />
...
</formats>
</seelog>
對(duì)框架有了解后,下面我們拿官方的實(shí)例配置文件來做說明:https://github.com/cihub/seelog/wiki/Example-config
<!-- type 設(shè)置記錄器類型 https://github.com/cihub/seelog/wiki/Logger-types-reference
minlevel 設(shè)置日志最低級(jí)別; maxlevel 設(shè)置日志的最高級(jí)別
也可以通過 <seelog levels="trace,info,critical"> 設(shè)置日記級(jí)別 -->
<seelog type="asynctimer" asyncinterval="5000000" minlevel="debug" maxlevel="error">
<exceptions>
<!-- <exception> 是為特定的程序文件(filepattern)或函數(shù)(funcpattern)設(shè)定特殊的日志規(guī)則 -->
<exception funcpattern="*main.test*Something*" minlevel="info"/>
<exception filepattern="*main.go" minlevel="error"/>
</exceptions>
<!-- <outputs> formatid 指定日志輸出的格式(格式在<formats>標(biāo)簽中定義) -->
<outputs formatid="main">
<!-- <console> 標(biāo)簽表示輸出到終端 -->
<console/>
<!-- <splitter> 用于細(xì)分<outputs>日志格式,內(nèi)部支持:file(文件), rollingfile(滾動(dòng)文件,自動(dòng)清除過期),
buffered(日志寫到內(nèi)存再寫到文件), smtp(發(fā)送日志到郵件), con(網(wǎng)絡(luò)轉(zhuǎn)發(fā)) -->
<splitter formatid="format1">
<!-- log.log, log2.log將按<formats>標(biāo)簽中的id="format1"格式寫入 -->
<file path="log.log"/>
<file path="log2.log"/>
</splitter>
<splitter formatid="format2">
<file path="log3.log"/>
<file path="log4.log"/>
</splitter>
<!-- <rollingfile>滾動(dòng)文件(定期清除過期日志)
formatid: 指定日志格式; type="size" 按大小; maxsize: 單日志文件最大大小; maxrools: 最大文件數(shù) -->
<rollingfile formatid="someformat" type="size" filename="./log/roll.log" maxsize="100" maxrolls="5" />
<!-- <buffered> 將日志先存在內(nèi)存中,定期寫入文件,適合日志并發(fā)量較大或 IO 比較緊張的場(chǎng)合
size: 緩存大小; flushperiod: 緩存間隔(毫秒) -->
<buffered formatid="testlevels" size="10000" flushperiod="1000">
<file path="./log/bufFileFlush.log"/>
</buffered>
<!-- <filter>用于單獨(dú)處理某級(jí)別日志
過濾日志,把級(jí)別是error的通過郵件smtp方式發(fā)送出去(一般會(huì)發(fā)給相應(yīng)的運(yùn)維人員) -->
<filter levels="error">
<file path="./log/error.log"/>
<smtp senderaddress="noreply-notification-service@none.org"
sendername="Automatic notification service"
hostname="mail.none.org"
hostport="587"
username="nns"
password="123">
<recipient address="john-smith@none.com"/>
<recipient address="hans-meier@none.com"/>
</smtp>
<!-- 按tcp4網(wǎng)絡(luò)協(xié)議發(fā)送日志 -->
<conn net="tcp4" addr="server.address:5514" tls="true" insecureskipverify="true" />
</filter>
</outputs>
<!-- <formats> 定制日志的輸出格式
https://github.com/cihub/seelog/wiki/Format-reference -->
<formats>
<format id="main" format="%Date(2006 Jan 02/3:04:05.000000000 PM MST) [%Level] %Msg%n"/>
<format id="someformat" format="%Ns [%Level] %Msg%n"/>
<format id="testlevels" format="%Level %Lev %LEVEL %LEV %l %Msg%n"/>
<format id="usetags" format="<msg>%Msg</time>"/>
<format id="format1" format="%Date/%Time [%LEV] %Msg%n"/>
<format id="format2" format="%File %FullPath %RelFile %Msg%n"/>
</formats>
</seelog>
初次接觸難免會(huì)覺得配置文件的內(nèi)容有點(diǎn)多,難以理解,其實(shí)只要我們理解它配置文件的大體框架,然后自己試著更改配置文件,看看輸出什么,就很快明白了。
<!-- 我只需要把日志按指定格式輸出到終端 -->
<seelog type="asynctimer" asyncinterval="1000000" minlevel="debug" maxlevel="error">
<outputs formatid="main">
<!-- 僅輸出到終端 -->
<console/>
</outputs>
<formats>
<!-- 設(shè)置格式 -->
<format id="main" format="%UTCDate %UTCTime - [%LEV] - %RelFile - l%Line - %Msg%n"/>
</formats>
</seelog>
<!-- ****** 我是分割線 ***** -->
<!-- 現(xiàn)在我想把日志輸出到終端同時(shí)也把日志輸出到文件 -->
<seelog type="asynctimer" asyncinterval="1000000" minlevel="debug" maxlevel="error">
<outputs formatid="main">
<console/>
<!-- 輸出到文件,且不同于終端的日志格式 -->
<splitter formatid="format1">
<file path="log.log"/>
</splitter>
</outputs>
<formats>
<!-- 設(shè)置格式 -->
<format id="main" format="%UTCDate %UTCTime - [%LEV] - %RelFile - l%Line - %Msg%n"/>
<format id="format1" format="%Date(2006 Jan 02/3:04:05.000000000 PM MST) [%Level] %Msg%n"/>
</formats>
</seelog>
?
轉(zhuǎn)載于:https://www.cnblogs.com/williamjie/p/10048159.html
總結(jié)
以上是生活随笔為你收集整理的golang第三方日志包seelog配置文件详解的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: scrollBy与scrollTo的区别
- 下一篇: [Xcode 实际操作]七、文件与数据-