factory

package
v0.0.0-...-b788499 Latest Latest
Warning

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

Go to latest
Published: Jul 26, 2026 License: MIT Imports: 22 Imported by: 0

README

factory

import "github.com/altessa-s/go-atlas/observability/slog/factory"

Package factory provides a fluent builder for creating structured loggers from configuration. LoggerBuilder uses deferred error accumulation — errors from any step are collected and returned at Build() time.

Quick Start

logger, err := factory.New(cfg.Logger).
    WithEnableMasking().
    Build()

After Build, the builder retains its level variable and can be used for runtime level changes.

Methods

Constructor
Method Description
New(cfg) Creates a LoggerBuilder for the given logger config
Configuration
Method Description
WithPrefixKey(key) Sets the attribute key used for module prefix values
WithPrefixColors(colors) Defines ANSI colors for prefix attributes in colorized output
WithEnableMasking() Enables sensitive field masking using tags from config
WithAppName(name) Overrides the application name included in log metadata
WithAppVersion(version) Overrides the application version included in log metadata
WithServiceId(id) Sets the service ID included in log metadata
WithLevelVar(lv) Sets a custom dynamic log level variable
Terminal
Method Description
Build Assembles and returns the *slog.Logger
SetLevel(level) Changes the logging level at runtime after Build
GetLevel() Returns the current logging level

Documentation

Overview

Package factory provides a fluent builder for creating slog loggers from configuration. It integrates colorized, prefixed, and masking handlers to create structured loggers with consistent defaults.

logger, err := factory.New(cfg.Logger).
    WithEnableMasking().
    Build()

Index

Constants

View Source
const ModuleKey = "module"

ModuleKey is the default attribute key for module prefixes.

Variables

This section is empty.

Functions

func RegisterHandler

func RegisterHandler(format config.LogFormat, factory HandlerFactory)

RegisterHandler registers a custom HandlerFactory for the given log format. Safe for concurrent use.

Types

type HandlerFactory

type HandlerFactory func(w io.Writer, cfg *config.Logger, opts *slog.HandlerOptions) slog.Handler

HandlerFactory creates a slog.Handler for a specific log format. Register custom factories via RegisterHandler.

type LoggerBuilder

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

LoggerBuilder assembles a slog.Logger from configuration using a fluent API with deferred error accumulation.

After LoggerBuilder.Build, the builder retains a reference to the slog.LevelVar and can be used for runtime level changes via LoggerBuilder.SetLevel and LoggerBuilder.GetLevel.

func New

func New(cfg *config.Logger) *LoggerBuilder

New creates a new LoggerBuilder for the given logger config.

func (*LoggerBuilder) Build

func (b *LoggerBuilder) Build() (*slog.Logger, error)

Build assembles and returns the logger.

func (*LoggerBuilder) GetLevel

func (b *LoggerBuilder) GetLevel() slog.Level

GetLevel returns the current logging level.

func (*LoggerBuilder) SetLevel

func (b *LoggerBuilder) SetLevel(level slog.Level)

SetLevel changes the logging level at runtime.

func (*LoggerBuilder) WithAppName

func (b *LoggerBuilder) WithAppName(v string) *LoggerBuilder

WithAppName sets the application name to include in logs.

func (*LoggerBuilder) WithAppVersion

func (b *LoggerBuilder) WithAppVersion(v string) *LoggerBuilder

WithAppVersion sets the application version to include in logs.

func (*LoggerBuilder) WithEnableMasking

func (b *LoggerBuilder) WithEnableMasking() *LoggerBuilder

WithEnableMasking enables the advanced masking handler wrapper, which applies a curated default sensitive-field set (`password`, `token`, `secret`, common patterns like `*api_key*`, ...) on top of any tags declared in `config.Logger.SensitiveTags`.

The wrapper is also enabled automatically when `config.Logger.SensitiveTags` is non-empty, so this option is mainly useful for callers that want the default field set without listing tags themselves.

func (*LoggerBuilder) WithLevelVar

func (b *LoggerBuilder) WithLevelVar(v *slog.LevelVar) *LoggerBuilder

WithLevelVar sets the dynamic log level variable.

func (*LoggerBuilder) WithPrefixColors

func (b *LoggerBuilder) WithPrefixColors(v map[string][]int) *LoggerBuilder

WithPrefixColors defines the colors for prefix attributes.

func (*LoggerBuilder) WithPrefixKey

func (b *LoggerBuilder) WithPrefixKey(v string) *LoggerBuilder

WithPrefixKey sets the attribute key for prefix values.

func (*LoggerBuilder) WithServiceId

func (b *LoggerBuilder) WithServiceId(v string) *LoggerBuilder

WithServiceId sets the service ID to include in logs.

Jump to

Keyboard shortcuts

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