Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

echo_logrus: use std context for fields #1174

Merged
merged 1 commit into from
Jun 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 22 additions & 9 deletions cmd/image-builder/logging.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ type ctxKey int
const (
requestIdCtx ctxKey = iota
insightsRequestIdCtx ctxKey = iota
requestDataCtx ctxKey = iota
)

// Use request id from the standard context and add it to the message as a field.
Expand All @@ -36,6 +37,10 @@ func (h *ctxHook) Fire(e *logrus.Entry) error {
if e.Context != nil {
e.Data["request_id"] = e.Context.Value(requestIdCtx)
e.Data["insights_id"] = e.Context.Value(insightsRequestIdCtx)
rd := e.Context.Value(requestDataCtx).(logrus.Fields)
for k, v := range rd {
e.Data[k] = v
}
}

return nil
Expand All @@ -59,27 +64,35 @@ func requestIdExtractMiddleware(next echo.HandlerFunc) echo.HandlerFunc {
iid = random.String(12)
}

// create fields stored with every log statement
rd := logrus.Fields{
"method": c.Request().Method,
"path": c.Path(),
}
for _, key := range c.ParamNames() {
// protect existing and the most important fields
if _, ok := rd[key]; !(ok || key == "msg" || key == "level") {
rd[key] = c.Param(key)
}
}

// store it in a standard context
ctx := c.Request().Context()
ctx = context.WithValue(ctx, requestIdCtx, rid)
ctx = context.WithValue(ctx, insightsRequestIdCtx, iid)
ctx = context.WithValue(ctx, requestDataCtx, rd)
c.SetRequest(c.Request().WithContext(ctx))

f := logrus.Fields{"method": c.Request().Method, "path": c.Path()}
for _, key := range c.ParamNames() {
f[key] = c.Param(key)
}

// and set echo logger to be context logger
ctxLogger := logrus.StandardLogger()
c.SetLogger(&common.EchoLogrusLogger{
newLogger := &common.EchoLogrusLogger{
Logger: ctxLogger,
Ctx: ctx,
Fields: f,
})
}
c.SetLogger(newLogger)

if !SkipPath(c.Path()) {
c.Logger().Debugf("Started request")
newLogger.Debugf("Started request")
}

return next(c)
Expand Down
3 changes: 2 additions & 1 deletion cmd/image-builder/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,8 @@ func main() {
if values.Error != nil {
fields["error"] = values.Error
}
logrus.WithFields(fields).Infof("Processed request %s %s", values.Method, values.URI)
logrus.WithContext(c.Request().Context()).
WithFields(fields).Infof("Processed request %s %s", values.Method, values.URI)
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here WithContext was missing, somewhat unrelated to the change but I think it is worth fixing.

Copy link
Contributor

@schuellerf schuellerf May 13, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do you think also adding
"path": c.Path(),
to the fields here would be beneficial as this is better for log entry searches/grouping (in contrast to values.URI) I guess
but no stopper for this PR I guess
(hmm or is this added by the c.Request().Context() anyway? - I'll check)


return nil
},
Expand Down
46 changes: 22 additions & 24 deletions internal/common/echo_logrus.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,12 @@ import (
// EchoLogrusLogger extend logrus.Logger
type EchoLogrusLogger struct {
*logrus.Logger
Ctx context.Context
Fields logrus.Fields
Ctx context.Context
}

var commonLogger = &EchoLogrusLogger{
Logger: logrus.StandardLogger(),
Ctx: context.Background(),
Fields: logrus.Fields{},
}

func Logger() *EchoLogrusLogger {
Expand Down Expand Up @@ -68,113 +66,113 @@ func (l *EchoLogrusLogger) SetPrefix(p string) {
}

func (l *EchoLogrusLogger) Print(i ...interface{}) {
l.Logger.WithContext(l.Ctx).WithFields(l.Fields).Print(i...)
l.Logger.WithContext(l.Ctx).Print(i...)
}

func (l *EchoLogrusLogger) Printf(format string, args ...interface{}) {
l.Logger.WithContext(l.Ctx).WithFields(l.Fields).Printf(format, args...)
l.Logger.WithContext(l.Ctx).Printf(format, args...)
}

func (l *EchoLogrusLogger) Printj(j log.JSON) {
b, err := json.Marshal(j)
if err != nil {
panic(err)
}
l.Logger.WithContext(l.Ctx).WithFields(l.Fields).Println(string(b))
l.Logger.WithContext(l.Ctx).Println(string(b))
}

func (l *EchoLogrusLogger) Debug(i ...interface{}) {
l.Logger.WithContext(l.Ctx).WithFields(l.Fields).Debug(i...)
l.Logger.WithContext(l.Ctx).Debug(i...)
}

func (l *EchoLogrusLogger) Debugf(format string, args ...interface{}) {
l.Logger.WithContext(l.Ctx).WithFields(l.Fields).Debugf(format, args...)
l.Logger.WithContext(l.Ctx).Debugf(format, args...)
}

func (l *EchoLogrusLogger) Debugj(j log.JSON) {
b, err := json.Marshal(j)
if err != nil {
panic(err)
}
l.Logger.WithContext(l.Ctx).WithFields(l.Fields).Debugln(string(b))
l.Logger.WithContext(l.Ctx).Debugln(string(b))
}

func (l *EchoLogrusLogger) Info(i ...interface{}) {
l.Logger.WithContext(l.Ctx).WithFields(l.Fields).Info(i...)
l.Logger.WithContext(l.Ctx).Info(i...)
}

func (l *EchoLogrusLogger) Infof(format string, args ...interface{}) {
l.Logger.WithContext(l.Ctx).WithFields(l.Fields).Infof(format, args...)
l.Logger.WithContext(l.Ctx).Infof(format, args...)
}

func (l *EchoLogrusLogger) Infoj(j log.JSON) {
b, err := json.Marshal(j)
if err != nil {
panic(err)
}
l.Logger.WithContext(l.Ctx).WithFields(l.Fields).Infoln(string(b))
l.Logger.WithContext(l.Ctx).Infoln(string(b))
}

func (l *EchoLogrusLogger) Warn(i ...interface{}) {
l.Logger.WithContext(l.Ctx).WithFields(l.Fields).Warn(i...)
l.Logger.WithContext(l.Ctx).Warn(i...)
}

func (l *EchoLogrusLogger) Warnf(format string, args ...interface{}) {
l.Logger.WithContext(l.Ctx).WithFields(l.Fields).Warnf(format, args...)
l.Logger.WithContext(l.Ctx).Warnf(format, args...)
}

func (l *EchoLogrusLogger) Warnj(j log.JSON) {
b, err := json.Marshal(j)
if err != nil {
panic(err)
}
l.Logger.WithContext(l.Ctx).WithFields(l.Fields).Warnln(string(b))
l.Logger.WithContext(l.Ctx).Warnln(string(b))
}

func (l *EchoLogrusLogger) Error(i ...interface{}) {
l.Logger.WithContext(l.Ctx).WithFields(l.Fields).Error(i...)
l.Logger.WithContext(l.Ctx).Error(i...)
}

func (l *EchoLogrusLogger) Errorf(format string, args ...interface{}) {
l.Logger.WithContext(l.Ctx).WithFields(l.Fields).Errorf(format, args...)
l.Logger.WithContext(l.Ctx).Errorf(format, args...)
}

func (l *EchoLogrusLogger) Errorj(j log.JSON) {
b, err := json.Marshal(j)
if err != nil {
panic(err)
}
l.Logger.WithContext(l.Ctx).WithFields(l.Fields).Errorln(string(b))
l.Logger.WithContext(l.Ctx).Errorln(string(b))
}

func (l *EchoLogrusLogger) Fatal(i ...interface{}) {
l.Logger.WithContext(l.Ctx).WithFields(l.Fields).Fatal(i...)
l.Logger.WithContext(l.Ctx).Fatal(i...)
}

func (l *EchoLogrusLogger) Fatalf(format string, args ...interface{}) {
l.Logger.WithContext(l.Ctx).WithFields(l.Fields).Fatalf(format, args...)
l.Logger.WithContext(l.Ctx).Fatalf(format, args...)
}

func (l *EchoLogrusLogger) Fatalj(j log.JSON) {
b, err := json.Marshal(j)
if err != nil {
panic(err)
}
l.Logger.WithContext(l.Ctx).WithFields(l.Fields).Fatalln(string(b))
l.Logger.WithContext(l.Ctx).Fatalln(string(b))
}

func (l *EchoLogrusLogger) Panic(i ...interface{}) {
l.Logger.WithContext(l.Ctx).WithFields(l.Fields).Panic(i...)
l.Logger.WithContext(l.Ctx).Panic(i...)
}

func (l *EchoLogrusLogger) Panicf(format string, args ...interface{}) {
l.Logger.WithContext(l.Ctx).WithFields(l.Fields).Panicf(format, args...)
l.Logger.WithContext(l.Ctx).Panicf(format, args...)
}

func (l *EchoLogrusLogger) Panicj(j log.JSON) {
b, err := json.Marshal(j)
if err != nil {
panic(err)
}
l.Logger.WithContext(l.Ctx).WithFields(l.Fields).Panicln(string(b))
l.Logger.WithContext(l.Ctx).Panicln(string(b))
}
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This file is a full revert. No other changes.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Awesome thanks, looks nicer and even covers more use cases!

Loading