logging

package
v0.0.0-...-a27e026 Latest Latest
Warning

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

Go to latest
Published: Dec 3, 2026 License: AGPL-3.0 Imports: 23 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Index

Constants

View Source
const (

	// LevelTrace is a custom TRACE log level.
	LevelTrace = slog.Level(-8)
	// LevelNotice is a custom NOTICE log level.
	LevelNotice = slog.Level(2) // between INFO (0) and WARN (4)
	// LevelFatal is a custom FATAL log level.
	LevelFatal = slog.Level(12)
	// LevelCritical is an alias of [LevelFatal].
	LevelCritical = LevelFatal
)

Variables

View Source
var LevelNames = map[slog.Leveler]string{
	LevelTrace:  "TRACE",
	LevelNotice: "NOTICE",
	LevelFatal:  "FATAL",
}

LevelNames contains a list of custom log level names.

View Source
var New = sync.OnceValue(func() *slog.Logger {

	cfg = &Config{
		Level:        "info",
		currentLevel: slog.LevelInfo,
	}
	if err := config.Load(configEnvPrefix, cfg); err != nil {
		panic(fmt.Errorf("load config: %w", err))
	}

	switch cfg.Level {
	case "trace":
		cfg.currentLevel = LevelTrace
	case "debug":
		cfg.currentLevel = slog.LevelDebug
	case "info":
		cfg.currentLevel = slog.LevelInfo
	case "warn":
		cfg.currentLevel = slog.LevelWarn
	case "error":
		cfg.currentLevel = slog.LevelError
	default:
		cfg.currentLevel = slog.LevelInfo
	}

	var handlers []slog.Handler

	switch {
	case cfg.Format == "text":
		slog.Info("Using specified format.", slog.String("format", cfg.Format))
		handlers = append(handlers,
			tint.NewTextHandler(os.Stderr, consoleOptions(cfg.currentLevel, os.Stderr.Fd())),
		)
	case config.DetectContainerRuntime() != config.RuntimeNone, cfg.Format == "console":
		slog.Info("Using container logging format.", slog.String("container", string(config.DetectContainerRuntime())))
		cfg.LogFile = ""
		instrumentedHandler := HandlerWithSpanContext(
			slog.NewJSONHandler(os.Stderr, containerConsoleOptions(cfg.currentLevel)),
		)
		handlers = append(handlers,
			instrumentedHandler,
		)
	default:
		slog.Info("Using text logging format.")
		handlers = append(handlers,
			tint.NewTextHandler(os.Stderr, consoleOptions(cfg.currentLevel, os.Stderr.Fd())),
		)
	}

	if cfg.LogFile != "" {
		if logFH, err := openLogFile(cfg.LogFile); err != nil {
			fmt.Fprintln(os.Stderr, "unable to open log file: %w", err)
		} else {
			handlers = append(handlers,
				slog.NewTextHandler(logFH, generateFileOpts(cfg.currentLevel)),
			)
		}
	}

	logger := slog.New(slogctx.NewHandler(slogmulti.Fanout(handlers...), &slogctx.HandlerOptions{

		Prependers: []slogctx.AttrExtractor{
			slogotel.ExtractTraceSpanID,
			slogctx.ExtractPrepended,
		},

		Appenders: []slogctx.AttrExtractor{
			slogctx.ExtractAppended,
		},
	}))
	slog.SetDefault(logger)

	logger.Info("Logger initialised.")

	return logger
})

New creates a new logger with the given options.

Functions

func GetLogLevel

func GetLogLevel() slog.Level

GetLogLevel returns the current default log level.

func HandlerWithSpanContext

func HandlerWithSpanContext(handler slog.Handler) *spanContextLogHandler

HandlerWithSpanContext adds attributes from the span context [START opentelemetry_instrumentation_spancontext_logger].

func StartProfiling

func StartProfiling(logger *slog.Logger, flags ProfileFlags) error

func StopProfiling

func StopProfiling(logger *slog.Logger, flags ProfileFlags) error

Types

type Config

type Config struct {
	// Level is the current default logging level.
	Level string `koanf:"level" validate:"omitempty,oneof=trace debug info warn error"`
	// Format overrides any auto-assigned format of the logs to the one specified.
	Format string `koanf:"format" validate:"omitempty,oneof=json console text"`
	// LogFile is a file to which logs will be written. Any existing file will be overwritten.
	LogFile string `koanf:"file" validate:"omitempty,file"`
	// contains filtered or unexported fields
}

type ProfileFlags

type ProfileFlags map[string]string

Jump to

Keyboard shortcuts

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