logger

package module
v0.21.1 Latest Latest
Warning

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

Go to latest
Published: Jun 27, 2026 License: MIT Imports: 6 Imported by: 0

Documentation

Overview

Package logger provides structured logging (log/slog) with resources in unique.

Bootstrap

Blank-import the use subpackage at the app composition root (not logger itself):

import _ "github.com/omcrgnt/logger/use"

logger.Info(ctx, "started", "port", 8080)

logger/use registers DefaultLog and DefaultStdout via unique.MustAddReplaceable. Without logger/use, package funcs are no-op until the logger resource is wired (Inject sets active).

User override

Build and register user resources with unique.Add (replaces system defaults of the same type):

logBuilt, err := cfg.Logger.Build()
outBuilt, err := cfg.LogFile.Build()
unique.Global().Add(outBuilt)
unique.Global().Add(logBuilt)

Types Config, OutputStdoutConfig, and OutputFileConfig implement Build for override.

Usage

Package funcs (Info, Debug, …) and Default delegate to the logger wired by Inject.

For DI, declare the port in Deps:

func (c *Controller) Deps() []any {
    return []any{(*logger.Logger)(nil)}
}

Wiring

Concrete logger implements Logger; Deps returns (*Output)(nil). DefaultLog and DefaultStdout are for logger/use system registration.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Debug

func Debug(ctx context.Context, msg string, args ...any)

Debug logs at debug level via the active process logger.

func DefaultLog

func DefaultLog() any

DefaultLog returns the system Logger resource for logger/use registration.

func DefaultStdout added in v0.1.1

func DefaultStdout() any

DefaultStdout returns the system Output resource for logger/use registration.

func Error

func Error(ctx context.Context, msg string, args ...any)

Error logs at error level via the active process logger.

func Info

func Info(ctx context.Context, msg string, args ...any)

Info logs at info level via the active process logger.

func Warn

func Warn(ctx context.Context, msg string, args ...any)

Warn logs at warn level via the active process logger.

Types

type Config

type Config struct {
	Level  *loggerv1.Level  `ecfg:"LEVEL"`
	Format *loggerv1.Format `ecfg:"FORMAT"`
}

Config configures the process logger (level, format). ecfg fills and protovalidates Level and Format before Build is called.

func (Config) Build

func (c Config) Build() (any, error)

Build returns a Logger resource for unique.Add.

type Logger

type Logger interface {
	Debug(ctx context.Context, msg string, args ...any)
	Info(ctx context.Context, msg string, args ...any)
	Warn(ctx context.Context, msg string, args ...any)
	Error(ctx context.Context, msg string, args ...any)
}

Logger is the logging API.

func Default

func Default() Logger

Default returns the process logger set by Inject, or a no-op logger before Inject.

type Output

type Output interface {
	io.Writer
	// contains filtered or unexported methods
}

Output is a sealed log sink port for SDI.

type OutputFileConfig

type OutputFileConfig struct {
	Path string `ecfg:"PATH"`
}

OutputFileConfig opens a file log sink (user resource via Build).

func (OutputFileConfig) Build

func (c OutputFileConfig) Build() (any, error)

Build returns an Output writing to Path.

type OutputStdoutConfig added in v0.1.1

type OutputStdoutConfig struct{}

OutputStdoutConfig registers stdout as the logger Output (user resource via Build).

func (OutputStdoutConfig) Build added in v0.1.1

func (c OutputStdoutConfig) Build() (any, error)

Build returns an Output writing to os.Stdout.

Directories

Path Synopsis
Package use registers logger system defaults in unique.Global.
Package use registers logger system defaults in unique.Global.

Jump to

Keyboard shortcuts

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