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 ¶
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 ¶
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.
type Option ¶
type Option func(*config)
An Option is a function that modifies the default handler configuration.
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.
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.