logging

package module
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: Apr 21, 2024 License: MIT Imports: 4 Imported by: 0

README

logging wrapper

This is logging wrapper for default slog logger in Go and it is not ideal.

It can helps you to change logger if you need this.

Also this is an example, how to wrap external libraries and don't make depends your project on them.

This project became public for YouTube channel: The Art of Development.

Use, fork, improve it!

Thank you.

Documentation

Index

Constants

View Source
const (
	LevelInfo  = slog.LevelInfo
	LevelWarn  = slog.LevelWarn
	LevelError = slog.LevelError
	LevelDebug = slog.LevelDebug
)

Variables

View Source
var (
	NewTextHandler = slog.NewTextHandler
	NewJSONHandler = slog.NewJSONHandler
	New            = slog.New
	SetDefault     = slog.SetDefault

	StringAttr   = slog.String
	BoolAttr     = slog.Bool
	Float64Attr  = slog.Float64
	AnyAttr      = slog.Any
	DurationAttr = slog.Duration
	IntAttr      = slog.Int
	Int64Attr    = slog.Int64
	Uint64Attr   = slog.Uint64

	GroupValue = slog.GroupValue
	Group      = slog.Group
)

Functions

func ContextWithLogger

func ContextWithLogger(ctx context.Context, l *slog.Logger) context.Context

ContextWithLogger adds logger to context.

Types

type Attr

type Attr = slog.Attr

func ErrAttr

func ErrAttr(err error) Attr

func Float32Attr

func Float32Attr(key string, val float32) Attr

func Int32Attr

func Int32Attr(key string, val int32) Attr

func TimeAttr

func TimeAttr(key string, time time.Time) Attr

func UInt32Attr

func UInt32Attr(key string, val uint32) Attr

type Handler

type Handler = slog.Handler

type HandlerOptions

type HandlerOptions = slog.HandlerOptions

type Level

type Level = slog.Level

type LogValuer

type LogValuer = slog.LogValuer

type Logger

type Logger = slog.Logger

func Default

func Default() *Logger

func L

func L(ctx context.Context) *Logger

func NewLogger

func NewLogger(opts ...LoggerOption) *Logger

func WithAttrs

func WithAttrs(ctx context.Context, attrs ...Attr) *Logger

WithAttrs returns logger with attributes.

func WithDefaultAttrs

func WithDefaultAttrs(logger *Logger, attrs ...Attr) *Logger

WithDefaultAttrs returns logger with default attributes.

type LoggerOption

type LoggerOption func(*LoggerOptions)

func WithAddSource

func WithAddSource(addSource bool) LoggerOption

WithAddSource logger option sets the add source option, which will add source file and line number to the log record.

func WithIsJSON

func WithIsJSON(isJSON bool) LoggerOption

WithIsJSON logger option sets the is json option, which will set JSON format for the log record.

func WithLevel

func WithLevel(level string) LoggerOption

WithLevel logger option sets the log level, if not set, the default level is Info.

func WithSetDefault

func WithSetDefault(setDefault bool) LoggerOption

WithSetDefault logger option sets the set default option, which will set the created logger as default logger.

type LoggerOptions

type LoggerOptions struct {
	Level      Level
	AddSource  bool
	IsJSON     bool
	SetDefault bool
}

type Value

type Value = slog.Value

Jump to

Keyboard shortcuts

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