Go Logger一個簡單而強大的 golang 日志工具包
go-logger
一個簡單而強大的 golang 日志工具包
功能
- 支持同時輸出到 console, file, url
- 命令行輸出字體可帶顏色
- 文件輸出支持根據 文件大小,文件行數,日期三種方式切分
- 文件輸出支持根據日志級別分別保存到不同的文件
- 支持異步和同步兩種方式寫入
- 支持 json 格式化輸出
- 代碼設計易擴展,可根據需要設計自己的 adapter
安裝使用
go get github.com/phachon/go-logger go get ./...
環(huán)境需要
go 1.8
支持輸出
- console // 輸出到命令行
- file // 文件
- api // http url 接口
- ...
快速使用
- 同步方式
import (
"github.com/phachon/go-logger"
)
func main() {
logger := go_logger.NewLogger()
logger.Info("this is a info log!")
logger.Errorf("this is a error %s log!", "format")
}
- 異步方式
import (
"github.com/phachon/go-logger"
)
func main() {
logger := go_logger.NewLogger()
logger.SetAsync()
logger.Info("this is a info log!")
logger.Errorf("this is a error %s log!", "format")
// 程序結束前必須調用 Flush
logger.Flush()
}
- 多個輸出
import (
"github.com/phachon/go-logger"
)
func main() {
logger := go_logger.NewLogger()
logger.Detach("console")
// 命令行輸出配置
consoleConfig := &go_logger.ConsoleConfig{
Color: true, // 命令行輸出字符串是否顯示顏色
JsonFormat: true, // 命令行輸出字符串是否格式化
Format: "" // 如果輸出的不是 json 字符串,JsonFormat: false, 自定義輸出的格式
}
// 添加 console 為 logger 的一個輸出
logger.Attach("console", go_logger.LOGGER_LEVEL_DEBUG, consoleConfig)
// 文件輸出配置
fileConfig := &go_logger.FileConfig {
Filename : "./test.log", // 日志輸出文件名,不自動存在
// 如果要將單獨的日志分離為文件,請配置LealFrimeNem參數。
LevelFileName : map[int]string {
logger.LoggerLevel("error"): "./error.log", // Error 級別日志被寫入 error .log 文件
logger.LoggerLevel("info"): "./info.log", // Info 級別日志被寫入到 info.log 文件中
logger.LoggerLevel("debug"): "./debug.log", // Debug 級別日志被寫入到 debug.log 文件中
},
MaxSize : 1024 * 1024, // 文件最大值(KB),默認值0不限
MaxLine : 100000, // 文件最大行數,默認 0 不限制
DateSlice : "d", // 文件根據日期切分, 支持 "Y" (年), "m" (月), "d" (日), "H" (時), 默認 "no", 不切分
JsonFormat: true, // 寫入文件的數據是否 json 格式化
Format: "" // 如果寫入文件的數據不 json 格式化,自定義日志格式
}
// 添加 file 為 logger 的一個輸出
logger.Attach("file", go_logger.LOGGER_LEVEL_DEBUG, fileConfig)
logger.Info("this is a info log!")
logger.Errorf("this is a error %s log!", "format")
}
命令行下的文本帶顏色效果
自定義格式化輸出
你想要自定義日志輸出格式 ?
Logger Message
| 字段 | 別名 | 類型 | 說明 | 例子 |
|---|---|---|---|---|
| Timestamp | timestamp | int64 | Unix時間戳 | 1521791201 |
| TimestampFormat | timestamp_format | string | 時間戳格式化字符串 | 2018-3-23 15:46:41 |
| Millisecond | millisecond | int64 | 毫秒時間戳 | 1524472688352 |
| MillisecondFormat | millisecond_format | string | 毫秒時間戳格式化字符串 | 2018-3-23 15:46:41.970 |
| Level | level | int | 日志級別 | 1 |
| LevelString | level_string | string | 日志級別字符串 | Error |
| Body | body | string | 日志內容 | this is a info log |
| File | file | string | 調用本次日志輸出的文件名 | main.go |
| Line | line | int | 調用本次日志輸出的方法 | 64 |
| Function | function | string | 調用本次日志輸出的方法名 | main.main |
配置 Format 參數:
consoleConfig := &go_logger.ConsoleConfig{
Format: "%millisecond_format% [%level_string%] %body%"
}
fileConfig := &go_logger.FileConfig{
Format: "%millisecond_format% [%level_string%] %body%"
}
輸出結果:
2018-03-23 14:55:07.003 [Critical] this is a critical log!
你只需要配置參數 Format: "% Logger Message 別名%" 來自定義輸出字符串格式
更多的 adapter 例子
性能測試結果
參考
beego/logs : github.com/astaxie/beego/logs
反饋
歡迎提交意見和代碼,聯系信息 [email protected]
License
MIT
謝謝
Create By [email protected]
評論
圖片
表情
