fastlog

package
v1.2.1 Latest Latest
Warning

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

Go to latest
Published: May 14, 2026 License: GPL-3.0 Imports: 8 Imported by: 0

README

fastlog

Import path: github.com/InsideGallery/core/fastlog

fastlog builds and installs structured log/slog handlers from environment-backed configuration. It fans out to registered output handlers, applies built-in middleware, and can replace slog.Default().

Main APIs

  • Config describes logging outputs, level, and middleware toggles.
  • GetConfigFromEnv() reads LOG_* environment variables.
  • (*Config).GetHandler(m ...slogmulti.Middleware) builds a composite slog.Handler.
  • SetupDefaultLogger(cfg *Config, m ...slogmulti.Middleware) installs the handler as slog.Default().

Usage

package example

import (
	"log/slog"

	_ "github.com/InsideGallery/core/fastlog/all"

	"github.com/InsideGallery/core/fastlog"
	"github.com/InsideGallery/core/fastlog/middlewares"
)

func configureLogging() error {
	cfg, err := fastlog.GetConfigFromEnv()
	if err != nil {
		return err
	}

	if err := fastlog.SetupDefaultLogger(cfg, middlewares.NewGDPRMiddleware()); err != nil {
		return err
	}

	slog.Info("logging configured")

	return nil
}

Configuration

GetConfigFromEnv uses the LOG prefix:

  • LOG_OUTPUTS: comma-separated kind:format values, default stderr:json.
  • LOG_LEVEL: parsed as a slog.Level, default INFO.
  • LOG_CALLER: adds a caller attribute when true, default true.
  • LOG_ERROR_FORMATTING: converts error attributes to structured groups when true, default false.

Valid formats are json and text; unknown formats fall back to JSON. Malformed output entries are skipped. Unknown handlers are collected as errors, and if no handler can be built the package falls back to the registered nop handler.

The base package imports only the nop fallback directly. Import fastlog/all or the specific handler packages before selecting stderr, datadog, or otel through LOG_OUTPUTS.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func SetupDefaultLogger added in v1.1.0

func SetupDefaultLogger(cfg *Config, m ...slogmulti.Middleware) error

SetupDefaultLogger initializes slog.Default() from an explicit configuration. Import handler packages (e.g. stderr, otel, datadog) via blank imports to register them.

Types

type Config

type Config struct {
	Outputs         []string   `env:"_OUTPUTS" envDefault:"stderr:json"`
	Level           slog.Level `env:"_LEVEL" envDefault:"INFO"`
	Caller          bool       `env:"_CALLER" envDefault:"true"`
	ErrorFormatting bool       `env:"_ERROR_FORMATTING" envDefault:"false"`
}

Config holds logging configuration parsed from environment variables.

func GetConfigFromEnv

func GetConfigFromEnv() (*Config, error)

GetConfigFromEnv reads logging configuration from environment variables.

func (*Config) GetHandler

func (c *Config) GetHandler(m ...slogmulti.Middleware) (slog.Handler, error)

GetHandler builds a composite slog.Handler from the configured outputs and middlewares.

Directories

Path Synopsis
Package all imports every in-tree fastlog handler so each handler registers with the default fastlog handler registry through its init hook.
Package all imports every in-tree fastlog handler so each handler registers with the default fastlog handler registry through its init hook.
nop

Jump to

Keyboard shortcuts

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