golang log模块之log4go文档

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/Webben/article/details/80541456

介绍

这个存储库是从alecthomas的log4go重构而来的,log4go是一个日志包,类似于Go编程语言的log4j。
支持两个新特性,一个是Json配置样式,另一个是根据类别的不同输出。

特点

  • 记录到控制台
  • 日志文件,支持按大小或时间旋转。
  • 登录到网络,支持tcp和udp
  • 支持xml配置
  • 支持Json样式配置
  • 为日志添加类别
    • 根据不同的输出和不同的用法对日志进行分类。
  • 兼容旧的

安装使用

首先,下载源码:

go get github.com/jeanphorn/log4go

导入进工程:

import log "github.com/jeanphorn/log4go"

源代码也可以直接从github仓库下载使用。

使用示例

这里使用json配置文件,配置文件是可选的,如果不配置,默认输出到终端。

{
    "console": {
        "enable": true,     // wether output the log
        "level": "FINE"     // log level: FINE, DEBUG, TRACE, INFO, WARNING,ERROR, CRITICAL
    },  
    "files": [{
        "enable": true,
        "level": "DEBUG",
        "filename":"./test.log",
        "category": "Test",         // different category log to different files
        "pattern": "[%D %T] [%C] [%L] (%S) %M"      // log output formmat
    },{ 
        "enable": false,
        "level": "DEBUG",
        "filename":"rotate_test.log",
        "category": "TestRotate",
        "pattern": "[%D %T] [%C] [%L] (%S) %M",
        "rotate": true,                             // wether rotate the log
        "maxsize": "500M",
        "maxlines": "10K",
        "daily": true
    }], 
    "sockets": [{
        "enable": false,
        "level": "DEBUG",
        "category": "TestSocket",
        "pattern": "[%D %T] [%C] [%L] (%S) %M",
        "addr": "127.0.0.1:12124",
        "protocol":"udp"
    }]  
}

Code

package main

import (
    log "github.com/jeanphorn/log4go"
)

func main() {
    // load config file, it's optional
    // or log.LoadConfiguration("./example.json", "json")
    // config file could be json or xml
    log.LoadConfiguration("./example.json")

    log.LOGGER("Test").Info("category Test info test ...")
    log.LOGGER("Test").Info("category Test info test message: %s", "new test msg")
    log.LOGGER("Test").Debug("category Test debug test ...")

    // Other category not exist, test
    log.LOGGER("Other").Debug("category Other debug test ...")

    // socket log test
    log.LOGGER("TestSocket").Debug("category TestSocket debug test ...")

    // original log4go test
    log.Info("nomal info test ...")
    log.Debug("nomal debug test ...")

    log.Close()
}

输出样式

[2017/11/15 14:35:11 CST] [Test] [INFO] (main.main:15) category Test info test ...
[2017/11/15 14:35:11 CST] [Test] [INFO] (main.main:16) category Test info test message: new test msg
[2017/11/15 14:35:11 CST] [Test] [DEBG] (main.main:17) category Test debug test ...
[2017/11/15 14:35:11 CST] [DEFAULT] [INFO] (main.main:26) nomal info test ...
[2017/11/15 14:35:11 CST] [DEFAULT] [DEBG] (main.main:27) nomal debug test ...

文档

日志等级

func (log Logger) Log(lvl Level, source, message string) {
func (log Logger) Finest(arg0 interface{}, args ...interface{}) {
func (log Logger) Fine(arg0 interface{}, args ...interface{}) {
func (log Logger) Debug(arg0 interface{}, args ...interface{}) {
func (log Logger) Trace(arg0 interface{}, args ...interface{}) {
func (log Logger) Info(arg0 interface{}, args ...interface{}) {
func (log Logger) Error(arg0 interface{}, args ...interface{}) error {
func (log Logger) Critical(arg0 interface{}, args ...interface{}) error {

猜你喜欢

转载自blog.csdn.net/Webben/article/details/80541456