Skip to content

Commit

Permalink
logging: fix caller in echo_logrus
Browse files Browse the repository at this point in the history
  • Loading branch information
schuellerf committed May 7, 2024
1 parent 8e7e1ba commit 6873ae1
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 21 deletions.
5 changes: 5 additions & 0 deletions cmd/image-builder/logging.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package main

import (
"context"
"runtime"
"strings"

"github.com/labstack/echo/v4"
Expand Down Expand Up @@ -41,6 +42,10 @@ func (h *ctxHook) Fire(e *logrus.Entry) error {
for k, v := range rd {
e.Data[k] = v
}
if e.Context.Value(common.LoggingFrameCtx) != nil {
frame := e.Context.Value(common.LoggingFrameCtx).(runtime.Frame)
e.Caller = &frame
}
}

return nil
Expand Down
63 changes: 42 additions & 21 deletions internal/common/echo_logrus.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,18 @@ import (
"context"
"encoding/json"
"io"
"runtime"

"github.com/labstack/gommon/log"
"github.com/sirupsen/logrus"
)

type ctxKey int

const (
LoggingFrameCtx ctxKey = iota
)

// EchoLogrusLogger extend logrus.Logger
type EchoLogrusLogger struct {
*logrus.Logger
Expand Down Expand Up @@ -39,6 +46,20 @@ func toEchoLevel(level logrus.Level) log.Lvl {
return log.OFF
}

// add the context and caller to the fields
// as logrus will report "echo_logrus.go" otherwise
func (l *EchoLogrusLogger) fixCaller() *logrus.Entry {
rpc := make([]uintptr, 1)
// fixCaller is always 3 frames below the calling context
n := runtime.Callers(3, rpc[:])
if n < 1 {
return l.Logger.WithContext(l.Ctx)
}
frame, _ := runtime.CallersFrames(rpc).Next()
frameOverride := context.WithValue(l.Ctx, LoggingFrameCtx, frame)
return l.Logger.WithContext(frameOverride)
}

func (l *EchoLogrusLogger) Output() io.Writer {
return l.Out
}
Expand Down Expand Up @@ -66,113 +87,113 @@ func (l *EchoLogrusLogger) SetPrefix(p string) {
}

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

func (l *EchoLogrusLogger) Printf(format string, args ...interface{}) {
l.Logger.WithContext(l.Ctx).Printf(format, args...)
l.fixCaller().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).Println(string(b))
l.fixCaller().Println(string(b))
}

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

func (l *EchoLogrusLogger) Debugf(format string, args ...interface{}) {
l.Logger.WithContext(l.Ctx).Debugf(format, args...)
l.fixCaller().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).Debugln(string(b))
l.fixCaller().Debugln(string(b))
}

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

func (l *EchoLogrusLogger) Infof(format string, args ...interface{}) {
l.Logger.WithContext(l.Ctx).Infof(format, args...)
l.fixCaller().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).Infoln(string(b))
l.fixCaller().Infoln(string(b))
}

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

func (l *EchoLogrusLogger) Warnf(format string, args ...interface{}) {
l.Logger.WithContext(l.Ctx).Warnf(format, args...)
l.fixCaller().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).Warnln(string(b))
l.fixCaller().Warnln(string(b))
}

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

func (l *EchoLogrusLogger) Errorf(format string, args ...interface{}) {
l.Logger.WithContext(l.Ctx).Errorf(format, args...)
l.fixCaller().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).Errorln(string(b))
l.fixCaller().Errorln(string(b))
}

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

func (l *EchoLogrusLogger) Fatalf(format string, args ...interface{}) {
l.Logger.WithContext(l.Ctx).Fatalf(format, args...)
l.fixCaller().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).Fatalln(string(b))
l.fixCaller().Fatalln(string(b))
}

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

func (l *EchoLogrusLogger) Panicf(format string, args ...interface{}) {
l.Logger.WithContext(l.Ctx).Panicf(format, args...)
l.fixCaller().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).Panicln(string(b))
l.fixCaller().Panicln(string(b))
}

0 comments on commit 6873ae1

Please sign in to comment.