mirror of
https://github.com/apricote/hcloud-upload-image.git
synced 2026-01-13 21:31:03 +00:00
refactor: change package names
This commit is contained in:
parent
62578ad5c5
commit
4b77b81689
24 changed files with 167 additions and 155 deletions
24
hcloudimages/contextlogger/context.go
Normal file
24
hcloudimages/contextlogger/context.go
Normal file
|
|
@ -0,0 +1,24 @@
|
|||
package contextlogger
|
||||
|
||||
import (
|
||||
"context"
|
||||
"log/slog"
|
||||
)
|
||||
|
||||
type key int
|
||||
|
||||
var loggerKey key
|
||||
|
||||
func New(ctx context.Context, logger *slog.Logger) context.Context {
|
||||
return context.WithValue(ctx, loggerKey, logger)
|
||||
}
|
||||
|
||||
func From(ctx context.Context) *slog.Logger {
|
||||
if ctxLogger := ctx.Value(loggerKey); ctxLogger != nil {
|
||||
if logger, ok := ctxLogger.(*slog.Logger); ok {
|
||||
return logger
|
||||
}
|
||||
}
|
||||
|
||||
return slog.New(discardHandler{})
|
||||
}
|
||||
15
hcloudimages/contextlogger/discard.go
Normal file
15
hcloudimages/contextlogger/discard.go
Normal file
|
|
@ -0,0 +1,15 @@
|
|||
package contextlogger
|
||||
|
||||
import (
|
||||
"context"
|
||||
"log/slog"
|
||||
)
|
||||
|
||||
// discardHandler is a [slog.Handler] that just discards any input. It is a safe default if any library
|
||||
// method does not get passed a logger through the context.
|
||||
type discardHandler struct{}
|
||||
|
||||
func (discardHandler) Enabled(_ context.Context, _ slog.Level) bool { return false }
|
||||
func (discardHandler) Handle(_ context.Context, _ slog.Record) error { return nil }
|
||||
func (d discardHandler) WithAttrs(_ []slog.Attr) slog.Handler { return d }
|
||||
func (d discardHandler) WithGroup(_ string) slog.Handler { return d }
|
||||
Loading…
Add table
Add a link
Reference in a new issue