log

package module
v0.0.0-...-a672c30 Latest Latest
Warning

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

Go to latest
Published: Apr 16, 2025 License: MIT Imports: 24 Imported by: 0

README

log

This structured logging library is developed with reference to Go's standard log/slog package. Although slog provides a solid foundation for structured logging, it does not support delayed field loading with the With method, resulting in premature evaluation of dynamic values. This reduces flexibility and efficiency in scenarios where log attributes—such as timestamps, request IDs, or metrics—require runtime computation. To address this, we rewrote this library.

Install

go get github.com/nexuer/log

Performance

After benchmarking, the overall performance is comparable to that of slog.

Benchmarks

Documentation

Index

Constants

View Source
const (
	// LevelKey is the key used by the built-in handlers for the level
	// of the log call.
	LevelKey = "level"
	// MessageKey is the key used by the built-in handlers for the
	// message of the log call. The associated value is a string.
	MessageKey = "msg"
	// NameKey is the key used by the built-in handlers for the logger name.
	NameKey = "logger"
	// ErrKey is the key used by the built-in handlers for the error message.
	ErrKey = "err"
)

Keys for "built-in" attributes.

Variables

View Source
var (
	// DefaultCaller is a Valuer that returns the file and line.
	DefaultCaller = Caller(7)

	// DefaultTimestamp is a Valuer that returns the current wallclock time.
	DefaultTimestamp = Timestamp(time.RFC3339)

	DefaultFields = []any{
		"ts", DefaultTimestamp,
		"caller", DefaultCaller,
	}
)
View Source
var Discard = writerWrapper{Writer: io.Discard}
View Source
var (
	// ErrorHandler is called whenever fails to write an event on its
	// output. default an error is printed on the stderr. This handler must
	// be thread safe and non-blocking.
	ErrorHandler func(err error) = func(err error) {
		_, _ = fmt.Fprintf(os.Stderr, "log: write failed, %v\n", err)
	}
)

Functions

func AddFlags

func AddFlags(fs *flag.FlagSet)

func Close

func Close() error

func Debug

func Debug(args ...any)

Debug logs a message at debug level.

func DebugS

func DebugS(msg string, kvs ...any)

DebugS logs a message at debug level with key vals.

func Debugf

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

Debugf logs a message at debug level.

func Error

func Error(args ...any)

Error logs a message at error level.

func ErrorS

func ErrorS(err error, msg string, kvs ...any)

ErrorS logs a message at error level with key vals.

func Errorf

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

Errorf logs a message at error level.

func Fatal

func Fatal(args ...any)

Fatal logs a message at fatal level.

func FatalS

func FatalS(err error, msg string, kvs ...any)

FatalS logs a message at fatal level with key vals.

func Fatalf

func Fatalf(format string, args ...any)

Fatalf logs a message at fatal level.

func FileWriter

func FileWriter(path string, size int64, backups int64, compress ...bool) io.Writer

func Info

func Info(args ...any)

Info logs a message at info level.

func InfoS

func InfoS(msg string, kvs ...any)

InfoS logs a message at info level with key vals.

func Infof

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

Infof logs a message at info level.

func MultiWriter

func MultiWriter(writers ...io.Writer) io.Writer

MultiWriter creates a writer that duplicates its writes to all the provided writers, similar to the Unix tee(1) command.

Each write is written to each listed writer, one at a time. If a listed writer returns an error, that overall write operation stops and returns the error; it does not continue down the list.

func SetDefault

func SetDefault(l *Logger)

SetDefault makes l the default Logger, which is used by the top-level functions Info, Debug and so on.

func TryMultiWriter

func TryMultiWriter(strategy ByteCountStrategy, writers ...io.Writer) io.Writer

TryMultiWriter creates a writer that attempts to write to all provided io.Writers. The first argument specifies the byte count strategy, which determines how the returned byte count is calculated. It collects all errors and joins them with errors.Join.

func Warn

func Warn(args ...any)

Warn logs a message at warn level.

func WarnS

func WarnS(msg string, kvs ...any)

WarnS logs a message at warn level with key vals.

func Warnf

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

Warnf logs a message at warn level.

func WithCallerDepth

func WithCallerDepth(ctx context.Context, depth int) context.Context

Types

type ByteCountStrategy

type ByteCountStrategy int

ByteCountStrategy defines the strategy for determining the number of bytes returned by Write.

const (
	// StrategyFirst returns the byte count from the first writer.
	StrategyFirst ByteCountStrategy = iota
	// StrategyMin returns the minimum byte count among writes.
	StrategyMin
	// StrategyMax returns the maximum byte count among writes.
	StrategyMax
)

type Config

type Config struct {
	Format   Format
	Level    Level
	Output   Output
	File     FileConfig
	Replacer Replacer
}

type Field

type Field struct {
	Key   string
	Value Value
}

func Any

func Any(key string, value any) Field

Any returns an Attr for the supplied value. See AnyValue for how values are treated.

func Bool

func Bool(key string, v bool) Field

Bool returns an Attr for a bool.

func Duration

func Duration(key string, v time.Duration) Field

Duration returns an Attr for a time.Duration.

func Float64

func Float64(key string, v float64) Field

Float64 returns a Field for a floating-point number.

func Group

func Group(key string, kvs ...any) Field

Group returns an Attr for a Group Value. The first argument is the key; the remaining arguments are converted to Attrs as in Logger.Log.

Use Group to collect several key-value pairs under a single key on a log line, or as the result of LogValue in order to log a single value as multiple Attrs.

func Int

func Int(key string, value int) Field

Int converts an int to an int64 and returns an Attr with that value.

func Int64

func Int64(key string, value