logging

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Jul 2, 2026 License: Apache-2.0 Imports: 7 Imported by: 0

Documentation

Overview

Package logging provides structured logging with built-in redaction for sensitive data.

It uses Go's standard library log/slog package with JSON output format. The redacting handler intercepts all log records and replaces sensitive values (API keys, tokens, passwords, private keys, high-entropy strings) with "[REDACTED]".

Usage:

logger := logging.NewLogger(slog.LevelInfo, os.Stdout)
logger.Info("server started", "addr", ":8080")
logger.Info("request", "api_key", "sk-test-key-1234567890abcdef")
// The API key above will be redacted in output.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewLogger

func NewLogger(level slog.Level, output io.Writer) *slog.Logger

NewLogger creates a new slog.Logger with JSON output format and redaction enabled.

The logger writes JSON-formatted log records to the given io.Writer. Log records at or above the specified level are emitted. All sensitive values (API keys, tokens, passwords, etc.) are automatically replaced with "[REDACTED]" in the output.

Example:

logger := logging.NewLogger(slog.LevelInfo, os.Stdout)
logger.Info("server starting", "port", 8080)

func Redact

func Redact(input string) string

Redact replaces all sensitive values found in the input string with "[REDACTED]". It checks the entire string and replaces any substring matching a sensitive pattern.

This is the core redaction function used by the logging handler.

Types

type RedactingHandler

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

RedactingHandler is a slog.Handler wrapper that redacts sensitive values from log records before delegating to the underlying handler.

func NewRedactingHandler

func NewRedactingHandler(inner slog.Handler) *RedactingHandler

NewRedactingHandler creates a new RedactingHandler that wraps the given handler.

func (*RedactingHandler) Enabled

func (h *RedactingHandler) Enabled(ctx context.Context, level slog.Level) bool

Enabled reports whether the handler handles records at the given level. It delegates to the underlying handler.

func (*RedactingHandler) Handle

func (h *RedactingHandler) Handle(ctx context.Context, record slog.Record) error

Handle redacts sensitive values in the log record and delegates to the underlying handler.

func (*RedactingHandler) WithAttrs

func (h *RedactingHandler) WithAttrs(attrs []slog.Attr) slog.Handler

WithAttrs returns a new handler with the given attributes pre-attached. The attributes are redacted before being stored.

func (*RedactingHandler) WithGroup

func (h *RedactingHandler) WithGroup(name string) slog.Handler

WithGroup returns a new handler with the given group name.

Jump to

Keyboard shortcuts

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