golang日志庫
golang標(biāo)準(zhǔn)庫的日志框架非常簡單,僅僅提供了print,panic和fatal三個(gè)函數(shù)對(duì)于更精細(xì)的日志級(jí)別、日志文件分割以及日志分發(fā)等方面并沒有提供支持。所以催生了很多第三方的日志庫,但是在golang的世界里,沒有一個(gè)日志庫像slf4j那樣在Java中具有絕對(duì)統(tǒng)治地位。golang中,流行的日志框架包括logrus、zap、zerolog、seelog等。
logrus是目前Github上star數(shù)量最多的日志庫,目前(2018.08,下同)star數(shù)量為8119,fork數(shù)為1031。logrus功能強(qiáng)大,性能高效,而且具有高度靈活性,提供了自定義插件的功能。很多開源項(xiàng)目,如docker,prometheus等,都是用了logrus來記錄其日志。
zap是Uber推出的一個(gè)快速、結(jié)構(gòu)化的分級(jí)日志庫。具有強(qiáng)大的ad-hoc分析功能,并且具有靈活的儀表盤。zap目前在GitHub上的star數(shù)量約為4.3k。
seelog提供了靈活的異步調(diào)度、格式化和過濾功能。目前在GitHub上也有約1.1k。
logrus特性
logrus具有以下特性:
- 完全兼容golang標(biāo)準(zhǔn)庫日志模塊:logrus擁有六種日志級(jí)別:debug、info、warn、error、fatal和panic,這是golang標(biāo)準(zhǔn)庫日志模塊的API的超集。如果您的項(xiàng)目使用標(biāo)準(zhǔn)庫日志模塊,完全可以以最低的代價(jià)遷移到logrus上。
- 可擴(kuò)展的Hook機(jī)制:允許使用者通過hook的方式將日志分發(fā)到任意地方,如本地文件系統(tǒng)、標(biāo)準(zhǔn)輸出、logstash、elasticsearch或者mq等,或者通過hook定義日志內(nèi)容和格式等。
- 可選的日志輸出格式:logrus內(nèi)置了兩種日志格式,JSONFormatter和TextFormatter,如果這兩個(gè)格式不滿足需求,可以自己動(dòng)手實(shí)現(xiàn)接口Formatter,來定義自己的日志格式。
- Field機(jī)制:logrus鼓勵(lì)通過Field機(jī)制進(jìn)行精細(xì)化的、結(jié)構(gòu)化的日志記錄,而不是通過冗長的消息來記錄日志。
- logrus是一個(gè)可插拔的、結(jié)構(gòu)化的日志框架。
logrus的使用
第一個(gè)示例
最簡單的使用logrus的示例如下:
package main
import (
log "github.com/sirupsen/logrus"
)
func main() {
log.WithFields(log.Fields{
"animal": "walrus",
}).Info("A walrus appears")
}
上面代碼執(zhí)行后,標(biāo)準(zhǔn)輸出上輸出如下:
time="2018-08-11T15:42:22+08:00" level=info msg="A walrus appears" animal=walrus
logrus與golang標(biāo)準(zhǔn)庫日志模塊完全兼容,因此您可以使用log“github.com/sirupsen/logrus”
替換所有日志導(dǎo)入。
logrus可以通過簡單的配置,來定義輸出、格式或者日志級(jí)別等。
package main
import (
"os"
log "github.com/sirupsen/logrus"
)
func init() {
// 設(shè)置日志格式為json格式
log.SetFormatter(log.JSONFormatter{})
// 設(shè)置將日志輸出到標(biāo)準(zhǔn)輸出(默認(rèn)的輸出為stderr,標(biāo)準(zhǔn)錯(cuò)誤)
// 日志消息輸出可以是任意的io.writer類型
log.SetOutput(os.Stdout)
// 設(shè)置日志級(jí)別為warn以上
log.SetLevel(log.WarnLevel)
}
func main() {
log.WithFields(log.Fields{
"animal": "walrus",
"size": 10,
}).Info("A group of walrus emerges from the ocean")
log.WithFields(log.Fields{
"omg": true,
"number": 122,
}).Warn("The group's number increased tremendously!")
log.WithFields(log.Fields{
"omg": true,
"number": 100,
}).Fatal("The ice breaks!")
}
Logger
logger是一種相對(duì)高級(jí)的用法, 對(duì)于一個(gè)大型項(xiàng)目, 往往需要一個(gè)全局的logrus實(shí)例,即logger
對(duì)象來記錄項(xiàng)目所有的日志。如:
package main
import (
"github.com/sirupsen/logrus"
"os"
)
// logrus提供了New()函數(shù)來創(chuàng)建一個(gè)logrus的實(shí)例。
// 項(xiàng)目中,可以創(chuàng)建任意數(shù)量的logrus實(shí)例。
var log = logrus.New()
func main() {
// 為當(dāng)前l(fā)ogrus實(shí)例設(shè)置消息的輸出,同樣地,
// 可以設(shè)置logrus實(shí)例的輸出到任意io.writer
log.Out = os.Stdout
// 為當(dāng)前l(fā)ogrus實(shí)例設(shè)置消息輸出格式為json格式。
// 同樣地,也可以單獨(dú)為某個(gè)logrus實(shí)例設(shè)置日志級(jí)別和hook,這里不詳細(xì)敘述。
log.Formatter = logrus.JSONFormatter{}
log.WithFields(logrus.Fields{
"animal": "walrus",
"size": 10,
}).Info("A group of walrus emerges from the ocean")
}
Fields
前一章提到過,logrus不推薦使用冗長的消息來記錄運(yùn)行信息,它推薦使用Fields
來進(jìn)行精細(xì)化的、結(jié)構(gòu)化的信息記錄。
例如下面的記錄日志的方式:
log.Fatalf("Failed to send event %s to topic %s with key %d", event, topic, key)
````
在logrus中不太提倡,logrus鼓勵(lì)使用以下方式替代之:
div class="se-preview-section-delimiter">/div>
```go
log.WithFields(log.Fields{
"event": event,
"topic": topic,
"key": key,
}).Fatal("Failed to send event")
前面的WithFields
API可以規(guī)范使用者按照其提倡的方式記錄日志。但是WithFields
依然是可選的,因?yàn)槟承﹫鼍跋拢褂谜叽_實(shí)只需要記錄儀一條簡單的消息。
通常,在一個(gè)應(yīng)用中、或者應(yīng)用的一部分中,都有一些固定的Field
。比如在處理用戶http請求時(shí),上下文中,所有的日志都會(huì)有request_id
和user_ip
。為了避免每次記錄日志都要使用log.WithFields(log.Fields{"request_id": request_id, "user_ip": user_ip})
,我們可以創(chuàng)建一個(gè)logrus.Entry
實(shí)例,為這個(gè)實(shí)例設(shè)置默認(rèn)Fields
,在上下文中使用這個(gè)logrus.Entry
實(shí)例記錄日志即可。
requestLogger := log.WithFields(log.Fields{"request_id": request_id, "user_ip": user_ip})
requestLogger.Info("something happened on that request") # will log request_id and user_ip
requestLogger.Warn("something not great happened")
Hook
logrus最令人心動(dòng)的功能就是其可擴(kuò)展的HOOK機(jī)制了,通過在初始化時(shí)為logrus添加hook,logrus可以實(shí)現(xiàn)各種擴(kuò)展功能。
Hook接口
logrus的hook接口定義如下,其原理是每此寫入日志時(shí)攔截,修改logrus.Entry。
// logrus在記錄Levels()返回的日志級(jí)別的消息時(shí)會(huì)觸發(fā)HOOK,
// 按照Fire方法定義的內(nèi)容修改logrus.Entry。
type Hook interface {
Levels() []Level
Fire(*Entry) error
}
一個(gè)簡單自定義hook如下,DefaultFieldHook
定義會(huì)在所有級(jí)別的日志消息中加入默認(rèn)字段appName="myAppName"
。
type DefaultFieldHook struct {
}
func (hook *DefaultFieldHook) Fire(entry *log.Entry) error {
entry.Data["appName"] = "MyAppName"
return nil
}
func (hook *DefaultFieldHook) Levels() []log.Level {
return log.AllLevels
}
hook的使用也很簡單,在初始化前調(diào)用log.AddHook(hook)
添加相應(yīng)的hook
即可。
logrus官方僅僅內(nèi)置了syslog的hook。
此外,但Github也有很多第三方的hook可供使用,文末將提供一些第三方HOOK的連接。
問題與解決方案
盡管logrus有諸多優(yōu)點(diǎn),但是為了靈活性和可擴(kuò)展性,官方也削減了很多實(shí)用的功能,例如:
- 沒有提供行號(hào)和文件名的支持
- 輸出到本地文件系統(tǒng)沒有提供日志分割功能
- 官方?jīng)]有提供輸出到ELK等日志處理中心的功能
但是這些功能都可以通過自定義hook來實(shí)現(xiàn)。
記錄文件名和行號(hào)
logrus的一個(gè)很致命的問題就是沒有提供文件名和行號(hào),這在大型項(xiàng)目中通過日志定位問題時(shí)有諸多不便。Github上的logrus的issue#63:Log filename and line number創(chuàng)建于2014年,四年過去了仍是open狀態(tài)~~~
網(wǎng)上給出的解決方案分位兩類,一就是自己實(shí)現(xiàn)一個(gè)hook;二就是通過裝飾器包裝logrus.Entry
。兩種方案網(wǎng)上都有很多代碼,但是大多無法正常工作。但總體來說,解決問題的思路都是對(duì)的:通過標(biāo)準(zhǔn)庫的runtime
模塊獲取運(yùn)行時(shí)信息,并從中提取文件名,行號(hào)和調(diào)用函數(shù)名。
標(biāo)準(zhǔn)庫runtime
模塊的Caller(skip int)
函數(shù)可以返回當(dāng)前goroutine調(diào)用棧中的文件名,行號(hào),函數(shù)信息等,參數(shù)skip表示表示返回的棧幀的層次,0表示runtime.Caller
的調(diào)用著。返回值包括響應(yīng)棧幀層次的pc(程序計(jì)數(shù)器),文件名和行號(hào)信息。為了提高效率,我們先通過跟蹤調(diào)用棧發(fā)現(xiàn),從runtime.Caller()
的調(diào)用者開始,到記錄日志的生成代碼之間,大概有8到11層左右,所有我們在hook中循環(huán)第8到11層調(diào)用棧應(yīng)該可以找到日志記錄的生產(chǎn)代碼。
此外,runtime.FuncForPC(pc uintptr) *Func
可以返回指定pc
的函數(shù)信息。
所有我們要實(shí)現(xiàn)的hook也是基于以上原理,使用runtime.Caller()
依次循環(huán)調(diào)用棧的第7~11層,過濾掉sirupsen
包內(nèi)容,那么第一個(gè)非siupsenr
包就認(rèn)為是我們的生產(chǎn)代碼了,并返回pc
以便通過runtime.FuncForPC()
獲取函數(shù)名稱。然后將文件名、行號(hào)和函數(shù)名組裝為source
字段塞到logrus.Entry
中即可。
time="2018-08-11T19:10:15+08:00" level=warning msg="postgres_exporter is ready for scraping on 0.0.0.0:9295..." source="postgres_exporter/main.go:60:main()"
time="2018-08-11T19:10:17+08:00" level=error msg="!!!msb info not found" source="postgres/postgres_query.go:63:QueryPostgresInfo()"
time="2018-08-11T19:10:17+08:00" level=error msg="get postgres instances info failed, scrape metrics failed, error:msb env not found" source="collector/exporter.go:71:Scrape()"
日志本地文件分割
logrus本身不帶日志本地文件分割功能,但是我們可以通過file-rotatelogs
進(jìn)行日志本地文件分割。 每次當(dāng)我們寫入日志的時(shí)候,logrus都會(huì)調(diào)用file-rotatelogs
來判斷日志是否要進(jìn)行切分。關(guān)于本地日志文件分割的例子網(wǎng)上很多,這里不再詳細(xì)介紹,奉上代碼:
import (
"github.com/lestrrat-go/file-rotatelogs"
"github.com/rifflock/lfshook"
log "github.com/sirupsen/logrus"
"time"
)
func newLfsHook(logLevel *string, maxRemainCnt uint) log.Hook {
writer, err := rotatelogs.New(
logName+".%Y%m%d%H",
// WithLinkName為最新的日志建立軟連接,以方便隨著找到當(dāng)前日志文件
rotatelogs.WithLinkName(logName),
// WithRotationTime設(shè)置日志分割的時(shí)間,這里設(shè)置為一小時(shí)分割一次
rotatelogs.WithRotationTime(time.Hour),
// WithMaxAge和WithRotationCount二者只能設(shè)置一個(gè),
// WithMaxAge設(shè)置文件清理前的最長保存時(shí)間,
// WithRotationCount設(shè)置文件清理前最多保存的個(gè)數(shù)。
//rotatelogs.WithMaxAge(time.Hour*24),
rotatelogs.WithRotationCount(maxRemainCnt),
)
if err != nil {
log.Errorf("config local file system for logger error: %v", err)
}
level, ok := logLevels[*logLevel]
if ok {
log.SetLevel(level)
} else {
log.SetLevel(log.WarnLevel)
}
lfsHook := lfshook.NewHook(lfshook.WriterMap{
log.DebugLevel: writer,
log.InfoLevel: writer,
log.WarnLevel: writer,
log.ErrorLevel: writer,
log.FatalLevel: writer,
log.PanicLevel: writer,
}, log.TextFormatter{DisableColors: true})
return lfsHook
}
使用上述本地日志文件切割的效果如下:
![](/d/20211017/dd629f004470e6fcd31f2b8f31adee2c.gif)
將日志發(fā)送到elasticsearch
將日志發(fā)送到elasticsearch是很多日志監(jiān)控系統(tǒng)的選擇,將logrus日志發(fā)送到elasticsearch的原理是在hook的每次fire調(diào)用時(shí),使用golang的es客戶端將日志信息寫到elasticsearch。elasticsearch官方?jīng)]有提供golang客戶端,但是有很多第三方的go語言客戶端可供使用,我們選擇elastic。elastic提供了豐富的文檔,以及Java中的流式接口,使用起來非常方便。
client, err := elastic.NewClient(elastic.SetURL("http://localhost:9200"))
if err != nil {
log.Panic(err)
}
// Index a tweet (using JSON serialization)
tweet1 := Tweet{User: "olivere", Message: "Take Five", Retweets: 0}
put1, err := client.Index().
Index("twitter").
Type("tweet").
Id("1").
BodyJson(tweet1).
Do(context.Background())
考慮到logrus的Fields機(jī)制,可以實(shí)現(xiàn)如下數(shù)據(jù)格式:
msg := struct {
Host string
Timestamp string `json:"@timestamp"`
Message string
Data logrus.Fields
Level string
}
其中Host
記錄產(chǎn)生日志主機(jī)信息,在創(chuàng)建hook是指定。其他數(shù)據(jù)需要從logrus.Entry
中取得。測試過程我們選擇按照此原理實(shí)現(xiàn)的第三方HOOK:elogrus。其使用如下:
import (
"github.com/olivere/elastic"
"gopkg.in/sohlich/elogrus"
)
func initLog() {
client, err := elastic.NewClient(elastic.SetURL("http://localhost:9200"))
if err != nil {
log.Panic(err)
}
hook, err := elogrus.NewElasticHook(client, "localhost", log.DebugLevel, "mylog")
if err != nil {
log.Panic(err)
}
log.AddHook(hook)
}
從Elasticsearch查詢得到日志存儲(chǔ),效果如下:
GET http://localhost:9200/mylog/_search
HTTP/1.1 200 OK
content-type: application/json; charset=UTF-8
transfer-encoding: chunked
{
"took": 1,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"failed": 0
},
"hits": {
"total": 2474,
"max_score": 1.0,
"hits": [
{
"_index": "mylog",
"_type": "log",
"_id": "AWUw13jWnMZReb-jHQup",
"_score": 1.0,
"_source": {
"Host": "localhost",
"@timestamp": "2018-08-13T01:12:32.212818666Z",
"Message": "!!!msb info not found",
"Data": {},
"Level": "ERROR"
}
},
{
"_index": "mylog",
"_type": "log",
"_id": "AWUw13jgnMZReb-jHQuq",
"_score": 1.0,
"_source": {
"Host": "localhost",
"@timestamp": "2018-08-13T01:12:32.223103348Z",
"Message": "get postgres instances info failed, scrape metrics failed, error:msb env not found",
"Data": {
"source": "collector/exporter.go:71:Scrape()"
},
"Level": "ERROR"
}
},
//...
{
"_index": "mylog",
"_type": "log",
"_id": "AWUw2f1enMZReb-jHQu_",
"_score": 1.0,
"_source": {
"Host": "localhost",
"@timestamp": "2018-08-13T01:15:17.212546892Z",
"Message": "!!!msb info not found",
"Data": {
"source": "collector/exporter.go:71:Scrape()"
},
"Level": "ERROR"
}
},
{
"_index": "mylog",
"_type": "log",
"_id": "AWUw2NhmnMZReb-jHQu1",
"_score": 1.0,
"_source": {
"Host": "localhost",
"@timestamp": "2018-08-13T01:14:02.21276903Z",
"Message": "!!!msb info not found",
"Data": {},
"Level": "ERROR"
}
}
]
}
}
將日志發(fā)送到其他位置
將日志發(fā)送到日志中心也是logrus所提倡的,雖然沒有提供官方支持,但是目前Github上有很多第三方hook可供使用:
logrus_amqp:Logrus hook for Activemq。
logrus-logstash-hook:Logstash hook for logrus。
mgorus:Mongodb Hooks for Logrus。
logrus_influxdb:InfluxDB Hook for Logrus。
logrus-redis-hook:Hook for Logrus which enables logging to RELK stack (Redis, Elasticsearch, Logstash and Kibana)。
等等,上述第三方hook我這里沒有具體驗(yàn)證,大家可以根據(jù)需要自行嘗試。
其他注意事項(xiàng)
Fatal處理
和很多日志框架一樣,logrus的Fatal
系列函數(shù)會(huì)執(zhí)行os.Exit(1)
。但是logrus提供可以注冊一個(gè)或多個(gè)fatal handler
函數(shù)的接口logrus.RegisterExitHandler(handler func() {} )
,讓logrus在執(zhí)行os.Exit(1)
之前進(jìn)行相應(yīng)的處理。fatal handler
可以在系統(tǒng)異常時(shí)調(diào)用一些資源釋放api等,讓應(yīng)用正確的關(guān)閉。
線程安全
默認(rèn)情況下,logrus的api都是線程安全的,其內(nèi)部通過互斥鎖來保護(hù)并發(fā)寫?;コ怄i工作于調(diào)用hooks或者寫日志的時(shí)候,如果不需要鎖,可以調(diào)用logger.SetNoLock()
來關(guān)閉之。可以關(guān)閉logrus互斥鎖的情形包括:
- 沒有設(shè)置hook,或者所有的hook都是線程安全的實(shí)現(xiàn)。
- 寫日志到
logger.Out
已經(jīng)是線程安全的了,如logger.Out
已經(jīng)被鎖保護(hù),或者寫文件時(shí),文件是以O_APPEND
方式打開的,并且每次寫操作都小于4k。
以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
您可能感興趣的文章:- mac下安裝golang框架iris的方法
- golang常用庫之操作數(shù)據(jù)庫的orm框架-gorm基本使用詳解
- golang 網(wǎng)絡(luò)框架之gin的使用方法
- 詳解Golang Iris框架的基本使用