logr

package module
v0.1.4 Latest Latest
Warning

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

Go to latest
Published: Aug 14, 2023 License: MIT Imports: 10 Imported by: 3

README

logr

a golang logger library

Usage


func TestDefault(t *testing.T) {

	log := Default().SetLevel(DebugLevel)
	err := errors.New("New_ERROR")

	log = log.With(
		"kk", "vv",
		"caller", CallerFile(4, false),
		"gg",
	)

	log.Debug("number=%d", 1)
	log.Info("number=%d", 1)
	log.Warn(err)
	log.Error(err)

	ctx := WithLogger(context.Background(), log)
	subcaller(ctx)
}

func subcaller(ctx context.Context) {
	log := FromContext(ctx)

	log = log.Start() // time cost
	defer log.Stop()

	time.Sleep(532 * time.Millisecond)
	log.Info("account=%d", 100)
}

This code appears to be a Go (golang) package named logr, which defines a Logger interface and provides a default implementation of this interface in the form of a levelLogger struct. The levelLogger struct contains fields for a logger instance, log level, a boolean flag indicating the presence of "valuer" parameters in the logger's context, and a slice of key-value pairs that represent additional context data for the logger.

The Logger interface defines methods for logging messages at different levels (e.g. debug, info, warning, etc.), as well as methods for adding context data to the logger and starting/stopping a timer.

The levelLogger struct provides implementations for these methods. For example, the Log method takes a log level and message string as arguments, and logs the message at the specified log level. The With method allows additional key-value pairs to be added to the logger's context, and returns a new levelLogger instance with the updated context. The Start and Stop methods can be used to measure the duration of some code execution and include that duration in the log output.

Overall, this package provides a simple and flexible interface for logging in Go applications.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func LevelToText added in v0.1.1

func LevelToText(level Level) string

func WithLogger

func WithLogger(ctx context.Context, log Logger) context.Context

Types

type Config

type Config struct {
	Level   string
	SLogger *slog.Logger
}

type Level added in v0.1.1

type Level int
const (
	DebugLevel Level = iota
	InfoLevel
	WarnLevel
	ErrorLevel
	FatalLevel
)

func LevelFromText added in v0.1.1

func LevelFromText(level string) Level

func (Level) Level added in v0.1.3

func (l Level) Level() Level

func (Level) MarshalJSON added in v0.1.3

func (l Level) MarshalJSON() ([]byte, error)

func (Level) String added in v0.1.3

func (l Level) String() string

type Logger

type Logger interface {
	Debug(format string, args ...any)
	Info(format string, args ...any)
	Warn(err error)
	Error(err error)

	With(args ...any) Logger

	// 启动计时器
	// log = log.Start()
	// defer log.Stop()
	Start() Logger
	Stop()

	Enabled(level Level) bool
	SetLevel(level Level) Logger
}

func Default

func Default() Logger

func FromContext

func FromContext(ctx context.Context) Logger

func New

func New(c Config) Logger

type LogrKey

type LogrKey int

type Valuer

type Valuer = func(context.Context) any

func CallerFile

func CallerFile(dep int, abspath bool) Valuer

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL
JackTT - Gopher 🇻🇳