golang日志库logger

golang日志库logger

  • 日志库
    • 配置文件
  • 配置文件logs.config
  • 代码
  • 控制台输出
  • 文件日志输出

日志库

github上面go语言其实已经有很多可用的日志库了,比如sirupsen/logrus,uber-go/zap,golang/glog等等,但是我总觉得没有一个适合自己用,基于以下基本要求,我决定自己写一个轮子。

1. 日志通过文件配置。
2. 支持日志级别输出。
3. 支持同时输出控制台和文件。
4. 支持文件按数量和大小自动切片。
5. 打印文件、行号、日志级别

日志组件基于https://github.com/jander/golog进行改造,改造后的组件为https://github.com/kuxuee/logger

配置文件

logger固定读取程序目录下的logs.config文件,该文件为JSON格式数据。

配置文件logs.config

  • name:单个logger配置项名字,由函数logger.NewLogger传入该名字作为参数来读取配置
  • data:同一日志配置多个输出端
    • handle:输出端console-控制台 file-普通文件 rotating-切片文件
    • dir:切片文件目录
    • filename:切片文件名,无须后缀名
    • levle:日志级别0-debug 1-info 2-warn 3-error 4-panic 5-fatal
    • maxnum:最大支持文件数,达到设置值后向前覆盖文件,0为无限个
    • maxsize:单个文件大小,达到大小后切片写新日志
{
	"logs" : [{
	"name":"default", 
	"data":[
		{"handle":"console", "level":3},
		{"handle":"rotating", "dir":"./log", "filename":"default", "level":0, "maxnum":0, "maxsize":"1MB"}
		]
	}]
}

代码

package main

import (
	"log"
	"time"

	"github.com/kuxuee/logger"
)

func main() {
	err := logger.NewLogger("default")
	if err != nil {
		log.Fatal(err)
	}

	defer logger.Close()

	for i := 0; i < 10; i++ {
		logger.Debug("something1", "debug")
		logger.Info("something:", i)
		logger.Warn("something")
		logger.Error("something")
		logger.Infof("This is info:%s-%d", "go", 11)
		time.Sleep(1 * time.Second)
		if 5 == i {
			logger.Fatal("fatal")
		}
	}
}

控制台输出

2019/02/27 10:20:53.964434 main.go:22: error [something]
2019/02/27 10:20:54.964491 main.go:22: error [something]
2019/02/27 10:20:55.964548 main.go:22: error [something]
2019/02/27 10:20:56.964605 main.go:22: error [something]
2019/02/27 10:20:57.964663 main.go:22: error [something]
2019/02/27 10:20:58.964720 main.go:22: error [something]
2019/02/27 10:20:59.964777 main.go:26: fatal [fatal]

文件日志输出

2019/02/27 10:20:53.963434 main.go:19: debug [something1 debug]
2019/02/27 10:20:53.964434 main.go:20: info [something: 0]
2019/02/27 10:20:53.964434 main.go:21: warn [something]
2019/02/27 10:20:53.964434 main.go:22: error [something]
2019/02/27 10:20:53.964434 main.go:23: info [This is info:go-11]
2019/02/27 10:20:54.964491 main.go:19: debug [something1 debug]
2019/02/27 10:20:54.964491 main.go:20: info [something: 1]
2019/02/27 10:20:54.964491 main.go:21: warn [something]
2019/02/27 10:20:54.964491 main.go:22: error [something]
2019/02/27 10:20:54.964491 main.go:23: info [This is info:go-11]
2019/02/27 10:20:55.964548 main.go:19: debug [something1 debug]
2019/02/27 10:20:55.964548 main.go:20: info [something: 2]
2019/02/27 10:20:55.964548 main.go:21: warn [something]
2019/02/27 10:20:55.964548 main.go:22: error [something]
2019/02/27 10:20:55.964548 main.go:23: info [This is info:go-11]
2019/02/27 10:20:56.964605 main.go:19: debug [something1 debug]
2019/02/27 10:20:56.964605 main.go:20: info [something: 3]
2019/02/27 10:20:56.964605 main.go:21: warn [something]
2019/02/27 10:20:56.964605 main.go:22: error [something]
2019/02/27 10:20:56.964605 main.go:23: info [This is info:go-11]
2019/02/27 10:20:57.964663 main.go:19: debug [something1 debug]
2019/02/27 10:20:57.964663 main.go:20: info [something: 4]
2019/02/27 10:20:57.964663 main.go:21: warn [something]
2019/02/27 10:20:57.964663 main.go:22: error [something]
2019/02/27 10:20:57.964663 main.go:23: info [This is info:go-11]
2019/02/27 10:20:58.964720 main.go:19: debug [something1 debug]
2019/02/27 10:20:58.964720 main.go:20: info [something: 5]
2019/02/27 10:20:58.964720 main.go:21: warn [something]
2019/02/27 10:20:58.964720 main.go:22: error [something]
2019/02/27 10:20:58.964720 main.go:23: info [This is info:go-11]
2019/02/27 10:20:59.964777 main.go:26: fatal [fatal]

你可能感兴趣的:(代码)