go-logger 日志工具包 v1.2 发布

来源: 投稿
作者: phachon
2018-04-23 18:30:00

go-logger

一个简单而强大的 golang 日志工具包

功能

  • 支持同时输出到 console, file, url

  • 命令行输出字体可带颜色

  • 文件输出支持根据 文件大小,文件行数,日期三种方式切分

  • 文件输出支持根据日志级别分别保存到不同的文件

  • 支持异步和同步两种方式写入

  • 支持 json 格式化输出

  • 代码设计易扩展,可根据需要设计自己的 adapter

本次更新

  1. 修复日志根据级别输出到不同文件的一些bug

  2. 修改日志切分后,文件命名由随机字符串改为时间戳后缀的格式

  3. 优化 Logger Config,方便调用

  4. Console 和 File 都支持增加自定义格式化字符串功能 

  5. 更改示例和文档

自定义格式化输出

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 别名%" 来自定义输出字符串格式
展开阅读全文
点击加入讨论🔥(2) 发布并加入讨论🔥
2 评论
18 收藏
分享
返回顶部
顶部