log

package
v0.0.8 Latest Latest
Warning

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

Go to latest
Published: Jan 28, 2026 License: GPL-3.0 Imports: 6 Imported by: 0

Documentation

Overview

Example
package main

import (
	"context"
	"log/slog"

	"github.com/haleyrc/lib/log"
)

func main() {
	h := log.NewJSONHandler(
		log.FreezeTime(),
		log.Debug(),
	)
	logger := slog.New(h)

	logger.Debug("hello", slog.String("target", "world"))
	logger.Info("hello", slog.String("target", "world"))
	logger.Warn("hello", slog.String("target", "world"))
	logger.Error("hello", slog.String("target", "world"))

	ctx := log.SetRequestInfo(context.Background(), &log.RequestInfo{
		ID:        "46f7e63a-5c89-42e5-a488-9809dbf47760",
		IP:        "192.168.1.100",
		Method:    "GET",
		Path:      "/test",
		UserAgent: "GoTest 1.1",
	})
	logger.DebugContext(ctx, "hello", slog.String("target", "world"))
	logger.InfoContext(ctx, "hello", slog.String("target", "world"))
	logger.WarnContext(ctx, "hello", slog.String("target", "world"))
	logger.ErrorContext(ctx, "hello", slog.String("target", "world"))

}
Output:

{"time":"2024-10-03T12:01:32-05:00","level":"DEBUG","msg":"hello","target":"world"}
{"time":"2024-10-03T12:01:32-05:00","level":"INFO","msg":"hello","target":"world"}
{"time":"2024-10-03T12:01:32-05:00","level":"WARN","msg":"hello","target":"world"}
{"time":"2024-10-03T12:01:32-05:00","level":"ERROR","msg":"hello","target":"world"}
{"time":"2024-10-03T12:01:32-05:00","level":"DEBUG","msg":"hello","target":"world","httpRequest":{"id":"46f7e63a-5c89-42e5-a488-9809dbf47760","ip":"192.168.1.100","method":"GET","path":"/test","ua":"GoTest 1.1"}}
{"time":"2024-10-03T12:01:32-05:00","level":"INFO","msg":"hello","target":"world","httpRequest":{"id":"46f7e63a-5c89-42e5-a488-9809dbf47760","ip":"192.168.1.100","method":"GET","path":"/test","ua":"GoTest 1.1"}}
{"time":"2024-10-03T12:01:32-05:00","level":"WARN","msg":"hello","target":"world","httpRequest":{"id":"46f7e63a-5c89-42e5-a488-9809dbf47760","ip":"192.168.1.100","method":"GET","path":"/test","ua":"GoTest 1.1"}}
{"time":"2024-10-03T12:01:32-05:00","level":"ERROR","msg":"hello","target":"world","httpRequest":{"id":"46f7e63a-5c89-42e5-a488-9809dbf47760","ip":"192.168.1.100","method":"GET","path":"/test","ua":"GoTest 1.1"}}

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func ParseRequestInfo

func ParseRequestInfo(next http.Handler) http.Handler

ParseRequestInfo is a middleware that parses an incoming HTTP request and adds relevant fields to the request context. These fields are then added to any log lines emitted by one of the handler implementations in this package. This function also adds a unique identifier to the response for tracking across system boundaries.

func SetRequestInfo

func SetRequestInfo(ctx context.Context, ri *RequestInfo) context.Context

SetRequestInfo returns a copy of ctx with ri attached.

Types

type JSONHandler

type JSONHandler struct {
	slog.Handler
}

JSONHandler writes log lines to stdout as one-line-per-object JSON.

func NewJSONHandler

func NewJSONHandler(opts ...Option) JSONHandler

NewJSONHandler returns a slog.Handler that writes log lines to stdout as one-line-per-object JSON.

func (JSONHandler) Handle

func (h JSONHandler) Handle(ctx context.Context, r slog.Record) error

Handle partially implements slog.Handler and decorates log lines with HTTP request info if any exists on the provided context.

type Option

type Option func(*config)

An Option is a function that modifies the default handler configuration.

func Debug

func Debug() Option

Debug forces a handler to output debug lines.

func FreezeTime

func FreezeTime() Option

FreezeTime forces a handler to use a constant timestamp when emitting log lines. This is only useful for testing where deterministic output is required.

func Output

func Output(w io.Writer) Option

Output sets the io.Writer to write messages to.

type RequestInfo

type RequestInfo struct {
	// A unique identifier for the request.
	ID string

	// The IP address of the upstream client.
	IP string

	// The HTTP method.
	Method string

	// The request path.
	Path string

	// The user agent string of the upstream client.
	UserAgent string
}

RequestInfo stores information about an HTTP request.

Jump to

Keyboard shortcuts

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