Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 49 additions & 0 deletions lfshookLogrus_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/**
* @Author : henry
* @Data: 2020-08-31 15:20
* @Note: a example about rotatelogs logrus rotatelogs
**/

package lfshook

import (
"fmt"
rotatelogs "github.com/lestrrat-go/file-rotatelogs"
"github.com/sirupsen/logrus"
"runtime"
"time"
)

var Log *logrus.Logger

func NewLogger() *logrus.Logger {
if Log != nil {
return Log
}

path := "/var/log/go.log"
writer, _ := rotatelogs.New(
path+".%Y%m%d%H%M",
rotatelogs.WithLinkName(path),
rotatelogs.WithMaxAge(time.Duration(86400)*time.Second),
rotatelogs.WithRotationTime(time.Duration(604800)*time.Second),
)

pathMap := logrus.Hook(lfshook.NewHook(
lfshook.WriterMap{
logrus.InfoLevel: writer,
logrus.ErrorLevel: writer,
},
nil,
))

Log = logrus.New()
Log.Hooks.Add(pathMap)
Logger.SetFormatter(&logrus.JSONFormatter{
CallerPrettyfier: func(f *runtime.Frame) (string, string) {
return fmt.Sprintf("%s()", f.Function), fmt.Sprintf("%s:%d", f.File, f.Line)
},
})

return Log
}