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

程序名称:Go Logger

授权协议: MIT

操作系统: 跨平台

开发语言: Google Go

Go Logger 介绍

go-logger

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

godoc


license

Build Status


Coverage

English document

功能

安装使用

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

    }

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

自定义格式化输出

你想要自定义日志输出格式 ?

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]

Go Logger 官网

https://github.com/phachon/go-logger

相关编程语言

BlazeDS 是一个基于服务器的Java 远程控制(remoting...
OVal 是一个可扩展的Java对象数据验证框架,验证的规...
Volta 是一套开发工具,专为开发分布式、实时系统应...
OpenDDS 是一个开源的 C++ 实现的 对象管理组织 OMG...
JADE (Java Agent DEvelopment Framework) 是一个完...
FastMM ,在D2006和2007中已代替了原来的内存管理器。