這是一個建立於 的文章,其中的資訊可能已經有所發展或是發生改變。
在Go語言世界中,日誌庫並不像Java世界那裡有一個具有統治力的日誌庫。在做新項目技術選型的時候,難免會遇到日誌庫的選擇問題,今天筆者就為大家介紹一下在Github中stars數最多的Go日誌庫。
logrus是我已知的Go語言日誌庫中在Github中stars數最多的日誌庫,功能強大,效能也不錯。另外值得一提的是logrus的一個fork庫gogap/logrus,可以配置輸出到檔案和graylog日誌系統,基礎的level、調用鏈、行號,檔案切分都有,特色是結構化日誌可以被統計和索引,藉助graylog就可以做日誌分析,不過這個庫很久沒有維護了。
zap是Go中一個快速,結構化,分級日誌庫,Uber出品。能夠啟用強大的ad-hoc分析, 靈活的儀表板, 其功能超過其他工具ELK, Splunk, 和 Sentry. 所有日誌訊息都是JSON-serialized。也是目前筆者使用的日誌庫之一。
zerolog也是一款效能相當好的日誌庫,有趣的是zerolog和zap都在自家的Github庫首頁中的效能對比資料裡打敗了對方:)
Seelog是一個原生Go日誌庫,提供了靈活的非同步調度、過濾和格式化。這也是筆者較早期使用的Go日誌庫之一。
blog4go是高效能日誌庫。創新地使用“邊解析邊輸出”方法進行日誌輸出,同時支援回呼函數、日誌淘汰和設定檔。可以解決高並發,調用日誌函數頻繁的情境下,日誌庫造成的效能問題。
有一些Github庫star數並不多,但是功能卻很多,例如mkideal/log、go-log、alog等就是功能十分齊全的日誌庫,簡單易用。還有一些是對開發工程中有些小協助的庫,例如happierall/l。
下面是我整理的Go語言日誌庫列表,大家可以依據自己項目的實際需求選擇(如果您還有好用的Go日誌庫推薦,請留言分享)。
| Project Name |
Stars |
Forks |
Description |
| logrus |
5471 |
775 |
Structured, pluggable logging for Go. |
| zap |
2761 |
175 |
Blazing fast, structured, leveled logging in Go. |
| oklog |
1914 |
61 |
A distributed and coördination-free log management system |
| glog |
1496 |
307 |
Leveled execution logs for Go |
| Seelog |
960 |
173 |
Seelog is a native Go logging library that provides flexible asynchronous dispatching, filtering, and formatting. |
| log15 |
625 |
79 |
Structured, composable logging for Go |
| zerolog |
444 |
18 |
Zero Allocation JSON Logger |
| apex/log |
433 |
38 |
Structured logging package for Go. |
| log |
215 |
14 |
Simple, configurable and scalable Structured Logging for Go. |
| blog4go |
189 |
31 |
BLog4go is an efficient logging library written in the Go programming language, providing logging hook, log rotate, filtering and formatting log message. |
| logutils |
176 |
18 |
Utilities for slightly better logging in Go (Golang). |
| log4go |
161 |
109 |
Logging package similar to log4j for the Go programming language |
| fileLogger |
80 |
27 |
fileLogger是一個基於Go開發的可自動分割檔案進行備份的非同步日誌庫 |
| gogap/logrus |
75 |
775 |
Obsolete, Please refer to gogap/logrus_mate |
| ozzo-log |
74 |
17 |
A Go (golang) package providing high-performance asynchronous logging, message filtering by severity and category, and multiple message targets. |
| azer/logger |
74 |
9 |
Minimalistic logging library for Go. |
| alexcesaro/log |
42 |
4 |
Logging packages for Go |
| happierall/l |
33 |
3 |
Golang Pretty Logger.Custom go logger for pretty print, log, debug, warn, error with colours and levels. |
| mkideal/log |
33 |
2 |
pluginable, fast,structrued and leveled logging package |
| slf |
33 |
1 |
Structured Logging Facade (SLF) for Go |
| logex |
29 |
6 |
An golang log lib, supports tracking and level, wrap by standard log lib |
| gologger |
26 |
5 |
Simple Logger for golang. Logs Into console, file or Elasticsearch. Simple, easy to use. |
| go-log |
24 |
9 |
A logger, for Go |
| slog |
22 |
1 |
The reference SLF (structured logging facade) implementation for Go |
| cxr29/log |
18 |
14 |
log - Go level and rotate log |
| ulog |
15 |
2 |
ulog - Structured and context based logging for golang |
| siddontang/go-log |
14 |
5 |
a golang log lib supports level and multi handlers |
| ccpaging/log4go |
13 |
109 |
Logging package similar to log4j for the Go programming language |
| mlog |
11 |
9 |
A simple logging module for go, with a rotating file feature and console logging. |
| alog |
6 |
2 |
Golang async log package |
| golog |
5 |
1 |
golog is a multilayer & leveled & structured logger for golang. |
| szxp/log |
5 |
0 |
A small structured logging library for Golang |
| go-async-log |
4 |
2 |
Golang非同步日誌庫,支援非同步批量寫入,按天或者小時自動切割,錯誤等級,多檔案等 |
| log |
1 |
1 |
一個基於Golang的日誌模組 |
最後是兩個benchmark庫,提供的部分Go日誌庫的效能對比資料,供大家做技術選型時參考
- https://github.com/hnakamur/go-log-benchmarks
- https://github.com/imkira/go-loggers-bench