logger

package
v0.5.0 Latest Latest
Warning

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

Go to latest
Published: Mar 30, 2026 License: EUPL-1.2 Imports: 10 Imported by: 0

Documentation

Overview

Package logger provides infrastructure adapters for structured logging.

This package implements the Logger port from the domain layer, providing multiple logging backends with secret masking:

  • ConsoleLogger: Human-readable colored output for CLI environments
  • JSONLogger: Structured JSON logging for production and log aggregation
  • MultiLogger: Broadcast logs to multiple loggers simultaneously

Architecture

  • Domain defines: Logger port interface with Debug/Info/Warn/Error/WithContext methods
  • Infrastructure provides: Three concrete logger implementations with secret masking
  • Application injects: Logger via dependency injection

Example Usage

ConsoleLogger:

logger := logger.NewConsoleLogger(os.Stdout, "INFO")
logger.Info("workflow started", "id", "wf-123", "name", "deploy")
// Output: [INFO] workflow started id=wf-123 name=deploy

JSONLogger:

logger := logger.NewJSONLogger(os.Stdout, "DEBUG")
logger.Error("command failed", "exit_code", 1, "stderr", "not found")
// Output: {"level":"ERROR","msg":"command failed","exit_code":1,"stderr":"not found"}

MultiLogger:

console := logger.NewConsoleLogger(os.Stdout, "INFO")
jsonFile := logger.NewJSONLogger(file, "DEBUG")
multi := logger.NewMultiLogger(console, jsonFile)
multi.Info("event", "key", "value") // Logged to both outputs

Secret Masking

  • Automatically masks values for keys matching: SECRET_*, API_KEY*, PASSWORD*, *_TOKEN
  • Masked values appear as "***MASKED***" in logs
  • Prevents accidental credential leaks in log files and console output
  • Applied by all logger implementations (ConsoleLogger, JSONLogger, MultiLogger)

Component: C056 Infrastructure Package Documentation Layer: Infrastructure

Index

Constants

This section is empty.

Variables

View Source
var DefaultSecretPatterns = []string{
	"SECRET_",
	"API_KEY",
	"PASSWORD",
	"TOKEN",
}

DefaultSecretPatterns contains the default patterns to detect secrets.

Functions

This section is empty.

Types

type ConsoleLogger

type ConsoleLogger struct {
	// contains filtered or unexported fields
}

ConsoleLogger implements ports.Logger with human-readable console output.

func NewConsoleLogger

func NewConsoleLogger(w io.Writer, level Level, useColor bool) *ConsoleLogger

NewConsoleLogger creates a console logger.

func (*ConsoleLogger) Debug

func (l *ConsoleLogger) Debug(msg string, fields ...any)

func (*ConsoleLogger) Error

func (l *ConsoleLogger) Error(msg string, fields ...any)

func (*ConsoleLogger) Info

func (l *ConsoleLogger) Info(msg string, fields ...any)

func (*ConsoleLogger) Warn

func (l *ConsoleLogger) Warn(msg string, fields ...any)

func (*ConsoleLogger) WithContext

func (l *ConsoleLogger) WithContext(ctx map[string]any) ports.Logger

type JSONLogger

type JSONLogger struct {
	// contains filtered or unexported fields
}

JSONLogger implements ports.Logger with JSON output using zap.

func NewJSONLogger

func NewJSONLogger(path string, level Level) (*JSONLogger, error)

NewJSONLogger creates a JSON logger that writes to a file.

func (*JSONLogger) Close

func (l *JSONLogger) Close() error

Close closes the log file.

func (*JSONLogger) Debug

func (l *JSONLogger) Debug(msg string, fields ...any)

func (*JSONLogger) Error

func (l *JSONLogger) Error(msg string, fields ...any)

func (*JSONLogger) Info

func (l *JSONLogger) Info(msg string, fields ...any)

func (*JSONLogger) Sync

func (l *JSONLogger) Sync() error

Sync flushes buffered logs.

func (*JSONLogger) Warn

func (l *JSONLogger) Warn(msg string, fields ...any)

func (*JSONLogger) WithContext

func (l *JSONLogger) WithContext(ctx map[string]any) ports.Logger

type Level

type Level int

Level represents log level.

const (
	LevelDebug Level = iota
	LevelInfo
	LevelWarn
	LevelError
)

func ParseLevel

func ParseLevel(s string) Level

ParseLevel parses a string into a Level.

func (Level) String

func (l Level) String() string

type MultiLogger

type MultiLogger struct {
	// contains filtered or unexported fields
}

MultiLogger delegates log calls to multiple loggers.

func NewMultiLogger

func NewMultiLogger(loggers ...ports.Logger) *MultiLogger

NewMultiLogger creates a logger that writes to all provided loggers.

func (*MultiLogger) Debug

func (m *MultiLogger) Debug(msg string, fields ...any)

func (*MultiLogger) Error

func (m *MultiLogger) Error(msg string, fields ...any)

func (*MultiLogger) Info

func (m *MultiLogger) Info(msg string, fields ...any)

func (*MultiLogger) Warn

func (m *MultiLogger) Warn(msg string, fields ...any)

func (*MultiLogger) WithContext

func (m *MultiLogger) WithContext(ctx map[string]any) ports.Logger

type NopLogger

type NopLogger struct{}

NopLogger is a no-op logger that discards all output.

func (NopLogger) Debug

func (NopLogger) Debug(msg string, fields ...any)

func (NopLogger) Error

func (NopLogger) Error(msg string, fields ...any)

func (NopLogger) Info

func (NopLogger) Info(msg string, fields ...any)

func (NopLogger) Warn

func (NopLogger) Warn(msg string, fields ...any)

func (NopLogger) WithContext

func (NopLogger) WithContext(ctx map[string]any) ports.Logger

type SecretMasker

type SecretMasker struct {
	// contains filtered or unexported fields
}

SecretMasker masks sensitive values in log fields.

func NewSecretMasker

func NewSecretMasker(additionalPatterns ...string) *SecretMasker

NewSecretMasker creates a masker with default patterns plus any additional ones.

func (*SecretMasker) IsSecretKey

func (m *SecretMasker) IsSecretKey(key string) bool

IsSecretKey checks if a key matches any secret pattern.

func (*SecretMasker) MaskFields

func (m *SecretMasker) MaskFields(fields []any) []any

MaskFields replaces values of secret keys with "***". Fields are expected as key-value pairs: key1, val1, key2, val2...

func (*SecretMasker) MaskText

func (m *SecretMasker) MaskText(text string, env map[string]string) string

MaskText replaces secret values in text output with "***".

Jump to

Keyboard shortcuts

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