-
-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #108 from bmf-san/feature/disable-debug-endpoints
Implement contextual logging and disable debug endpoints
- Loading branch information
Showing
9 changed files
with
234 additions
and
168 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 |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package domain | ||
|
||
import "context" | ||
|
||
// A Logger is a logger interface. | ||
type Logger interface { | ||
WithTraceID(context.Context) context.Context | ||
Error(string, ...any) | ||
ErrorContext(context.Context, string, ...any) | ||
Info(string, ...any) | ||
InfoContext(context.Context, string, ...any) | ||
} |
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,16 +1,66 @@ | ||
package infrastructure | ||
|
||
import ( | ||
"context" | ||
"os" | ||
|
||
"log/slog" | ||
|
||
"github.com/google/uuid" | ||
) | ||
|
||
// Logger represents the singular of logger. | ||
type Logger struct { | ||
*slog.Logger | ||
} | ||
|
||
// NewLogger creates a logger. | ||
func NewLogger(level int) *slog.Logger { | ||
handler := slog.NewJSONHandler(os.Stdout, &slog.HandlerOptions{ | ||
func NewLogger(level int) *Logger { | ||
handler := TraceIDHandler{slog.NewJSONHandler(os.Stdout, &slog.HandlerOptions{ | ||
Level: slog.Level(level), | ||
}) | ||
})} | ||
logger := slog.New(handler) | ||
return logger | ||
return &Logger{ | ||
logger, | ||
} | ||
} | ||
|
||
// WithTraceID returns a context with a trace id. | ||
func (l *Logger) WithTraceID(ctx context.Context) context.Context { | ||
uuid, _ := uuid.NewRandom() | ||
return context.WithValue(ctx, ctxTraceIDKey, uuid.String()) | ||
} | ||
|
||
func (l *Logger) Error(msg string, args ...any) { | ||
l.Logger.Error(msg, args...) | ||
} | ||
|
||
func (l *Logger) ErrorContext(ctx context.Context, msg string, args ...any) { | ||
l.Logger.ErrorContext(ctx, msg, args...) | ||
} | ||
|
||
func (l *Logger) Info(msg string, args ...any) { | ||
l.Logger.Info(msg, args...) | ||
} | ||
|
||
func (l *Logger) InfoContext(ctx context.Context, msg string, args ...any) { | ||
l.Logger.InfoContext(ctx, msg, args...) | ||
} | ||
|
||
// TraceIDHandler represents the singular of trace id handler. | ||
type TraceIDHandler struct { | ||
slog.Handler | ||
} | ||
|
||
type ctxTraceID struct{} | ||
|
||
var ctxTraceIDKey = ctxTraceID{} | ||
|
||
// Handle implements slog.Handler. | ||
func (t TraceIDHandler) Handle(ctx context.Context, r slog.Record) error { | ||
tid, ok := ctx.Value(ctxTraceIDKey).(string) | ||
if ok { | ||
r.AddAttrs(slog.String("trace_id", tid)) | ||
} | ||
return t.Handler.Handle(ctx, r) | ||
} |
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
Oops, something went wrong.