Skip to content

Latest commit

 

History

History
208 lines (164 loc) · 7.04 KB

README_CN.md

File metadata and controls

208 lines (164 loc) · 7.04 KB

gologger

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

godoc license

English document

功能

  • 支持同时输出到 console, file, url
  • 命令行输出字体可带颜色
  • 文件输出支持根据 文件大小,文件行数,日期三种方式切分
  • 文件输出支持根据日志级别分别保存到不同的文件
  • 支持异步和同步两种方式写入
  • 支持 json 格式化输出
  • 代码设计易扩展,可根据需要设计自己的 adapter

安装使用

go get github.com/lorock/gologger
go get ./...

环境需要

go 1.8

支持输出

  • console // 输出到命令行
  • file // 文件
  • api // http url 接口
  • ...

快速使用

  • 同步方式
import (
    "github.com/lorock/gologger"
)
func main()  {
    logger := gologger.NewLogger()

    logger.Info("this is a info log!")
    logger.Errorf("this is a error %s log!", "format")
}
  • 异步方式
import (
    "github.com/lorock/gologger"
)
func main()  {
    logger := gologger.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/lorock/gologger"
)
func main()  {
    logger := gologger.NewLogger()

    logger.Detach("console")

    // 命令行输出配置
    consoleConfig := &gologger.ConsoleConfig{
        Color: true, // 命令行输出字符串是否显示颜色
        JsonFormat: true, // 命令行输出字符串是否格式化
        Format: "", // 如果输出的不是 json 字符串,JsonFormat: false, 自定义输出的格式
    }
    // 添加 console 为 logger 的一个输出
    logger.Attach("console", gologger.LOGGER_LEVEL_DEBUG, consoleConfig)

    // 文件输出配置
    fileConfig := &gologger.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", gologger.LOGGER_LEVEL_DEBUG, fileConfig)


    logger.Info("this is a info log!")
    logger.Errorf("this is a error %s log!", "format")
}

命令行下的文本带颜色效果

image

自定义格式化输出

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 := &gologger.ConsoleConfig{
    Format: "%millisecond_format% [%level_string%] %body%",
}
fileConfig := &gologger.FileConfig{
    Format: "%millisecond_format% [%level_string%] %body%",
}

输出结果:

2018-03-23 14:55:07.003 [Critical] this is a critical log!

你只需要配置参数 Format: "% Logger Message 别名%" 来自定义输出字符串格式

更多的 adapter 例子

性能测试结果

system: Linux Mint 18.2 Sonya
cpu(s): 4
model name: Intel(R) Core(TM) i5-3210M CPU @ 2.50GHz
memery: 4G

BenchmarkLoggerConsoleText          500000             11375 ns/op             672 B/op         15 allocs/op
BenchmarkLoggerConsoleText-2        500000             11345 ns/op             672 B/op         15 allocs/op
BenchmarkLoggerConsoleText-4        500000              9897 ns/op             672 B/op         15 allocs/op
BenchmarkLoggerConsoleAsyncText     500000              9323 ns/op             672 B/op         15 allocs/op
BenchmarkLoggerConsoleAsyncText-2   500000              9087 ns/op             672 B/op         15 allocs/op
BenchmarkLoggerConsoleAsyncText-4   500000             10685 ns/op             672 B/op         15 allocs/op
BenchmarkLoggerConsoleJson          200000             30918 ns/op            2048 B/op         10 allocs/op
BenchmarkLoggerConsoleJson-2        200000             33153 ns/op            2048 B/op         10 allocs/op
BenchmarkLoggerConsoleJson-4        200000             30918 ns/op            2048 B/op         10 allocs/op
BenchmarkLoggerFileText             300000             14083 ns/op             912 B/op         21 allocs/op
BenchmarkLoggerFileText-2           200000             21159 ns/op             912 B/op         21 allocs/op
BenchmarkLoggerFileText-4           200000             23776 ns/op             912 B/op         21 allocs/op
BenchmarkLoggerFileAsyncText        300000             13956 ns/op             912 B/op         21 allocs/op
BenchmarkLoggerFileAsyncText-2      300000             16124 ns/op             912 B/op         21 allocs/op
BenchmarkLoggerFileAsyncText-4      300000             18641 ns/op             912 B/op         21 allocs/op
BenchmarkLoggerFileJson             200000             15472 ns/op            1968 B/op         15 allocs/op
BenchmarkLoggerFileJson-2           200000             22523 ns/op            1968 B/op         15 allocs/op
BenchmarkLoggerFileJson-4           200000             25596 ns/op            1968 B/op         15 allocs/op

参考

beego/logs : github.com/astaxie/beego/logs

反馈

欢迎提交意见和代码

License

MIT

谢谢