golang日志框架之logrus


golang日志庫
golang標准庫的日志框架非常簡單,僅僅提供了print,panic和fatal三個函數對於更精細的日志級別、日志文件分割以及日志分發等方面並沒有提供支持。所以催生了很多第三方的日志庫,但是在golang的世界里,沒有一個日志庫像slf4j那樣在Java中具有絕對統治地位。golang中,流行的日志框架包括logrus、zap、zerolog、seelog等。
logrus是目前Github上star數量最多的日志庫,目前(2018.08,下同)star數量為8119,fork數為1031。logrus功能強大,性能高效,而且具有高度靈活性,提供了自定義插件的功能。很多開源項目,如docker,prometheus等,都是用了logrus來記錄其日志。
zap是Uber推出的一個快速、結構化的分級日志庫。具有強大的ad-hoc分析功能,並且具有靈活的儀表盤。zap目前在GitHub上的star數量約為4.3k。
seelog提供了靈活的異步調度、格式化和過濾功能。目前在GitHub上也有約1.1k。

logrus特性
logrus具有以下特性:

完全兼容golang標准庫日志模塊:logrus擁有六種日志級別:debug、info、warn、error、fatal和panic,這是golang標准庫日志模塊的API的超集。如果您的項目使用標准庫日志模塊,完全可以以最低的代價遷移到logrus上。
可擴展的Hook機制:允許使用者通過hook的方式將日志分發到任意地方,如本地文件系統、標准輸出、logstash、elasticsearch或者mq等,或者通過hook定義日志內容和格式等。
可選的日志輸出格式:logrus內置了兩種日志格式,JSONFormatter和TextFormatter,如果這兩個格式不滿足需求,可以自己動手實現接口Formatter,來定義自己的日志格式。
Field機制:logrus鼓勵通過Field機制進行精細化的、結構化的日志記錄,而不是通過冗長的消息來記錄日志。
logrus是一個可插拔的、結構化的日志框架。
logrus的使用
第一個示例
最簡單的使用logrus的示例如下:

package main

import (
log "github.com/sirupsen/logrus"
)

func main() {
log.WithFields(log.Fields{
"animal": "walrus",
}).Info("A walrus appears")
}
1
2
3
4
5
6
7
8
9
10
11
上面代碼執行后,標准輸出上輸出如下:

time="2018-08-11T15:42:22+08:00" level=info msg="A walrus appears" animal=walrus
1
logrus與golang標准庫日志模塊完全兼容,因此您可以使用log“github.com/sirupsen/logrus”替換所有日志導入。
logrus可以通過簡單的配置,來定義輸出、格式或者日志級別等。

package main

import (
"os"
log "github.com/sirupsen/logrus"
)

func init() {
// 設置日志格式為json格式
log.SetFormatter(&log.JSONFormatter{})

// 設置將日志輸出到標准輸出(默認的輸出為stderr,標准錯誤)
// 日志消息輸出可以是任意的io.writer類型
log.SetOutput(os.Stdout)

// 設置日志級別為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!")
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
Logger
logger是一種相對高級的用法, 對於一個大型項目, 往往需要一個全局的logrus實例,即logger對象來記錄項目所有的日志。如:

package main

import (
"github.com/sirupsen/logrus"
"os"
)

// logrus提供了New()函數來創建一個logrus的實例。
// 項目中,可以創建任意數量的logrus實例。
var log = logrus.New()

func main() {
// 為當前logrus實例設置消息的輸出,同樣地,
// 可以設置logrus實例的輸出到任意io.writer
log.Out = os.Stdout

// 為當前logrus實例設置消息輸出格式為json格式。
// 同樣地,也可以單獨為某個logrus實例設置日志級別和hook,這里不詳細敘述。
log.Formatter = &logrus.JSONFormatter{}

log.WithFields(logrus.Fields{
"animal": "walrus",
"size": 10,
}).Info("A group of walrus emerges from the ocean")
}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
Fields
前一章提到過,logrus不推薦使用冗長的消息來記錄運行信息,它推薦使用Fields來進行精細化的、結構化的信息記錄。
例如下面的記錄日志的方式:

log.Fatalf("Failed to send event %s to topic %s with key %d", event, topic, key)
1
在logrus中不太提倡,logrus鼓勵使用以下方式替代之:

log.WithFields(log.Fields{
"event": event,
"topic": topic,
"key": key,
}).Fatal("Failed to send event")
1
2
3
4
5
前面的WithFields API可以規范使用者按照其提倡的方式記錄日志。但是WithFields依然是可選的,因為某些場景下,使用者確實只需要記錄儀一條簡單的消息。

通常,在一個應用中、或者應用的一部分中,都有一些固定的Field。比如在處理用戶http請求時,上下文中,所有的日志都會有request_id和user_ip。為了避免每次記錄日志都要使用log.WithFields(log.Fields{"request_id": request_id, "user_ip": user_ip}),我們可以創建一個logrus.Entry實例,為這個實例設置默認Fields,在上下文中使用這個logrus.Entry實例記錄日志即可。

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")
1
2
3
Hook
logrus最令人心動的功能就是其可擴展的HOOK機制了,通過在初始化時為logrus添加hook,logrus可以實現各種擴展功能。

Hook接口
logrus的hook接口定義如下,其原理是每此寫入日志時攔截,修改logrus.Entry。

// logrus在記錄Levels()返回的日志級別的消息時會觸發HOOK,
// 按照Fire方法定義的內容修改logrus.Entry。
type Hook interface {
Levels() []Level
Fire(*Entry) error
}
1
2
3
4
5
6
一個簡單自定義hook如下,DefaultFieldHook定義會在所有級別的日志消息中加入默認字段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
}
1
2
3
4
5
6
7
8
9
10
11
hook的使用也很簡單,在初始化前調用log.AddHook(hook)添加相應的hook即可。

logrus官方僅僅內置了syslog的hook。
此外,但Github也有很多第三方的hook可供使用,文末將提供一些第三方HOOK的連接。

記錄文件名和行號
logrus的一個很致命的問題就是沒有提供文件名和行號,這在大型項目中通過日志定位問題時有諸多不便。Github上的logrus的issue#63:Log filename and line number創建於2014年,四年過去了仍是open狀態~~~
網上給出的解決方案分位兩類,一就是自己實現一個hook;二就是通過裝飾器包裝logrus.Entry。兩種方案網上都有很多代碼,但是大多無法正常工作。但總體來說,解決問題的思路都是對的:通過標准庫的runtime模塊獲取運行時信息,並從中提取文件名,行號和調用函數名。

標准庫runtime模塊的Caller(skip int)函數可以返回當前goroutine調用棧中的文件名,行號,函數信息等,參數skip表示表示返回的棧幀的層次,0表示runtime.Caller的調用着。返回值包括響應棧幀層次的pc(程序計數器),文件名和行號信息。為了提高效率,我們先通過跟蹤調用棧發現,從runtime.Caller()的調用者開始,到記錄日志的生成代碼之間,大概有8到11層左右,所有我們在hook中循環第8到11層調用棧應該可以找到日志記錄的生產代碼。

此外,runtime.FuncForPC(pc uintptr) *Func可以返回指定pc的函數信息。
所有我們要實現的hook也是基於以上原理,使用runtime.Caller()依次循環調用棧的第7~11層,過濾掉sirupsen包內容,那么第一個非siupsenr包就認為是我們的生產代碼了,並返回pc以便通過runtime.FuncForPC()獲取函數名稱。然后將文件名、行號和函數名組裝為source字段塞到logrus.Entry中即可。

import (
"fmt"
log "github.com/sirupsen/logrus"
"runtime"
"strings"
)

// line number hook for log the call context,
type lineHook struct {
Field string
// skip為遍歷調用棧開始的索引位置
Skip int
levels []log.Level
}

// Levels implement levels
func (hook lineHook) Levels() []log.Level {
return log.AllLevels
}

// Fire implement fire
func (hook lineHook) Fire(entry *log.Entry) error {
entry.Data[hook.Field] = findCaller(hook.Skip)
return nil
}

func findCaller(skip int) string {
file := ""
line := 0
var pc uintptr
// 遍歷調用棧的最大索引為第11層.
for i := 0; i < 11; i++ {
file, line, pc = getCaller(skip + i)
// 過濾掉所有logrus包,即可得到生成代碼信息
if !strings.HasPrefix(file, "logrus") {
break
}
}

fullFnName := runtime.FuncForPC(pc)

fnName := ""
if fullFnName != nil {
fnNameStr := fullFnName.Name()
// 取得函數名
parts := strings.Split(fnNameStr, ".")
fnName = parts[len(parts)-1]
}

return fmt.Sprintf("%s:%d:%s()", file, line, fnName)
}

func getCaller(skip int) (string, int, uintptr) {
pc, file, line, ok := runtime.Caller(skip)
if !ok {
return "", 0, pc
}
n := 0

// 獲取包名
for i := len(file) - 1; i > 0; i-- {
if file[i] == '/' {
n++
if n >= 2 {
file = file[i+1:]
break
}
}
}
return file, line, pc
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
效果如下:

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()"
1
2
3
日志本地文件分割
logrus本身不帶日志本地文件分割功能,但是我們可以通過file-rotatelogs進行日志本地文件分割。 每次當我們寫入日志的時候,logrus都會調用file-rotatelogs來判斷日志是否要進行切分。關於本地日志文件分割的例子網上很多,這里不再詳細介紹,奉上代碼:

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為最新的日志建立軟連接,以方便隨着找到當前日志文件
rotatelogs.WithLinkName(logName),

// WithRotationTime設置日志分割的時間,這里設置為一小時分割一次
rotatelogs.WithRotationTime(time.Hour),

// WithMaxAge和WithRotationCount二者只能設置一個,
// WithMaxAge設置文件清理前的最長保存時間,
// WithRotationCount設置文件清理前最多保存的個數。
//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
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
使用上述本地日志文件切割的效果如下:


將日志發送到elasticsearch
將日志發送到elasticsearch是很多日志監控系統的選擇,將logrus日志發送到elasticsearch的原理是在hook的每次fire調用時,使用golang的es客戶端將日志信息寫到elasticsearch。elasticsearch官方沒有提供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())
1
2
3
4
5
6
7
8
9
10
11
12
13
考慮到logrus的Fields機制,可以實現如下數據格式:

msg := struct {
Host string
Timestamp string `json:"@timestamp"`
Message string
Data logrus.Fields
Level string
}
1
2
3
4
5
6
7
其中Host記錄產生日志主機信息,在創建hook是指定。其他數據需要從logrus.Entry中取得。測試過程我們選擇按照此原理實現的第三方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)
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
從Elasticsearch查詢得到日志存儲,效果如下:

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"
}
}
]
}
}

Response code: 200 (OK); Time: 16ms; Content length: 3039 bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
將日志發送到其他位置
將日志發送到日志中心也是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我這里沒有具體驗證,大家可以根據需要自行嘗試。

其他注意事項
Fatal處理
和很多日志框架一樣,logrus的Fatal系列函數會執行os.Exit(1)。但是logrus提供可以注冊一個或多個fatal handler函數的接口logrus.RegisterExitHandler(handler func() {} ),讓logrus在執行os.Exit(1)之前進行相應的處理。fatal handler可以在系統異常時調用一些資源釋放api等,讓應用正確的關閉。

線程安全
默認情況下,logrus的api都是線程安全的,其內部通過互斥鎖來保護並發寫。互斥鎖工作於調用hooks或者寫日志的時候,如果不需要鎖,可以調用logger.SetNoLock()來關閉之。可以關閉logrus互斥鎖的情形包括:

沒有設置hook,或者所有的hook都是線程安全的實現。
寫日志到logger.Out已經是線程安全的了,如logger.Out已經被鎖保護,或者寫文件時,文件是以O_APPEND方式打開的,並且每次寫操作都小於4k。
————————————————
版權聲明:本文為CSDN博主「jack-life」的原創文章,遵循 CC 4.0 BY-SA 版權協議,轉載請附上原文出處鏈接及本聲明。
原文鏈接:https://blog.csdn.net/wslyk606/article/details/81670713


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM