logger

package
v0.4.6 Latest Latest
Warning

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

Go to latest
Published: Jul 16, 2024 License: MIT Imports: 5 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var Default = &Logger{
	AddSource:     true,
	IsVerbose:     false,
	IsJSONHandler: false,
	Level:         slog.LevelWarn,
	ReplaceAttr:   defaultReplaceAttrFunc,
	Output:        os.Stderr,
}

Default is the default *Logger for the default handler.

Functions

func Configure

func Configure(opts *Logger)

Configure configures the logger by the given options.

Types

type LogHandler

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

LogHandler is a slog.Handler implementation that can be used to log to a file.

func NewHandler

func NewHandler(logger *Logger) LogHandler

func (LogHandler) Enabled

func (t LogHandler) Enabled(ctx context.Context, l slog.Level) bool

func (LogHandler) Handle

func (t LogHandler) Handle(ctx context.Context, r slog.Record) error

func (LogHandler) WithAttrs

func (t LogHandler) WithAttrs(attrs []slog.Attr) slog.Handler

func (LogHandler) WithGroup

func (t LogHandler) WithGroup(name string) slog.Handler

type Logger

type Logger struct {
	// AddSource indicates whether to add source code location to the log.
	AddSource bool

	// IsVerbose indicates whether to enable verbose mode. If true, debug level will be enabled.
	// If false, only warn and error level will be enabled.
	IsVerbose bool

	// Level indicates the log level of the handler. If IsVerbose is true, Level will be slog.LevelDebug.
	Level slog.Level

	// IsJSONHandler indicates whether to use JSON format for log output.
	IsJSONHandler bool

	// ReplaceAttr is a function that can be used to replace the value of an attribute.
	ReplaceAttr func(groups []string, a slog.Attr) slog.Attr

	// Output is the writer to write the log to. If nil, os.Stderr will be used.
	Output io.Writer
}

func (*Logger) SetJSONHandler

func (o *Logger) SetJSONHandler()

func (*Logger) SetMaxLevel

func (o *Logger) SetMaxLevel(level slog.Level)

SetMaxLevel sets the max logging level for logger, default is slog.LevelWarn. if IsVerbose is true, level will be slog.LevelDebug.

func (*Logger) SetOutput

func (o *Logger) SetOutput(output io.Writer)

func (*Logger) SetReplaceAttrFunc

func (o *Logger) SetReplaceAttrFunc(replaceAttrFunc func(groups []string, a slog.Attr) slog.Attr)

func (*Logger) SetTextHandler

func (o *Logger) SetTextHandler()

func (*Logger) SetVerbose

func (o *Logger) SetVerbose()

Jump to

Keyboard shortcuts

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