Categorygithub.com/zc2638/wslog
modulepackage
0.0.0-20230907023703-58d4be1e378f
Repository: https://github.com/zc2638/wslog.git
Documentation: pkg.go.dev

# README

wslog

wslog is a wrapper for slog.

In order to ensure only relying on the standard library, the lumberjack package is copied to implement log rolling.

If you don't know how to configure log rolling, please refer to lumberjack.

Installation

go get -u github.com/zc2638/wslog

Definition

Format

  • json represents the JSON format log
  • text represents the Text format log
  • others represent the default Log format log

Examples

package main

import (
	"github.com/zc2638/wslog"
)

func main() {
	cfg := wslog.Confg{
		Format: "json",
		Level:  "info",
	}
	l := wslog.New(cfg)
	l.Info("the info log")
	l.Log(wslog.LevelInfo+1, "the info+1 log")
	l.Log(1, "another info+1 log")
}

Support digital definition level.

cfg := wslog.Confg{
    Format: "json",
    Level:  "info+2", // equivalent to `wslog.LevelInfo+2`
}

Use with context

logger := wslog.New(cfg)
ctx := wslog.WithContext(context.Backgroud(), logger)
l := wslog.FromContext(ctx)
l.Info("the info log")

You can get the built-in level to realize the level change during the running of the program.

level := l.Level().(*wslog.LevelVar)
level.Set(LevelInfo)

You can use a custom Leveler.

level := new(wslog.LevelVar)
wslog.New(cfg, level)

You can use a custom HandlerOptions.

cfg := wslog.Confg{
    Format: "text",
    Level:  "info",
}
handlerOptions := cfg.HandlerOptions()
wslog.New(cfg, handlerOptions)

You can use a custom Handler.

handler := wslog.NewLogHandler(os.Stdout, nil)
wslog.New(cfg, handler)

You can use a custom io.Writer.

wslog.New(cfg, io.Writer(os.Stdout))

Log rolling is built in by default, you can also use the lumberjack package as a writer.

w := &lumberjack.Logger{}
wslog.New(cfg, io.Writer(w))

You may want to replace some key associated content in the log by default.

replaceAttrFunc := func (groups []string, a Attr) Attr {
    // Remove time.
    if a.Key == slog.TimeKey && len(groups) == 0 {
        return slog.Attr{}
    }
    // Remove the directory from the source's filename.
    if a.Key == slog.SourceKey {
        source := a.Value.Any().(*slog.Source)
        source.File = filepath.Base(source.File)
    }
    return a
}
wslog.New(cfg, replaceAttrFunc)

You can combine multiple output sources.

h1 := wslog.NewLogHandler(os.Stdout, nil, false)
h2 := slog.NewJSONHandler(os.Stdout, nil)
h3 := NewKafkaHandler() // custom writer, like kafka
multiHandler := wslog.NewMultiHandler(h1, h2, h3)
wslog.New(cfg, multiHandler)

# Functions

Debug calls Logger.Debug on the default logger.
DebugCtx calls Logger.DebugCtx on the default logger.
Debugf calls Logger.Debugf on the default logger.
Default returns the default Logger.
Error calls Logger.Error on the default logger.
ErrorCtx calls Logger.ErrorCtx on the default logger.
Errorf calls Logger.Errorf on the default logger.
FromContext retrieves the current logger from the context.
FromRequest retrieves the current logger from the request.
Info calls Logger.Info on the default logger.
InfoCtx calls Logger.InfoCtx on the default logger.
Infof calls Logger.Infof on the default logger.
Log calls Logger.Log on the default logger.
LogAttrs calls Logger.LogAttrs on the default logger.
LogAttrsCtx calls Logger.LogAttrsCtx on the default logger.
LogCtx calls Logger.LogCtx on the default logger.
No description provided by the author
NewLogger creates a new Logger with the given non-nil Handler.
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
SetDefault makes l the default Logger.
Warn calls Logger.Warn on the default logger.
WarnCtx calls Logger.WarnCtx on the default logger.
Warnf calls Logger.Warnf on the default logger.
With calls Logger.With on the default logger.
WithContext returns a new context with the provided logger.

# Constants

No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
LevelKey is the key used by the built-in handlers for the level of the log call.
No description provided by the author
MessageKey is the key used by the built-in handlers for the message of the log call.
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
SourceKey is the key used by the built-in handlers for the source file and line of the log call.
TimeKey is the key used by the built-in handlers for the time when the log method is called.

# Structs

No description provided by the author
No description provided by the author
No description provided by the author

# Type aliases

No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author