logger

package
v0.5.5 Latest Latest
Warning

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

Go to latest
Published: Oct 11, 2024 License: GPL-3.0 Imports: 9 Imported by: 2

Documentation

Overview

nolint: sloglint

Index

Constants

View Source
const (
	LevelCritical = slog.Level(12)
	LevelPanic    = slog.Level(14)
	LevelFatal    = slog.Level(16)
)
View Source
const (
	// DefaultLevel is the default minimum reporting level for the logger
	DefaultLevel = slog.LevelDebug
)

Variables

This section is empty.

Functions

func Debug

func Debug(msg string, args ...any)

Debug logs at [LevelDebug].

func DebugContext

func DebugContext(ctx context.Context, msg string, args ...any)

DebugContext logs at [LevelDebug] from logger in the given context.

func Error

func Error(msg string, args ...any)

Error logs at [LevelError] with an error.

func ErrorContext

func ErrorContext(ctx context.Context, msg string, args ...any)

ErrorContext logs at [LevelError] from logger in the given context.

func Fatal

func Fatal(msg string, args ...any)

Fatal logs at LevelFatal followed by a call to os.Exit(1).

func FatalContext

func FatalContext(ctx context.Context, msg string, args ...any)

FatalContext logs at LevelFatal and then os.Exit(1) from logger in the given context.

func FromContext

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

FromContext returns the logger from the context. If no logger is found, a new

func GCPAttrReplacer

func GCPAttrReplacer(groups []string, attr slog.Attr) slog.Attr

GCPAttrReplacer replaces the default attribute keys with the GCP logging attribute keys.

func Info

func Info(msg string, args ...any)

Info logs at [LevelInfo].

func InfoContext

func InfoContext(ctx context.Context, msg string, args ...any)

InfoContext logs at [LevelInfo] from logger in the given context.

func Init

func Init(cfg Config) error

Init initializes global logger and slog logger with given configuration.

func Log

func Log(level slog.Level, msg string, args ...any)

Log emits a log record with the current time and the given level and message. The Record's Attrs consist of the Logger's attributes followed by the Attrs specified by args.

func LogAttrs

func LogAttrs(ctx context.Context, level slog.Level, msg string, attrs ...slog.Attr)

LogAttrs is a more efficient version of [Logger.Log] that accepts only Attrs.

func LogContext

func LogContext(ctx context.Context, level slog.Level, msg string, args ...any)

LogContext logs at the given level from logger in the given context.

func NewContext

func NewContext(ctx context.Context, log *slog.Logger) context.Context

NewContext returns a new context with logger attached.

func NewGCPHandler

func NewGCPHandler(opts *slog.HandlerOptions) slog.Handler

NewGCPHandler returns a new GCP handler. The handler writes logs to the os.Stdout and replaces the default attribute keys/values with the GCP logging attribute keys/values

https://cloud.google.com/logging/docs/reference/v2/rest/v2/LogEntry

func Panic

func Panic(msg string, args ...any)

Panic logs at LevelPanic and then panics.

func PanicContext

func PanicContext(ctx context.Context, msg string, args ...any)

PanicContext logs at LevelPanic and then panics from logger in the given context.

func SetLevel

func SetLevel(level slog.Level) (old slog.Level)

SetLevel sets the minimum reporting level for the logger

func Warn

func Warn(msg string, args ...any)

Warn logs at [LevelWarn].

func WarnContext

func WarnContext(ctx context.Context, msg string, args ...any)

WarnContext logs at [LevelWarn] from logger in the given context.

func With

func With(args ...any) *slog.Logger

With returns a Logger that includes the given attributes in each output operation. Arguments are converted to attributes as if by [Logger.Log].

func WithContext

func WithContext(ctx context.Context, args ...any) context.Context

WithContext returns a new context with given logger attributes.

func WithGroup

func WithGroup(group string) *slog.Logger

WithGroup returns a Logger that starts a group, if name is non-empty. The keys of all attributes added to the Logger will be qualified by the given name. (How that qualification happens depends on the [Handler.WithGroup] method of the Logger's Handler.)

If name is empty, WithGroup returns the receiver.

func WithGroupContext

func WithGroupContext(ctx context.Context, group string) context.Context

WithGroupContext returns a new context with given group.

Types

type Config

type Config struct {
	// Output is the logger output format.
	// Possible values:
	//  - Text (default)
	//  - JSON
	//  - GCP: Output format for Stackdriver Logging/Cloud Logging or others GCP services.
	Output string `mapstructure:"output" env:"OUTPUT" envDefault:"text"`

	// Debug is enabled logger level debug. (default: false)
	Debug bool `mapstructure:"debug" env:"DEBUG" envDefault:"false"`
}

Config is the logger configuration.

Directories

Path Synopsis
slogx is extension of slog package and logger package.
slogx is extension of slog package and logger package.

Jump to

Keyboard shortcuts

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