slog

package module
v0.0.0-...-7ce2150 Latest Latest
Warning

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

Go to latest
Published: Jul 23, 2023 License: BSD-3-Clause Imports: 14 Imported by: 39

README

Overview

slog is a collection of handy log utilities, including ZapLogger, Scavenger and DumbLogger.

Each of them implements the following interface:

type Logger interface {
    NewLoggerWith(keyVals ...any) Logger

    Debug(args ...any)
    Info(args ...any)
    Warn(args ...any)
    Error(args ...any)

    Debugf(format string, args ...any)
    Infof(format string, args ...any)
    Warnf(format string, args ...any)
    Errorf(format string, args ...any)

    Debugw(msg string, keyVals ...any)
    Infow(msg string, keyVals ...any)
    Warnw(msg string, keyVals ...any)
    Errorw(msg string, keyVals ...any)

    FlushLogger() error
}

Getting Started

go get -u github.com/edwingeng/slog

Usage

logger, err := NewDevelopmentConfig().Build()
if err != nil {
    panic(err)
}

logger.Debug("str1")
logger.Infof("str2: %d", 200)
logger.Warnw("str3", "foo", 300)

type HandlerContext struct {
    context.Context
    Logger
}

ctx := &HandlerContext{
    Context: context.TODO(),
    Logger:  logger.NewLoggerWith("handler", "UpdateUserName"),
}
ctx.Error("invalid user name")

// Output:
// 15|23:10:48     DEBUG   slog/example_test.go:11 str1
// 15|23:10:48     INFO    slog/example_test.go:12 str2: 200
// 15|23:10:48     WARN    slog/example_test.go:13 str3    {"foo": 300}
// 15|23:10:48     ERROR   slog/example_test.go:24 invalid user name       {"handler": "UpdateUserName"}

Scavenger

I love Scavenger the most. Scavenger saves all log messages in memory for later use, which makes it much easier to design complex test cases.

func NewScavenger() *Scavenger

func (sc *Scavenger) StringExists(str string) (yes bool)
func (sc *Scavenger) UniqueStringExists(str string) (yes bool)
func (sc *Scavenger) FindStringSequence(seq []string) (found int, yes bool)
func (sc *Scavenger) RegexpExists(pat string) (yes bool)
func (sc *Scavenger) UniqueRegexpExists(pat string) (yes bool)
func (sc *Scavenger) FindRegexpSequence(seq []string) (found int, yes bool)
func (sc *Scavenger) Exists(str string) (yes bool)
func (sc *Scavenger) UniqueExists(str string) (yes bool)
func (sc *Scavenger) FindSequence(seq []string) (found int, yes bool)
func (sc *Scavenger) Finder() *MessageFinder

func (sc *Scavenger) Dump() string
func (sc *Scavenger) Entries() []LogEntry
func (sc *Scavenger) Filter(fn func(level, msg string) bool) *Scavenger
func (sc *Scavenger) Len() int
func (sc *Scavenger) Reset()

func (sc *Scavenger) Debug(args ...any)
func (sc *Scavenger) Debugf(format string, args ...any)
func (sc *Scavenger) Debugw(msg string, keyVals ...any)
func (sc *Scavenger) Info(args ...any)
func (sc *Scavenger) Infof(format string, args ...any)
func (sc *Scavenger) Infow(msg string, keyVals ...any)
func (sc *Scavenger) Warn(args ...any)
func (sc *Scavenger) Warnf(format string, args ...any)
func (sc *Scavenger) Warnw(msg string, keyVals ...any)
func (sc *Scavenger) Error(args ...any)
func (sc *Scavenger) Errorf(format string, args ...any)
func (sc *Scavenger) Errorw(msg string, keyVals ...any)

func (sc *Scavenger) NewLoggerWith(keyVals ...any) Logger
func (sc *Scavenger) FlushLogger() error

Documentation

Overview

Example
logger, err := NewDevelopmentConfig().Build()
if err != nil {
	panic(err)
}

logger.Debug("str1")
logger.Infof("str2: %d", 200)
logger.Warnw("str3", "foo", 300)

type HandlerContext struct {
	context.Context
	Logger
}

ctx := &HandlerContext{
	Context: context.TODO(),
	Logger:  logger.NewLoggerWith("handler", "UpdateUserName"),
}
ctx.Error("invalid user name")
Output:

Index

Examples

Constants

View Source
const (
	LevelDebug = "DEBUG"
	LevelInfo  = "INFO"
	LevelWarn  = "WARN"
	LevelError = "ERROR"
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Config

type Config struct {
	zap.Config
}

func NewDevelopmentConfig

func NewDevelopmentConfig() *Config

func NewDevelopmentConfigWith

func NewDevelopmentConfigWith(dateTimeFormat string) *Config

func NewProductionConfig

func NewProductionConfig() *Config

func (*Config) Build

func (cfg *Config) Build(opts ...zap.Option) (*ZapLogger, error)

func (*Config) MustBuild

func (cfg *Config) MustBuild(opts ...zap.Option) *ZapLogger

type DumbLogger

type DumbLogger struct{}

DumbLogger devours all log messages and outputs nothing.

func NewDumbLogger

func NewDumbLogger() DumbLogger

NewDumbLogger creates a new DumbLogger.

func (DumbLogger) Debug

func (DumbLogger) Debug(args ...any)

func (DumbLogger) Debugf

func (DumbLogger) Debugf(format string, args ...any)

func (DumbLogger) Debugw

func (DumbLogger) Debugw(msg string, keyVals ...any)

func (DumbLogger) Error

func (DumbLogger) Error(args ...any)

func (DumbLogger) Errorf

func (DumbLogger) Errorf(format string, args ...any)

func (DumbLogger) Errorw

func (DumbLogger) Errorw(msg string, keyVals ...any)

func (DumbLogger) FlushLogger

func (DumbLogger) FlushLogger() error

func (DumbLogger) Info

func (DumbLogger) Info(args ...any)

func (DumbLogger) Infof

func (DumbLogger) Infof(format string, args ...any)

func (DumbLogger) Infow

func (DumbLogger) Infow(msg string, keyVals ...any)

func (DumbLogger) NewLoggerWith

func (DumbLogger) NewLoggerWith(keyVals ...any) Logger

func (DumbLogger) Warn

func (DumbLogger) Warn(args ...any)

func (DumbLogger) Warnf

func (DumbLogger) Warnf(format string, args ...any)

func (DumbLogger) Warnw

func (DumbLogger) Warnw(msg string, keyVals ...any)

type LogEntry

type LogEntry struct {
	Level   string `json:"level"`
	Message string `json:"message"`
}

type Logger

type Logger interface {
	// NewLoggerWith adds a variadic number of fields to the logging context. It accepts a
	// mix of zap.Field objects and loosely-typed key-value pairs. When
	// processing pairs, the first element of the pair is used as the field key
	// and the second as the field value. The keys in key-value pairs should be strings.
	NewLoggerWith(keyVals ...any) Logger

	// Debug uses fmt.Sprint to construct and log a message.
	Debug(args ...any)
	// Info uses fmt.Sprint to construct and log a message.
	Info(args ...any)
	// Warn uses fmt.Sprint to construct and log a message.
	Warn(args ...any)
	// Error uses fmt.Sprint to construct and log a message.
	Error(args ...any)

	// Debugf uses fmt.Sprintf to log a templated message.
	Debugf(format string, args ...any)
	// Infof uses fmt.Sprintf to log a templated message.
	Infof(format string, args ...any)
	// Warnf uses fmt.Sprintf to log a templated message.
	Warnf(format string, args ...any)
	// Errorf uses fmt.Sprintf to log a templated message.
	Errorf(format string, args ...any)

	// Debugw logs a message with some additional context. The variadic key-value
	// pairs are treated as they are in NewLoggerWith.
	Debugw(msg string, keyVals ...any)
	// Infow logs a message with some additional context. The variadic key-value
	// pairs are treated as they are in NewLoggerWith.
	Infow(msg string, keyVals ...any)
	// Warnw logs a message with some additional context. The variadic key-value
	// pairs are treated as they are in NewLoggerWith.
	Warnw(msg string, keyVals ...any)
	// Errorw logs a message with some additional context. The variadic key-value
	// pairs are treated as they are in NewLoggerWith.
	Errorw(msg string, keyVals ...any)

	// FlushLogger flushes any buffered log entries.
	FlushLogger() error
}

Logger is an interface which is highly compatible with zap.SugaredLogger.

type MessageFinder

type MessageFinder Scavenger

func (*MessageFinder) Find

func (mf *MessageFinder) Find(str string) (string, int, bool)

func (*MessageFinder) FindRegexp

func (mf *MessageFinder) FindRegexp(pat string) (string, int, bool)

func (*MessageFinder) FindRegexpSequence

func (mf *MessageFinder) FindRegexpSequence(a []string) (int, bool)

func (*MessageFinder) FindSequence

func (mf *MessageFinder) FindSequence(a []string) (int, bool)

func (*MessageFinder) FindString

func (mf *MessageFinder) FindString(str string) (string, int, bool)

func (*MessageFinder) FindStringSequence

func (mf *MessageFinder) FindStringSequence(a []string) (int, bool)

func (*MessageFinder) FindUnique

func (mf *MessageFinder) FindUnique(str string) (string, int, bool)

func (*MessageFinder) FindUniqueRegexp

func (mf *MessageFinder) FindUniqueRegexp(pat string) (string, int, bool)

func (*MessageFinder) FindUniqueString

func (mf *MessageFinder) FindUniqueString(str string) (string, int, bool)

type Scavenger

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

Scavenger collects all log messages for later queries.

func NewScavenger

func NewScavenger() *Scavenger

NewScavenger creates a new Scavenger.

func (*Scavenger) Debug

func (sc *Scavenger) Debug(args ...any)

func (*Scavenger) Debugf

func (sc *Scavenger) Debugf(format string, args ...any)

func (*Scavenger) Debugw

func (sc *Scavenger) Debugw(msg string, keyVals ...any)

func (*Scavenger) Dump

func (sc *Scavenger) Dump() string

Dump returns a string that contains all the collected messages.

func (*Scavenger) Entries

func (sc *Scavenger) Entries() []LogEntry

Entries returns a duplicate of the collected messages.

func (*Scavenger) Error

func (sc *Scavenger) Error(args ...any)

func (*Scavenger) Errorf

func (sc *Scavenger) Errorf(format string, args ...any)

func (*Scavenger) Errorw

func (sc *Scavenger) Errorw(msg string, keyVals ...any)

func (*Scavenger) Exists

func (sc *Scavenger) Exists(str string) (yes bool)

Exists returns whether any collected message contains str. If str starts with "rex: ", it is regarded as a regular expression.

func (*Scavenger) Filter

func (sc *Scavenger) Filter(fn func(level, msg string) bool) *Scavenger

Filter creates a new Scavenger that contains only the log messages satisfying the predicate fn.

func (*Scavenger) FindRegexpSequence

func (sc *Scavenger) FindRegexpSequence(seq []string) (found int, yes bool)

FindRegexpSequence returns whether the collected messages contain the specified regular expression sequence.

func (*Scavenger) FindSequence

func (sc *Scavenger) FindSequence(seq []string) (found int, yes bool)

FindSequence returns whether the collected messages contain the specified sequence. If a string in seq starts with "rex: ", it is regarded as a regular expression.

func (*Scavenger) FindStringSequence

func (sc *Scavenger) FindStringSequence(seq []string) (found int, yes bool)

FindStringSequence returns whether the collected messages contain the specified sequence.

func (*Scavenger) Finder

func (sc *Scavenger) Finder() *MessageFinder

Finder returns a MessageFinder.

func (*Scavenger) FlushLogger

func (sc *Scavenger) FlushLogger() error

func (*Scavenger) Info

func (sc *Scavenger) Info(args ...any)

func (*Scavenger) Infof

func (sc *Scavenger) Infof(format string, args ...any)

func (*Scavenger) Infow

func (sc *Scavenger) Infow(msg string, keyVals ...any)

func (*Scavenger) Len

func (sc *Scavenger) Len() int

Len returns the number of the collected messages.

func (*Scavenger) NewLoggerWith

func (sc *Scavenger) NewLoggerWith(keyVals ...any) Logger

func (*Scavenger) RegexpExists

func (sc *Scavenger) RegexpExists(pat string) (yes bool)

RegexpExists returns whether any collected message contains the regular expression pat.

func (*Scavenger) Reset

func (sc *Scavenger) Reset()

Reset clears all collected messages.

func (*Scavenger) StringExists

func (sc *Scavenger) StringExists(str string) (yes bool)

StringExists returns whether any collected message contains str.

func (*Scavenger) UniqueExists

func (sc *Scavenger) UniqueExists(str string) (yes bool)

UniqueExists returns whether one and only one collected message contains str. If str starts with "rex: ", it is regarded as a regular expression.

func (*Scavenger) UniqueRegexpExists

func (sc *Scavenger) UniqueRegexpExists(pat string) (yes bool)

UniqueRegexpExists returns whether one and only one collected message contains the regular expression pat.

func (*Scavenger) UniqueStringExists

func (sc *Scavenger) UniqueStringExists(str string) (yes bool)

UniqueStringExists returns whether one and only one collected message contains str.

func (*Scavenger) Warn

func (sc *Scavenger) Warn(args ...any)

func (*Scavenger) Warnf

func (sc *Scavenger) Warnf(format string, args ...any)

func (*Scavenger) Warnw

func (sc *Scavenger) Warnw(msg string, keyVals ...any)

type ZapLogger

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

ZapLogger is a wrapper of zap.SugaredLogger.

func NewZapLogger

func NewZapLogger(zsl *zap.SugaredLogger) *ZapLogger

NewZapLogger creates a new ZapLogger.

func (*ZapLogger) Debug

func (zl *ZapLogger) Debug(args ...any)

func (*ZapLogger) Debugf

func (zl *ZapLogger) Debugf(format string, args ...any)

func (*ZapLogger) Debugw

func (zl *ZapLogger) Debugw(msg string, keyVals ...any)

func (*ZapLogger) Error

func (zl *ZapLogger) Error(args ...any)

func (*ZapLogger) Errorf

func (zl *ZapLogger) Errorf(format string, args ...any)

func (*ZapLogger) Errorw

func (zl *ZapLogger) Errorw(msg string, keyVals ...any)

func (*ZapLogger) FlushLogger

func (zl *ZapLogger) FlushLogger() error

func (*ZapLogger) Info

func (zl *ZapLogger) Info(args ...any)

func (*ZapLogger) Infof

func (zl *ZapLogger) Infof(format string, args ...any)

func (*ZapLogger) Infow

func (zl *ZapLogger) Infow(msg string, keyVals ...any)

func (*ZapLogger) NewLoggerWith

func (zl *ZapLogger) NewLoggerWith(keyVals ...any) Logger

func (*ZapLogger) Warn

func (zl *ZapLogger) Warn(args ...any)

func (*ZapLogger) Warnf

func (zl *ZapLogger) Warnf(format string, args ...any)

func (*ZapLogger) Warnw

func (zl *ZapLogger) Warnw(msg string, keyVals ...any)

func (*ZapLogger) Zap

func (zl *ZapLogger) Zap() *zap.SugaredLogger

Zap returns the internal zap.SugaredLogger to the caller.

Jump to

Keyboard shortcuts

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