logx

package
v0.111.2 Latest Latest
Warning

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

Go to latest
Published: Oct 28, 2026 License: Apache-2.0 Imports: 7 Imported by: 1 module, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package logx is the fleet's structured logger: slog JSON on stdout, with the service, version and node on every line, the trace id when the call is inside a traced request, and credentials redacted before they are written.

logx.Install(logx.Options{Service: "go_example", Version: "1.2.3"})
slog.InfoContext(ctx, "charged", "account", id)   // {"service":"go_example", "trace_id":"…", …}
log.Printf("legacy line %d", n)                   // the same JSON shape, level INFO

Why one call is enough

slog.SetDefault, which Install calls, also redirects the standard library's log package to the new handler. Code written with log.Printf (this repository has dozens of such calls, and so do the services) therefore becomes structured without being edited. Those records have no context, so they carry the service fields but no trace id; use slog.InfoContext or middleware.LoggerFromContext where the trace matters.

What it redacts

  • an attribute whose key looks like a credential (token, secret, password, authorization, api_key, cookie, credential) and whose value is a string; numbers are left alone, so "tokens": 52 stays;
  • in any string value, including the message: "Bearer <token>", the user and password of a URL (scheme://user:pass@host), and token=, password=, secret= or api_key= in a query string or key=value text.

Redaction is a net under the careless log line, not permission to log secrets.

Adopting it

It is opt-in per service: server.New installs it when LOG_FORMAT=json (or the service passes server.WithStructuredLogs). The default output is unchanged, because moving every service to JSON at once would break anything that greps or alerts on the old text.

Index

Constants

View Source
const Redacted = "[redacted]"

Redacted replaces a redacted value.

Variables

This section is empty.

Functions

func Install

func Install(o Options) *slog.Logger

Install makes the logger the process default for slog and, because slog.SetDefault does so, for the standard library's log package too. It returns the logger.

func New

func New(o Options) *slog.Logger

New returns a logger using NewHandler.

func NewHandler

func NewHandler(o Options) slog.Handler

NewHandler returns the slog.Handler.

func RedactString

func RedactString(s string) string

RedactString removes the credentials a string carries: bearer tokens, the user and password of a URL, and token=, password=, secret= or api_key= values.

Types

type Options

type Options struct {
	// Service, Version and Node are added to every record when non-empty.
	Service string
	Version string
	Node    string
	// Level is the minimum level; the default is slog.LevelInfo.
	Level slog.Leveler
	// Output defaults to os.Stdout.
	Output io.Writer
	// AddSource adds the file and line of the call.
	AddSource bool
	// ContextAttrs adds attributes derived from each record's context. Do not
	// return one that the logger already carries (the request logger has
	// request_id): the JSON would then hold the key twice.
	ContextAttrs func(ctx context.Context) []slog.Attr
}

Options configures a logger.

Jump to

Keyboard shortcuts

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