Go Logger 介绍
go-logger
功能
- 支持同时输出到 console, file, url
- 命令行输出字体可带颜色
- 文件输出支持根据 文件大小,文件行数,日期三种方式切分
- 文件输出支持根据日志级别分别保存到不同的文件
- 支持异步和同步两种方式写入
- 支持 json 格式化输出
- 代码设计易扩展,可根据需要设计自己的 adapter
安装使用
go get github.com/phachon/go-logger go get ./...
环境需要
go 1.8
支持输出
快速使用
-
同步方式
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 @H_404_127@ | timestamp @H_404_127@ | int64 @H_404_127@ | Unix时间戳 @H_404_127@ | 1521791201 @H_404_127@ |
Timestampformat @H_404_127@ | timestamp_format @H_404_127@ | string @H_404_127@ | 时间戳格式化字符串 @H_404_127@ | 2018-3-23 15:46:41 @H_404_127@ |
Millisecond @H_404_127@ | millisecond @H_404_127@ | int64 @H_404_127@ | 毫秒时间戳 @H_404_127@ | 1524472688352 @H_404_127@ |
MillisecondFormat @H_404_127@ | millisecond_format @H_404_127@ | string @H_404_127@ | 毫秒时间戳格式化字符串 @H_404_127@ | 2018-3-23 15:46:41.970 @H_404_127@ |
Level @H_404_127@ | level @H_404_127@ | int @H_404_127@ | 日志级别 @H_404_127@ | 1 @H_404_127@ |
LevelString @H_404_127@ | level_string @H_404_127@ | string @H_404_127@ | 日志级别字符串 @H_404_127@ | Error @H_404_127@ |
Body @H_404_127@ | body @H_404_127@ | string @H_404_127@ | 日志内容 @H_404_127@ | this is a info log @H_404_127@ |
File @H_404_127@ | file @H_404_127@ | string @H_404_127@ | 调用本次日志输出的文件名 @H_404_127@ | main.go @H_404_127@ |
Line @H_404_127@ | line @H_404_127@ | int @H_404_127@ | 调用本次日志输出的方法 @H_404_127@ | 64 @H_404_127@ |
Function @H_404_127@ | function @H_404_127@ | string @H_404_127@ | 调用本次日志输出的方法名 @H_404_127@ | main.main @H_404_127@ |
配置 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!
更多的 adapter 例子
性能测试结果
参考
beego/logs : github.com/astaxie/beego/logs
反馈
欢迎提交意见和代码,联系信息 [email protected]
License
MIT
谢谢
Create By [email protected]
Go Logger 官网
https://github.com/phachon/go-logger
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。