log

package
v0.65.0 Latest Latest
Warning

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

Go to latest
Published: May 22, 2024 License: Apache-2.0 Imports: 14 Imported by: 0

Documentation

Overview

Package log is used to initialize the logging.

Index

Constants

View Source
const (
	LevelTrace = slog.LevelDebug - 4
	LevelFatal = slog.LevelError + 4
)

Define some extra levels.

Variables

View Source
var Level = new(slog.LevelVar)

Level is the global log level.

Writer is the default global writer.

Functions

func Fatal

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

Fatal emits a FATAL log message.

func Init added in v0.58.0

func Init(level, file string, logfilenum int)

Init initializes the logging configuration.

If file is empty or equal to "stderr", output the log to os.Stderr. If file is equal to "stdout", output the log to os.Stdout. Or, output the log to the given file.

func NewFileWriter added in v0.56.0

func NewFileWriter(filename, filesize string, filenum int) (io.WriteCloser, error)

NewFileWriter returns a new file writer that rotates the files based on the file size, which is used as the log writer.

filesize is parsed as the file size, which maybe have a unit suffix, such as "123", "123M, 123G". Valid size units contain "b", "B", "k", "K", "m", "M", "g", "G", "t", "T", "p", "P", "e", "E". The lower units are 1000x, and the upper units are 1024x.

func NewJSONHandler added in v0.56.0

func NewJSONHandler(w io.Writer, level slog.Leveler) slog.Handler

NewJSONHandler returns a new json handler.

If w is nil, use Writer instead.

func SetAppName added in v0.62.0

func SetAppName(appname string)

SetAppName sets the app name to append it into the log attrs.

func SetDefault added in v0.56.0

func SetDefault(handler slog.Handler, attrs ...slog.Attr)

SetDefault is used to set default global logger with the handler.

func SetLevel added in v0.44.0

func SetLevel(level string) error

SetLevel resets the log level, which supports the case-insensitive string as follow:

trace
debug
info
warn
error
fatal

If empty, use slog.LevelInfo instead. If an integer, convert it to slog.Level directly.

func Trace

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

Trace emits a TRACE log message.

Types

type OptionHandler added in v0.56.0

type OptionHandler struct {
	slog.Handler

	// Options
	EnableFunc  func(context.Context, slog.Level) bool         // Default: nil
	FilterFunc  func(context.Context, slog.Record) bool        // Default: nil
	ReplaceFunc func(context.Context, slog.Record) slog.Record // Default: nil
}

func NewOptionHandler added in v0.56.0

func NewOptionHandler(handler slog.Handler) *OptionHandler

NewOptionHandler returns a new OptionHandler wrapping the given handler.

func (*OptionHandler) Enabled added in v0.56.0

func (h *OptionHandler) Enabled(c context.Context, l slog.Level) bool

Enabled implements the interface Handler#Enabled.

func (*OptionHandler) Handle added in v0.56.0

func (h *OptionHandler) Handle(c context.Context, r slog.Record) error

Handle implements the interface Handler#Handle.

func (*OptionHandler) Unwrap added in v0.56.0

func (h *OptionHandler) Unwrap() slog.Handler

Unwrap returns the inner wrapped slog handler.

func (*OptionHandler) WithAttrs added in v0.56.0

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

WithAttrs implements the interface Handler#WithAttrs.

func (*OptionHandler) WithGroup added in v0.56.0

func (h *OptionHandler) WithGroup(name string) slog.Handler

WithGroup implements the interface Handler#WithGroup.

Directories

Path Synopsis
Package provides a rotating file based on the file size.
Package provides a rotating file based on the file size.

Jump to

Keyboard shortcuts

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