Select Git revision
logging_middleware.go
logging_middleware.go 5.91 KiB
package middleware
// Code generated by gowrap. DO NOT EDIT.
// template: ../../../assets/templates/middleware/access_log
// gowrap: http://github.com/hexdigest/gowrap
//go:generate gowrap gen -p git.perx.ru/perxis/perxis-go/pkg/spaces -i Spaces -t ../../../assets/templates/middleware/access_log -o logging_middleware.go -l ""
import (
"context"
"fmt"
"time"
"git.perx.ru/perxis/perxis-go/pkg/auth"
"git.perx.ru/perxis/perxis-go/pkg/spaces"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
)
// loggingMiddleware implements spaces.Spaces that is instrumented with logging
type loggingMiddleware struct {
logger *zap.Logger
next spaces.Spaces
}
// LoggingMiddleware instruments an implementation of the spaces.Spaces with simple logging
func LoggingMiddleware(logger *zap.Logger) Middleware {
return func(next spaces.Spaces) spaces.Spaces {
return &loggingMiddleware{
next: next,
logger: logger,
}
}
}
func (m *loggingMiddleware) Create(ctx context.Context, space *spaces.Space) (created *spaces.Space, err error) {
begin := time.Now()
var fields []zapcore.Field
for k, v := range map[string]interface{}{
"ctx": ctx,
"space": space} {
if k == "ctx" {
fields = append(fields, zap.String("principal", fmt.Sprint(auth.GetPrincipal(ctx))))
continue
}
fields = append(fields, zap.Reflect(k, v))
}
m.logger.Debug("Create.Request", fields...)
created, err = m.next.Create(ctx, space)
fields = []zapcore.Field{
zap.Duration("time", time.Since(begin)),
}
for k, v := range map[string]interface{}{
"created": created,
"err": err} {
if k == "err" {
err, _ := v.(error)
fields = append(fields, zap.Error(err))
continue
}
fields = append(fields, zap.Reflect(k, v))
}
m.logger.Debug("Create.Response", fields...)
return created, err