gobuch/microservices/logging/main.go

34 lines
674 B
Go
Raw Permalink Normal View History

2020-08-21 04:26:40 +00:00
package main
import (
2020-10-21 04:05:47 +00:00
"time"
2020-08-21 04:26:40 +00:00
log "github.com/sirupsen/logrus"
)
2020-10-21 04:05:47 +00:00
type Testing struct {
Val1 bool
Val2 string
Val3 time.Time
}
2020-08-21 04:26:40 +00:00
func main() {
2020-10-21 04:05:47 +00:00
log.SetFormatter(&log.JSONFormatter{})
2020-08-21 04:26:40 +00:00
// zusätzliche Felder an log.Entry anhängen
log.WithFields(log.Fields{
"importId": "ka18s",
"size": 10,
}).Info("Starte den Import")
// Wiederverwenden von log.Entry damit alle weiteren
// Aufrufe ebenfalls Felder bekommen
contextLogger := log.WithFields(log.Fields{
"importId": "0815s",
"other": "Ich werde geloggt",
2020-10-21 04:05:47 +00:00
"test": Testing{Val3: time.Now()},
2020-08-21 04:26:40 +00:00
})
contextLogger.Info("I'll be logged with common and other field")
contextLogger.Info("Me too")
}