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

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值