log

package
v0.10.0 Latest Latest
Warning

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

Go to latest
Published: Jan 25, 2026 License: MIT Imports: 10 Imported by: 0

Documentation

Index

Constants

View Source
const (
	DefaultLogLevel = slog.LevelInfo
	DefaultFormat   = FormatPretty
)

Variables

View Source
var (
	DefaultWriter            = os.Stdout
	DefaultTimeFormat        = time.StampMilli
	DefaultPrettyReplaceAttr = func(groups []string, a slog.Attr) slog.Attr {
		if a.Key == "pkg" {
			return tint.Attr(243, a)
		}
		if a.Key == "status" {
			code := a.Value.Int64()
			switch {
			case code >= 200 && code < 300:
				return tint.Attr(2, a)
			case code >= 400 && code < 500:
				return tint.Attr(3, a)
			case code >= 500 && code < 600:
				return tint.Attr(1, a)
			}
		}
		return a
	}
)

Functions

func Configure added in v0.10.0

func Configure(opts Options) error

Configure initializes and installs the global logger configuration.

It sets the default slog logger according to the provided Options, allowing to control:

  • output destination (stdout, file, custom writer);
  • log format (plain text, JSON, or pretty colored output);
  • attribute transformation via ReplaceAttr.

Behavior by format:

  • FormatText Uses slog.TextHandler and writes human-readable logs. Intended for simple local usage or file-based text logs.

  • FormatJSON Uses slog.JSONHandler and produces structured JSON logs. Recommended for production environments and log aggregation systems.

  • FormatPretty Uses tint.Handler to produce colorized, developer-friendly output. Colors are enabled automatically only when the writer is a terminal; when the output is redirected (e.g. to a file), colors are disabled.

ReplaceAttr:

If ReplaceAttr is non-nil, it is applied to every log attribute
before it is written. This can be used to:
  - mask sensitive data;
  - rename or drop attributes;
  - customize time, level, or message formatting.

If ReplaceAttr is nil, attributes are written as-is.

Global effects:

Configure replaces the global slog default logger via slog.SetDefault.
All loggers created via log.New(...) will immediately start using
the new configuration.

Thread-safety:

Configure is expected to be called during application startup.
Reconfiguring the logger at runtime is supported by slog but may
lead to mixed log formats in concurrent systems and is discouraged.

Example:

log.Configure(log.Options{
    Writer: os.Stdout,
    Format: log.FormatPretty,
})

func Level added in v0.4.0

func Level() slog.Level

func New

func New(pkg string) *slog.Logger

func NewProxyHandler

func NewProxyHandler() *proxyHandler

func SetLevel added in v0.4.0

func SetLevel(level slog.Level)

func SetLevelStr added in v0.4.0

func SetLevelStr(level string) error

Types

type Options added in v0.10.0

type Options struct {
	Writer      io.Writer
	Format      OutputFormat
	ReplaceAttr func(groups []string, a slog.Attr) slog.Attr
}

type OutputFormat added in v0.10.0

type OutputFormat string
const (
	// FormatText writes plain text logs, human-readable, suitable for local or file output.
	FormatText OutputFormat = "text"

	// FormatJSON writes structured JSON logs, recommended for production and log aggregation.
	FormatJSON OutputFormat = "json"

	// FormatPretty writes colorized, developer-friendly logs, automatically disables
	// color if output is not a terminal.
	FormatPretty OutputFormat = "pretty"
)

Jump to

Keyboard shortcuts

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