Skip to content

Commit

Permalink
Merge pull request #13 from nrfta/feat/add-slog-attr-replacer
Browse files Browse the repository at this point in the history
Add slog attribute replacer
  • Loading branch information
josemarluedke authored Apr 22, 2024
2 parents 7c1deac + 8d98bca commit b9047d1
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 21 deletions.
31 changes: 23 additions & 8 deletions slog.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,18 +77,33 @@ func NewSLogGraphQLResponseMiddleware(l *slog.Logger, s VariablesScrubber) graph
slog.LevelInfo,
"GraphQL Request Served",
slog.Group(
"req",
slog.String("query", oc.RawQuery),
slog.Any("variables", s.Scrub(oc.Variables)),
),
slog.Group(
"res",
slog.String("errors", res.Errors.Error()),
"graphql",
slog.Group(
"req",
slog.String("query", oc.RawQuery),
slog.Any("variables", s.Scrub(oc.Variables)),
),
slog.Group(
"res",
slog.String("errors", res.Errors.Error()),
),
slog.Duration("duration", time.Since(start)),
),
slog.Duration("duration", time.Since(start)),
)
}

return res
}
}

func SLogReplaceAttr(_ []string, a slog.Attr) slog.Attr {
switch a.Value.Kind() {
case slog.KindAny:
switch v := a.Value.Any().(type) {
case error:
a.Value = slog.GroupValue(slog.String("message", v.Error()))
}
}

return a
}
59 changes: 46 additions & 13 deletions slog_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,26 +99,59 @@ var _ = Describe("Logger", func() {
subject(graphql.WithOperationContext(context.Background(), oc), handler)

type logOutput struct {
Msg string `json:"msg"`
Req struct {
Query string `json:"query"`
Variables map[string]any `json:"variables"`
} `json:"req"`
Res struct {
Errors string `json:"errors"`
} `json:"res"`
Duration int `json:"duration"`
Msg string `json:"msg"`
Graphql struct {
Req struct {
Query string `json:"query"`
Variables map[string]any `json:"variables"`
} `json:"req"`
Res struct {
Errors string `json:"errors"`
} `json:"res"`
Duration int `json:"duration"`
} `json:"graphql"`
}

var lo logOutput
err := json.Unmarshal(buf.Bytes(), &lo)
g.Expect(err).To(g.Succeed())

g.Expect(lo.Msg).To(g.Equal("GraphQL Request Served"))
g.Expect(lo.Req.Query).To(g.Equal(query))
g.Expect(lo.Req.Variables).To(g.BeNil())
g.Expect(lo.Res.Errors).To(g.Equal(fmt.Sprintf("input: %s\n", errMsg)))
g.Expect(lo.Duration).To(g.BeNumerically(">", 0))
g.Expect(lo.Graphql.Req.Query).To(g.Equal(query))
g.Expect(lo.Graphql.Req.Variables).To(g.BeNil())
g.Expect(lo.Graphql.Res.Errors).To(g.Equal(fmt.Sprintf("input: %s\n", errMsg)))
g.Expect(lo.Graphql.Duration).To(g.BeNumerically(">", 0))
})
})

Describe("SLogReplaceAttr", func() {
It("should return a new slog.Attr with the error message", func() {
var (
buf bytes.Buffer
logger = slog.New(
slog.NewJSONHandler(&buf, &slog.HandlerOptions{ReplaceAttr: SLogReplaceAttr}),
)
errMsg = "error"
)

logger.ErrorContext(
context.Background(),
"An error occurred",
slog.Any("error", errors.New(errMsg)),
)

var lo struct {
Msg string `json:"msg"`
Error struct {
Message string `json:"message"`
} `json:"error"`
}

err := json.Unmarshal(buf.Bytes(), &lo)
g.Expect(err).To(g.Succeed())

g.Expect(lo.Msg).To(g.Equal("An error occurred"))
g.Expect(lo.Error.Message).To(g.Equal(errMsg))
})
})
})

0 comments on commit b9047d1

Please sign in to comment.