taskcafe/internal/logger/logger.go

22 lines
554 B
Go
Raw Normal View History

package logger
2020-04-10 04:40:22 +02:00
import (
2020-10-14 23:52:32 +02:00
"context"
2020-04-10 04:40:22 +02:00
2020-10-14 23:52:32 +02:00
"github.com/google/uuid"
"github.com/jordanknott/taskcafe/internal/utils"
log "github.com/sirupsen/logrus"
2020-04-10 04:40:22 +02:00
)
2020-10-14 23:52:32 +02:00
// New returns a log entry with the reqID and userID fields populated if they exist
func New(ctx context.Context) *log.Entry {
entry := log.NewEntry(log.StandardLogger())
if reqID, ok := ctx.Value(utils.ReqIDKey).(uuid.UUID); ok {
entry = entry.WithField("reqID", reqID)
2020-04-10 04:40:22 +02:00
}
2020-10-14 23:52:32 +02:00
if userID, ok := ctx.Value(utils.UserIDKey).(uuid.UUID); ok {
entry = entry.WithField("userID", userID)
2020-04-10 04:40:22 +02:00
}
return entry
}