log

package
v0.0.1 Latest Latest
Warning

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

Go to latest
Published: Jun 9, 2026 License: MIT Imports: 2 Imported by: 77

Documentation

Overview

Package log provides a minimal, backend-agnostic logging interface for the go-wind framework.

It defines a small Logger interface that any project can adapt to its own backend (stdlib *slog.Logger, zap, zerolog, kratos log, …) with a few lines of glue code. A zero-cost [nopLogger] is included as the default; concrete adapters (slog, LevelFilter, MultiLogger) live in go-wind-plugins.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func SetLogger

func SetLogger(l Logger)

SetLogger sets the package-level Logger used by the framework. Pass nil to revert to the silent nopLogger.

SetLogger is safe to call concurrently with log calls; callers that want to mutate the logger without races should hold their own coordination.

Example

ExampleSetLogger demonstrates setting the package-level global logger. After this call, all code using log.GetLogger will receive the new logger. Pass nil to revert to the silent nopLogger.

package main

import (
	"context"

	"github.com/tx7do/go-wind/log"
)

func main() {
	// Use a custom Logger implementation or one from go-wind-plugins.
	// Here we demonstrate the global logger lifecycle.
	log.SetLogger(nil) // revert to nopLogger
	defer log.SetLogger(nil)

	logger := log.GetLogger()
	logger.Info(context.Background(), "hello from global logger")
}

Types

type Level

type Level int

Level represents a log severity level. Higher values are more severe. It is used as an argument to Logger.Enabled to check whether a given level would be emitted by the underlying backend.

const (
	LevelDebug Level = iota
	LevelInfo
	LevelWarn
	LevelError
)

Standard log levels ordered from least to most severe.

func (Level) String

func (l Level) String() string

String returns the human-readable name of the level, e.g. "DEBUG".

type Logger

type Logger interface {
	// Debug logs a message at DEBUG level with optional structured key/value
	// pairs passed as args (alternating keys and values).
	Debug(ctx context.Context, msg string, args ...any)
	// Info logs a message at INFO level.
	Info(ctx context.Context, msg string, args ...any)
	// Warn logs a message at WARN level.
	Warn(ctx context.Context, msg string, args ...any)
	// Error logs a message at ERROR level.
	Error(ctx context.Context, msg string, args ...any)

	// Enabled reports whether the logger emits records at the given [Level].
	// Expensive argument construction can be guarded by this check:
	//
	//		if logger.Enabled(log.LevelDebug) {
	//		    logger.Debug(ctx, "detail", computeExpensiveData())
	//		}
	Enabled(level Level) bool

	// With returns a new Logger instance with the given key-value pairs
	// attached. This is typically used to distinguish modules, e.g.,
	// logger.With("module", "registry").
	With(args ...any) Logger
}

Logger is the minimal logging interface used throughout the framework.

It is deliberately small (4 log methods + Enabled + With) so that any project can adapt its own backend (the stdlib *slog.Logger, zap, zerolog, kratos log, …) with a few lines of glue code and inject it via SetLogger.

The first argument is always a context, so backends that support it can extract trace ids / request-scoped attributes. Callers that have no context at hand may pass nil.

Example

ExampleLogger demonstrates the Logger interface contract. Callers implement this interface to bridge their own logging backend.

package main

import (
	"context"
	"fmt"

	"github.com/tx7do/go-wind/log"
)

func main() {
	// A minimal custom logger implementation:
	var l log.Logger = myLogger{}

	// All methods take context as the first argument for trace propagation.
	l.Debug(context.Background(), "debug message", "key", "value")
	l.Info(context.Background(), "info message")
	l.Warn(context.Background(), "warn message")
	l.Error(context.Background(), "error message")

	// Enabled lets callers guard expensive argument construction.
	if l.Enabled(log.LevelDebug) {
		l.Debug(context.Background(), "expensive", computeData())
	}

	// With returns a new logger with attached key-value pairs.
	child := l.With("module", "example")
	child.Info(context.Background(), "child logger")
}

func computeData() any { return nil }

// myLogger is a minimal Logger implementation for demonstration purposes.
type myLogger struct{}

func (myLogger) Debug(context.Context, string, ...any) { fmt.Println("DEBUG") }
func (myLogger) Info(context.Context, string, ...any)  { fmt.Println("INFO") }
func (myLogger) Warn(context.Context, string, ...any)  { fmt.Println("WARN") }
func (myLogger) Error(context.Context, string, ...any) { fmt.Println("ERROR") }
func (myLogger) Enabled(log.Level) bool                { return true }
func (l myLogger) With(args ...any) log.Logger         { return l }
Output:
DEBUG
INFO
WARN
ERROR
DEBUG
INFO

func GetLogger

func GetLogger() Logger

GetLogger returns the currently active package-level Logger. It is the entry point submodules use to obtain the shared logger without taking a direct dependency on any logging framework.

Jump to

Keyboard shortcuts

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