Documentation ¶
Overview ¶
Package log is used to initialize the logging.
Index ¶
- Constants
- Variables
- func Fatal(msg string, args ...any)
- func Init(level, file string, logfilenum int)
- func NewFileWriter(filename, filesize string, filenum int) (io.WriteCloser, error)
- func NewJSONHandler(w io.Writer, level slog.Leveler) slog.Handler
- func SetAppName(appname string)
- func SetDefault(handler slog.Handler, attrs ...slog.Attr)
- func SetLevel(level string) error
- func Trace(msg string, args ...any)
- type OptionHandler
- func (h *OptionHandler) Enabled(c context.Context, l slog.Level) bool
- func (h *OptionHandler) Handle(c context.Context, r slog.Record) error
- func (h *OptionHandler) Unwrap() slog.Handler
- func (h *OptionHandler) WithAttrs(attrs []slog.Attr) slog.Handler
- func (h *OptionHandler) WithGroup(name string) slog.Handler
Constants ¶
const ( LevelTrace = slog.LevelDebug - 4 LevelFatal = slog.LevelError + 4 )
Define some extra levels.
Variables ¶
var Level = new(slog.LevelVar)
Level is the global log level.
var Writer = writer.NewSwitcher(os.Stderr)
Writer is the default global writer.
Functions ¶
func Init ¶ added in v0.58.0
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
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
SetDefault is used to set default global logger with the handler.
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) Unwrap ¶ added in v0.56.0
func (h *OptionHandler) Unwrap() slog.Handler
Unwrap returns the inner wrapped slog handler.