matterbridge/vendor/github.com/x-cray/logrus-prefixed-formatter/examples/basic.go

60 lines
1.3 KiB
Go
Raw Normal View History

2016-04-10 14:39:38 -07:00
package main
import (
2018-02-20 14:41:09 -08:00
"github.com/sirupsen/logrus"
prefixed "github.com/x-cray/logrus-prefixed-formatter"
2016-04-10 14:39:38 -07:00
)
var log = logrus.New()
func init() {
2018-02-20 14:41:09 -08:00
formatter := new(prefixed.TextFormatter)
log.Formatter = formatter
2016-04-10 14:39:38 -07:00
log.Level = logrus.DebugLevel
}
func main() {
defer func() {
err := recover()
if err != nil {
2018-02-20 14:41:09 -08:00
// Fatal message
2016-04-10 14:39:38 -07:00
log.WithFields(logrus.Fields{
"omg": true,
"number": 100,
2018-02-20 14:41:09 -08:00
}).Fatal("[main] The ice breaks!")
2016-04-10 14:39:38 -07:00
}
}()
2018-02-20 14:41:09 -08:00
// You could either provide a map key called `prefix` to add prefix
2016-04-10 14:39:38 -07:00
log.WithFields(logrus.Fields{
2018-02-20 14:41:09 -08:00
"prefix": "main",
2016-04-10 14:39:38 -07:00
"animal": "walrus",
"number": 8,
}).Debug("Started observing beach")
2018-02-20 14:41:09 -08:00
// Or you can simply add prefix in square brackets within message itself
2016-04-10 14:39:38 -07:00
log.WithFields(logrus.Fields{
"animal": "walrus",
"size": 10,
2018-02-20 14:41:09 -08:00
}).Debug("[main] A group of walrus emerges from the ocean")
2016-04-10 14:39:38 -07:00
2018-02-20 14:41:09 -08:00
// Warning message
2016-04-10 14:39:38 -07:00
log.WithFields(logrus.Fields{
"omg": true,
"number": 122,
2018-02-20 14:41:09 -08:00
}).Warn("[main] The group's number increased tremendously!")
2016-04-10 14:39:38 -07:00
2018-02-20 14:41:09 -08:00
// Information message
2016-04-10 14:39:38 -07:00
log.WithFields(logrus.Fields{
2018-02-20 14:41:09 -08:00
"prefix": "sensor",
2016-04-10 14:39:38 -07:00
"temperature": -4,
2018-02-20 14:41:09 -08:00
}).Info("Temperature changes")
2016-04-10 14:39:38 -07:00
2018-02-20 14:41:09 -08:00
// Panic message
2016-04-10 14:39:38 -07:00
log.WithFields(logrus.Fields{
2018-02-20 14:41:09 -08:00
"prefix": "sensor",
2016-04-10 14:39:38 -07:00
"animal": "orca",
"size": 9009,
}).Panic("It's over 9000!")
}