logger

package
v1.2.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 3, 2025 License: AGPL-3.0 Imports: 10 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CreateLogger

func CreateLogger(out io.Writer, format LogFormat, level LogLevel) func() *slog.Logger

func GetLogger added in v1.2.0

func GetLogger(ctx context.Context) *slog.Logger

GetLogger retrieves a logger instance from the given context.

This function attempts to extract a logger from the context using the key "logger". If the key is not present or the value is not of type *slog.Logger, it falls back to returning the default logger provided by slog.

Usage:

  1. To use this function effectively, ensure that the context has a logger stored using context.WithValue, with the key "logger" and a value of type *slog.Logger.
  2. If no logger is found in the context, slog.Default() is returned.

Parameters: - ctx (context.Context): The context from which the logger will be extracted.

Returns: - *slog.Logger: The logger instance extracted from the context or the default logger.

Example: ```go logger := slog.New(slog.WithLevel(slog.LevelInfo)) ctx := context.WithValue(context.Background(), "logger", logger) retrievedLogger := GetLogger(ctx) retrievedLogger.Info("Logger successfully retrieved!") ```

Types

type GroupOrAttrs added in v1.2.0

type GroupOrAttrs struct {
	// contains filtered or unexported fields
}

type LogFormat

type LogFormat string
const (
	JSON    LogFormat = "json"
	Text    LogFormat = "text"
	Console LogFormat = "console"
)

type LogLevel

type LogLevel string
const (
	Debug LogLevel = "debug"
	Info  LogLevel = "info"
	Warn  LogLevel = "warn"
	Error LogLevel = "error"
)

func (LogLevel) ToSLog

func (l LogLevel) ToSLog() slog.Level

type PrettyHandler added in v1.2.0

type PrettyHandler struct {
	slog.Handler
	// contains filtered or unexported fields
}

func NewPrettyHandler added in v1.2.0

func NewPrettyHandler(out io.Writer, opts PrettyHandlerOptions) *PrettyHandler

func (*PrettyHandler) Handle added in v1.2.0

func (h *PrettyHandler) Handle(_ context.Context, r slog.Record) error

func (*PrettyHandler) WithAttrs added in v1.2.0

func (h *PrettyHandler) WithAttrs(attrs []slog.Attr) slog.Handler

func (*PrettyHandler) WithGroup added in v1.2.0

func (h *PrettyHandler) WithGroup(group string) slog.Handler

type PrettyHandlerOptions added in v1.2.0

type PrettyHandlerOptions struct {
	SlogOpts slog.HandlerOptions
}

type TimeSince added in v1.2.0

type TimeSince struct {
	// contains filtered or unexported fields
}

func NewTimeSince added in v1.2.0

func NewTimeSince() *TimeSince

func (*TimeSince) LogValue added in v1.2.0

func (t *TimeSince) LogValue() slog.Value

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL