基础1
package main
import (
log "github.com/sirupsen/logrus"
)
func main() {
log.WithFields(log.Fields{
"animal": "dog",
}).Info("一条舔狗出现了。")
log.SetLevel(log.TraceLevel) //输出级别
//log.SetReportCaller(true)//行号
//log.SetFormatter(&log.JSONFormatter{})
log.SetFormatter(&log.TextFormatter{})//输出格式 默认
log.Trace("Trace")
log.Debug("Debug")
log.Info("Info")
log.Warn("Warn")
log.Error("Error")
cnt := 100
rst := true
log.WithFields(log.Fields{
"event": "bledisconnect",
"numbers": cnt,
"key": rst,
}).Fatal("Failed to bledisconnect")
// 记完日志后会调用os.Exit(1)
log.Fatal("Fatal")
// 记完日志后会调用 panic()
log.Panic("Panic")
}
2-
文件
package main
import (
"os"
"time"
"github.com/sirupsen/logrus"
)
var log = logrus.New()
func main() {
file, err := os.OpenFile("log-"+time.Now().Format("2006-01-02")+".log", os.O_CREATE|os.O_WRONLY, 0666)
if err == nil {
log.Out = file
} else {
log.Info("Failed to log to file")
}
log.WithFields(logrus.Fields{
"filename": "123.txt",
}).Info("打开文件失败")
}
https://www.cnblogs.com/rxbook/p/15161553.html
颜色
package main
import (
log "github.com/sirupsen/logrus"
)
func main() {
log.WithFields(log.Fields{
"animal": "dog",
}).Info("一条舔狗出现了。")
log.SetLevel(log.TraceLevel) //输出级别
//log.SetReportCaller(true)//行号
//log.SetFormatter(&log.JSONFormatter{})
//log.SetFormatter(&log.TextFormatter{})//输出格式 默认
customFormatter := new(log.TextFormatter)
customFormatter.FullTimestamp = true // 显示完整时间
customFormatter.TimestampFormat = "2006-01-02 15:04:05" // 时间格式
customFormatter.DisableTimestamp = false // 禁止显示时间
customFormatter.DisableColors = false // 禁止颜色显示
log.SetFormatter(customFormatter)
log.Trace("Trace")
log.Debug("Debug")
log.Info("Info")
log.Warn("Warn")
log.Error("Error")
cnt := 100
rst := true
log.WithFields(log.Fields{
"event": "bledisconnect",
"numbers": cnt,
"key": rst,
}).Fatal("Failed to bledisconnect")
// 记完日志后会调用os.Exit(1)
log.Fatal("Fatal")
// 记完日志后会调用 panic()
log.Panic("Panic")
}