-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat: adds custom logger package * feat: adds request headers as logging fields in slog * feat: replaces log with slog in api request flow --------- Signed-off-by: Amit Singh <[email protected]>
- Loading branch information
Showing
8 changed files
with
109 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,8 @@ | ||
issues: | ||
exclude: | ||
- (.*)_test\.go$ | ||
- (.*)_test\.go$ | ||
|
||
linters-settings: | ||
errcheck: | ||
exclude-functions: | ||
- (*Logger).Sync |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,12 @@ | ||
package helpers | ||
|
||
const ( | ||
LOCALHOST = "127.0.0.1" | ||
Localhost = "127.0.0.1" | ||
RequestIdHeader = "X-Request-ID" | ||
) | ||
|
||
var ( | ||
ApiReqLogFields = []string { | ||
RequestIdHeader, | ||
} | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package logger | ||
|
||
import ( | ||
"context" | ||
"log/slog" | ||
|
||
"github.com/gin-gonic/gin" | ||
) | ||
|
||
const ( | ||
slogFields = "slog_fields" | ||
) | ||
|
||
type ContextHandler struct { | ||
slog.Handler | ||
} | ||
|
||
// Handle adds contextual attributes to the Record before calling the underlying handler | ||
func (h ContextHandler) Handle(ctx context.Context, r slog.Record) error { | ||
if attrs, ok := ctx.Value(slogFields).([]slog.Attr); ok { | ||
for _, v := range attrs { | ||
r.AddAttrs(v) | ||
} | ||
} | ||
|
||
return h.Handler.Handle(ctx, r) | ||
} | ||
|
||
// AppendGinCtx adds an slog attribute to the provided context so that it will be included | ||
// in any Record created with such context | ||
func AppendGinCtx(parent *gin.Context, attr slog.Attr) { | ||
if v, ok := parent.Value(slogFields).([]slog.Attr); ok { | ||
v = append(v, attr) | ||
parent.Set(slogFields, v) | ||
} | ||
|
||
v := []slog.Attr{} | ||
v = append(v, attr) | ||
parent.Set(slogFields, v) | ||
} |