slog

package module
v0.0.0-...-43ce19c Latest Latest
Warning

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

Go to latest
Published: May 10, 2024 License: BSD-3-Clause Imports: 14 Imported by: 39

README

Overview

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

Each of them implements the following interface:

type Logger interface {
    NewLoggerWith(keyVals ...any) Logger
    LogLevelEnabled(level int) bool
    FlushLogger() error

    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)
}

Getting Started

go get -u github.com/edwingeng/slog

Usage

logger, err := slog.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
    slog.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) Exists(str string) bool
func (sc *Scavenger) SequenceExists(seq []string) bool
func (sc *Scavenger) Finder() *MessageFinder

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

func (sc *Scavenger) NewLoggerWith(keyVals ...any) Logger
func (sc *Scavenger) LogLevelEnabled(level int) bool
func (sc *Scavenger) FlushLogger() error

func (sc *Scavenger) Debug(args ...any)
func (sc *Scavenger) Info(args ...any)
func (sc *Scavenger) Warn(args ...any)
func (sc *Scavenger) Error(args ...any)

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

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

Documentation

Overview

Example
package main

import (
	"context"
	"github.com/edwingeng/slog"
)

func main() {
	logger, err := slog.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
		slog.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 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

	// LogLevelEnabled checks if the given log level is enabled.
	LogLevelEnabled(level int) bool

	// 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.

func NewDevourer

func NewDevourer() Logger

NewDevourer creates a new Logger that devours all log messages and outputs nothing.

type MessageFinder

type MessageFinder Scavenger

func (*MessageFinder) Find

func (mf *MessageFinder) Find(str string) []int

func (*MessageFinder) FindRegexp

func (mf *MessageFinder) FindRegexp(pat string) []int

func (*MessageFinder) FindRegexpSequence

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

func (*MessageFinder) FindSequence

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

func (*MessageFinder) FindString

func (mf *MessageFinder) FindString(str string) []int

func (*MessageFinder) FindStringSequence

func (mf *MessageFinder) FindStringSequence(seq []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) bool

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) 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) LogEntry

func (sc *Scavenger) LogEntry(index int) LogEntry

LogEntry returns the log entry at index.

func (*Scavenger) LogLevelEnabled

func (sc *Scavenger) LogLevelEnabled(level int) bool

func (*Scavenger) NewLoggerWith

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

func (*Scavenger) Reset

func (sc *Scavenger) Reset()

Reset clears all collected messages.

func (*Scavenger) SequenceExists

func (sc *Scavenger) SequenceExists(seq []string) bool

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) LogLevelEnabled

func (zl *ZapLogger) LogLevelEnabled(level int) bool

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.Logger

Zap returns the internal zap.Logger to the caller.

Jump to

Keyboard shortcuts

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