Skip to content

Commit

Permalink
Add middleware to embed logger into context (#36)
Browse files Browse the repository at this point in the history
  • Loading branch information
harnash authored Sep 21, 2021
1 parent d903559 commit fc8e62d
Showing 1 changed file with 54 additions and 8 deletions.
62 changes: 54 additions & 8 deletions logging/echo_logger.go
Original file line number Diff line number Diff line change
@@ -1,25 +1,43 @@
package logging

import (
"context"
"net/http"

"github.com/labstack/echo/v4"
"github.com/labstack/echo/v4/middleware"
"github.com/opentracing/opentracing-go"
"github.com/uber/jaeger-client-go"
"go.uber.org/zap"
)

type key int

const loggerIDKey key = 119

//LoggerInContext will embed zap.Logger into request context for handler to use
func LoggerInContext(logger *zap.Logger) echo.MiddlewareFunc {
return func(next echo.HandlerFunc) echo.HandlerFunc {
return func(c echo.Context) error {
req := c.Request()
c.SetRequest(req.WithContext(addLoggerToContext(req.Context(), logger)))
return next(c)
}
}
}

// EchoLogger is a middleware and zap to provide an "access log" like logging for each request.
func EchoLogger(logger *zap.Logger) echo.MiddlewareFunc {
return middleware.RequestLoggerWithConfig(middleware.RequestLoggerConfig{
LogRequestID: true,
LogLatency: true,
LogHost: true,
LogRemoteIP: true,
LogReferer: true,
LogURIPath: true,
LogLatency: true,
LogHost: true,
LogRemoteIP: true,
LogReferer: true,
LogURIPath: true,
LogUserAgent: true,
LogMethod: true,
LogStatus: true,
LogMethod: true,
LogStatus: true,
LogValuesFunc: func(c echo.Context, v middleware.RequestLoggerValues) error {
req := c.Request()

Expand All @@ -30,7 +48,7 @@ func EchoLogger(logger *zap.Logger) echo.MiddlewareFunc {
traceID = sc.TraceID().String()
}
}

logger.Info("request",
zap.String("request_id", v.RequestID),
zap.Duration("latency", v.Latency),
Expand All @@ -47,3 +65,31 @@ func EchoLogger(logger *zap.Logger) echo.MiddlewareFunc {
},
})
}

func FromEchoContext(ctx echo.Context) *zap.Logger {
return FromContext(ctx.Request().Context())
}

// FromRequest will return current logger embedded in the given request object
func FromRequest(r *http.Request) *zap.Logger {
return FromContext(r.Context())
}

// FromContext will return current logger from the given context.Context object
func FromContext(ctx context.Context) *zap.Logger {
logger := ctx.Value(loggerIDKey)
if logger == nil {
return nil
}

return logger.(*zap.Logger)
}

func AddToContext(c echo.Context, logger *zap.Logger) {
c.SetRequest(c.Request().WithContext(addLoggerToContext(c.Request().Context(), logger)))
}

// addLoggerToContext adds given logger to the context.Context and returns new context
func addLoggerToContext(ctx context.Context, logger *zap.Logger) context.Context {
return context.WithValue(ctx, loggerIDKey, logger)
}

0 comments on commit fc8e62d

Please sign in to comment.