-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: switch to slog instead of standard log pkg (#8)
* chore: switch to slog instead of standard log pkg * chore: bump go v1.21 * chore: use json handler * chore: use a single logger instance * feat: use ctx logger * feat: add new context logger to check config * feat: instrument * chore: add logging to config validation * chore: inline test cases
- Loading branch information
Showing
10 changed files
with
108 additions
and
33 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
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 |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package logger | ||
|
||
import ( | ||
"context" | ||
"log/slog" | ||
"os" | ||
) | ||
|
||
type logger struct{} | ||
|
||
// NewLogger creates a new slog.Logger instance. | ||
// If handlers are provided, the first handler in the slice is used; otherwise, | ||
// a default JSON handler writing to os.Stderr is used. This function allows for | ||
// custom configuration of logging handlers. | ||
func NewLogger(h ...slog.Handler) *slog.Logger { | ||
var handler slog.Handler | ||
if len(h) > 0 { | ||
handler = h[0] | ||
} else { | ||
handler = slog.NewJSONHandler(os.Stderr, nil) | ||
} | ||
return slog.New(handler) | ||
} | ||
|
||
// NewContextWithLogger creates a new context based on the provided parent context. | ||
// It embeds a logger into this new context, which is a child of the logger from the parent context. | ||
// The child logger inherits settings from the parent and is grouped under the provided childName. | ||
// It also returns a cancel function to cancel the new context. | ||
func NewContextWithLogger(parent context.Context, childName string) (context.Context, context.CancelFunc) { | ||
ctx, cancel := context.WithCancel(parent) | ||
return IntoContext(ctx, FromContext(parent).WithGroup(childName)), cancel | ||
} | ||
|
||
// IntoContext embeds the provided slog.Logger into the given context and returns the modified context. | ||
// This function is used for passing loggers through context, allowing for context-aware logging. | ||
func IntoContext(ctx context.Context, log *slog.Logger) context.Context { | ||
return context.WithValue(ctx, logger{}, log) | ||
} | ||
|
||
// FromContext extracts the slog.Logger from the provided context. | ||
// If the context does not have a logger, it returns a new logger with the default configuration. | ||
// This function is useful for retrieving loggers from context in different parts of an application. | ||
func FromContext(ctx context.Context) *slog.Logger { | ||
if ctx != nil { | ||
if logger, ok := ctx.Value(logger{}).(*slog.Logger); ok { | ||
return logger | ||
} | ||
} | ||
return NewLogger() | ||
} |
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
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