初始化logrus,简单配置
- package logger
-
- import (
- "github.com/sirupsen/logrus"
- "os"
- )
-
- func Init() {
- var log = logrus.New()
- log.Formatter = new(logrus.TextFormatter)
- log.Formatter.(*logrus.TextFormatter).DisableTimestamp = false // remove timestamp from test output
- log.Formatter.(*logrus.TextFormatter).FullTimestamp = true
- log.Formatter.(*logrus.TextFormatter).TimestampFormat = "2006-01-02 15:04:05" //default
- log.Formatter.(*logrus.TextFormatter).DisableColors = false // remove colors
- logrus.SetFormatter(log.Formatter)
-
- logrus.SetLevel(logrus.TraceLevel)
- logrus.SetOutput(os.Stdout)
- }
调用logrus
- package main
-
- import (
- "github.com/sirupsen/logrus"
- "study/logger/logger"
- )
-
- var log = logrus.WithField("prefix", "main")
-
- func main() {
- logger.Init()
- log.WithFields(logrus.Fields{
- "animal": "walrus",
- "number": 0,
- }).Trace("Went to the beach")
-
- log.WithFields(logrus.Fields{
- "animal": "walrus",
- "number": 8,
- }).Debug("Started observing beach")
-
- log.WithFields(logrus.Fields{
- "animal": "walrus",
- "size": 10,
- }).Info("A group of walrus emerges from the ocean")
-
- log.WithFields(logrus.Fields{
- "omg": true,
- "number": 122,
- }).Warn("The group's number increased tremendously!")
-
- log.WithFields(logrus.Fields{
- "temperature": -4,
- }).Debug("Temperature changes")
-
- log.WithFields(logrus.Fields{
- "animal": "orca",
- "size": 9009,
- }).Panic("It's over 9000!")
- }