github 官网
https://github.com/sirupsen/logrus
代码
package main
/**
依赖网址
https://github.com/Sirupsen/logrus
https://github.com/onrik/logrus
https://github.com/keepeye/logrus-filename
高级实用
https://juejin.im/entry/5a5830c4f265da3e2b16586c
*/
import (
"github.com/onrik/logrus/filename"
"os"
"github.com/onrik/logrus/sentry"
log "github.com/sirupsen/logrus"
)
var logInstance3 = log.New()
func init() {
logInstance3.SetLevel(log.ErrorLevel)
//aa := log.JSONFormatter{TimestampFormat: "2006-01-02 15:04:05"} // 设置时间输出格式
aa := log.TextFormatter{TimestampFormat: "2006-01-02 15:04:05"}
logInstance3.SetFormatter(&aa)
}
func LoginNew3() *os.File {
filenameHook := filename.NewHook()
filenameHook.Field = "line" // Customize source field name
logInstance3.AddHook(filenameHook)
sentryHook := sentry.NewHook("", log.PanicLevel, log.FatalLevel, log.ErrorLevel)
log.AddHook(sentryHook)
logInstance3.Out = os.Stdout
file, err := os.OpenFile("logs/admin.log", os.O_CREATE|os.O_WRONLY, 0666)
if err == nil {
logInstance3.Out = file
} else {
logInstance3.Info("Failed to log to file, using default stderr")
}
return file
}
func main() {
file := LoginNew3()
defer file.Close()
//logEn.Error("我拍的天啊")
logInstance3.Info("hahha")
logInstance3.Error("koi")
logInstance3.Fatal("fatal")
}
注意
答应出 行号 时间也进行格式化