9 Star 36 Fork 13

phachon / go-logger

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
贡献代码
同步代码
取消
提示: 由于 Git 不支持空文件夾,创建文件夹后会生成空的 .keep 文件
Loading...
README
MIT

go-logger

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

Sourcegraph godoc license

English document

功能

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

安装使用

go get github.com/phachon/go-logger
go get ./...

环境需要

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")
}

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

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 := &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 例子

性能测试结果

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

反馈

欢迎提交意见和代码,联系信息 phachon@163.com

License

MIT

谢谢

Create By phachon@163.com

MIT License Copyright (c) 2018 phachon Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

简介

一个简单的 golang 日志工具包,支持同步和异步输出到 命令行,文件, api 接口,文件支持按文件大小,文件行数,日期切分 展开 收起
Go
MIT
取消

发行版

暂无发行版

贡献者

全部

近期动态

加载更多
不能加载更多了
Go
1
https://gitee.com/phachon/go-logger.git
git@gitee.com:phachon/go-logger.git
phachon
go-logger
go-logger
master

搜索帮助