golang入门笔记——logrus

logrus的简单使用

下载依赖

go get github.com/sirupsen/logrus
package main

import log "github.com/sirupsen/logrus"

func main() {
	log.WithFields(log.Fields{
		"animal": "walrus",
	}).Info("A walrus appears")
}

package main

import (
	"os"

	log "github.com/sirupsen/logrus"
)

func init() {
	//以JSON字符串的形式而不是字符串的形式来记录日志
	log.SetFormatter(&log.JSONFormatter{})

	//将日志默认输出到命令行
	log.SetOutput(os.Stdout)

	//日志输出基本是Warn
	log.SetLevel(log.DebugLevel)
}

func main() {
	log.WithFields(log.Fields{ //以字段的方式添加一些信息,实际就是以map形式将字段给存储起来
		"animal": "walrus",
		"size":   10,
	}).Info("A group of walrus emerges from the ocean")

	log.WithFields(log.Fields{
		"omg":    true,
		"number": 122,
	}).Warn("The group's number increased tremendously!")

	log.WithFields(log.Fields{
		"omg":    true,
		"number": 100,
	}).Fatal("The ice breaks!") //fatal日志会把应用程序中断

	//一个可以被其它log使用的公共的模式,返回一个log对象
	contextLogger := log.WithFields(log.Fields{
		"common": "this is a common field",
		"other":  "I also should be logged always",
	})

	contextLogger = contextLogger.WithFields(
		log.Fields{
			"field1": "value1",
			"field2": "value2",
		},
	) //追加字段
	contextLogger.Info("I'll be logged with common and other field")
	contextLogger.Warn("Me too")
}

钩子函数

type Hook interface {
	Levels() []Level
	Fire(*Entry) error
}
package main

import (
	"os"

	log "github.com/sirupsen/logrus"
)

var Log *log.Entry

const LogPath = "./log/test.log"

type MyHook struct {
}

func (m *MyHook) Levels() []log.Level { //hook挂在哪些level的日志上
	//TODO implement me
	return log.AllLevels
}

func (m *MyHook) Fire(entry *log.Entry) error {
	file, err := os.OpenFile(LogPath, os.O_CREATE|os.O_APPEND|os.O_WRONLY, 0666)
	if err != nil {
		log.Error(err)
	} else {
		log.SetOutput(file)
	}
	return nil
} //可以改变Entry里面的值,设置一些公共参数

func init() {
	log.SetLevel(log.DebugLevel)
	log.SetOutput(os.Stdout)
	log.SetReportCaller(true) //输出打印日志的代码及其行号
	log.AddHook(&MyHook{})
	Log = log.WithFields(log.Fields{
		"version": "1.0.0",
	})
}

func main() {
	Log.Info("test")
}

log文件切割

go get github.com/lestrrat-go/file-rotatelogs
package main

import (
	"io"
	"os"
	"time"

	rotatelogs "github.com/lestrrat-go/file-rotatelogs"
	log "github.com/sirupsen/logrus"
)

var Log *log.Entry

const LogPath = "./log/test.log"

type MyHook struct {
}

func (m *MyHook) Levels() []log.Level { //hook挂在哪些level的日志上
	//TODO implement me
	return log.AllLevels
}

func (m *MyHook) Fire(entry *log.Entry) error {
	file, err := getWriter()
	if err != nil {
		log.Error(err)
	} else {
		log.SetOutput(file)
	}
	return nil
} //可以改变Entry里面的值,设置一些公共参数

func init() {
	log.SetLevel(log.DebugLevel)
	log.SetOutput(os.Stdout)
	log.SetReportCaller(true) //输出打印日志的代码及其行号
	log.AddHook(&MyHook{})
	Log = log.WithFields(log.Fields{
		"version": "1.0.0",
	})
}

func getWriter() (io.Writer, error) {
	return rotatelogs.New(LogPath+".%Y%m%d%H%M", //指定文件名格式
		//rotatelogs.WithLinkName(LogPath),        //将最新文件软链接到指定path,windows环境不支持
		rotatelogs.WithMaxAge(time.Second*1800),     //日志最长保存时长
		rotatelogs.WithRotationTime(time.Second*60)) //日志分割时间间隔

}

func main() {
	Log.Info("test")
}

你可能感兴趣的:(go语言,golang,java,开发语言)