logrus 安装 使用

 

安装

logrus
go get github.com/sirupsen/logrus

无法访问 golang 所以就 先 clone github.com/golang的源码 然后生成
cd $GOPATH/src/golang.org/x
git clone https://github.com/golang/crypto.git
go get -u golang.org/x/crypto/ssh/terminal


git clone https://github.com/golang/sys.git

go get -u golang.org/x/sys/unix

 

使用

package main

import (
	log "github.com/sirupsen/logrus"
	"time"
)

type Animal struct {
	Name string
	age int
}

func main() {
	//log.SetFormatter(&log.JSONFormatter{})
	a := Animal{"dog", 22}
	log.SetFormatter(&log.TextFormatter{
		FullTimestamp:true})
	log.WithFields(log.Fields{
		"event": "ne",
		"topic": "title",
		"key": "my key",
	}).Info("hello", a)
	
	log.Error("hello world")
	for {
		time.Sleep(time.Second)
		log.Printf("i am ok %s", "dock")
	}
	log.Fatal("kill ")
}

 

写在一个文件里

func SetLogFile() {
	file := time.Now().Format("20060102") + ".txt"
	logFile, err := os.OpenFile(file, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0766)
	if nil != err {
		panic(err)
	}
	log.SetOutput(logFile)
}

 

自动拆分日志文件 

先安装拆分日志文件的2份源码

go get github.com/lestrrat-go/file-rotatelogs
go get github.com/rifflock/lfshook

使用示例 

package main

import (
	log "github.com/sirupsen/logrus"
	"github.com/lestrrat-go/file-rotatelogs"
    "github.com/rifflock/lfshook"
    "time"
    "os"
    "github.com/pkg/errors"
    "path"
)

func main() {
    // 24小时一个日志文件,最多存365天的日志文件。 再多了就删掉
	ConfigLocalFilesystemLogger("log", "lg", time.Hour*24*365, time.Hour*24)
	
	for {
		time.Sleep(time.Second*3)
		log.Info("hello ")
	}
}

// config logrus log to local filesystem, with file rotation
func ConfigLocalFilesystemLogger(logPath string, logFileName string, maxAge time.Duration, rotationTime time.Duration) {
    baseLogPaht := path.Join(logPath, logFileName)
    writer, err := rotatelogs.New(
        baseLogPaht+".%Y%m%d%H%M",
        rotatelogs.WithLinkName(baseLogPaht), // 生成软链,指向最新日志文件

        rotatelogs.WithMaxAge(maxAge),        // 文件最大保存时间
        // rotatelogs.WithRotationCount(365),  // 最多存365个文件

        rotatelogs.WithRotationTime(rotationTime), // 日志切割时间间隔
    )
    if err != nil {
        log.Errorf("config local file system logger error. %+v", errors.WithStack(err))
    }
    lfHook := lfshook.NewHook(lfshook.WriterMap{
        log.DebugLevel: writer, // 为不同级别设置不同的输出目的
        log.InfoLevel:  writer,
        log.WarnLevel:  writer,
        log.ErrorLevel: writer,
        log.FatalLevel: writer,
        log.PanicLevel: writer,
    }, &log.TextFormatter{})
    log.AddHook(lfHook)
}

 WithRotationTime 设置为1分钟时,生成的日志文件如下

~/gol/log $ ls
lg               lg.201812041357  lg.201812041410  lg.201812041423
lg.201812041344  lg.201812041358  lg.201812041411  lg.201812041424
lg.201812041346  lg.201812041359  lg.201812041412  lg.201812041425
lg.201812041347  lg.201812041400  lg.201812041413  lg.201812041426
lg.201812041348  lg.201812041401  lg.201812041414  lg.201812041427
lg.201812041349  lg.201812041402  lg.201812041415  lg.201812041428
lg.201812041350  lg.201812041403  lg.201812041416  lg.201812041429
lg.201812041351  lg.201812041404  lg.201812041417  lg.201812041430
lg.201812041352  lg.201812041405  lg.201812041418  lg.201812041431
lg.201812041353  lg.201812041406  lg.201812041419  lg.201812041432
lg.201812041354  lg.201812041407  lg.201812041420  lg.201812041433
lg.201812041355  lg.201812041408  lg.201812041421  lg.201812041434
lg.201812041356  lg.201812041409  lg.201812041422  lg.201812041435

 

阅读更多

没有更多推荐了,返回首页