Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 584cb85

Browse files
authoredNov 7, 2023
request logger: add example for Slog https://pkg.go.dev/log/slog (#2543)
1 parent 4b26cde commit 584cb85

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed
 

‎middleware/request_logger.go

+24
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,30 @@ import (
88
"github.com/labstack/echo/v4"
99
)
1010

11+
// Example for `slog` https://pkg.go.dev/log/slog
12+
// logger := slog.New(slog.NewJSONHandler(os.Stdout, nil))
13+
// e.Use(middleware.RequestLoggerWithConfig(middleware.RequestLoggerConfig{
14+
// LogStatus: true,
15+
// LogURI: true,
16+
// LogError: true,
17+
// HandleError: true, // forwards error to the global error handler, so it can decide appropriate status code
18+
// LogValuesFunc: func(c echo.Context, v middleware.RequestLoggerValues) error {
19+
// if v.Error == nil {
20+
// logger.LogAttrs(context.Background(), slog.LevelInfo, "REQUEST",
21+
// slog.String("uri", v.URI),
22+
// slog.Int("status", v.Status),
23+
// )
24+
// } else {
25+
// logger.LogAttrs(context.Background(), slog.LevelError, "REQUEST_ERROR",
26+
// slog.String("uri", v.URI),
27+
// slog.Int("status", v.Status),
28+
// slog.String("err", v.Error.Error()),
29+
// )
30+
// }
31+
// return nil
32+
// },
33+
// }))
34+
//
1135
// Example for `fmt.Printf`
1236
// e.Use(middleware.RequestLoggerWithConfig(middleware.RequestLoggerConfig{
1337
// LogStatus: true,

0 commit comments

Comments
 (0)
Please sign in to comment.