log

package
v0.0.82 Latest Latest
Warning

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

Go to latest
Published: Sep 15, 2023 License: MIT Imports: 12 Imported by: 1

Documentation

Overview

Package log implements a simple logging handler.

Index

Examples

Constants

View Source
const (
	// ImmediateLevel is the severity which if a log event has, it is logged immediately without buffering.
	LevelImmediate = slog.Level(-6142973)
)

Variables

This section is empty.

Functions

func GetId

func GetId(ctx context.Context) string

GetId gets a logId either from the provided context or auto-generated.

func New

func New(ctx context.Context, w io.Writer, maxSize int) *slog.Logger

New returns an slog.Logger The logger is backed by an slog.Handler that stores log messages into a circular buffer. Those log messages are only flushed to the underlying io.Writer when a message with level >= slog.LevelError is logged. A unique logID is also added to the logs that acts as a correlation id of log events from diffrent components that neverthless are related.

Example
package main

import (
	"context"
	"errors"
	"os"

	"github.com/komuw/ong/log"
)

func main() {
	l := log.New(context.Background(), os.Stdout, 1000)

	l.Info("sending email", "email", "jane@example.com")
	l.Error("fail", "err", errors.New("sending email failed."), "email", "jane@example.com")

	// example output:
	//   {"time":"2023-02-03T11:26:47.460792396Z","level":"INFO","source":"main.go:17","msg":"sending email","email":"jane@example.com","logID":"DQTXGs3HM8Xgx3yt"}
	//   {"time":"2023-02-03T11:26:47.46080217Z","level":"ERROR","source":"main.go:18","msg":"fail","err":"sending email failed.","email":"jane@example.com","logID":"DQTXGs3HM8Xgx3yt"}
}
Output:

func TestHandler added in v0.0.74

func TestHandler(h slog.Handler, results func() []map[string]any) error

TestHandler tests a slog.Handler. If TestHandler finds any misbehaviors, it returns an error for each, combined into a single error with errors.Join.

TestHandler installs the given Handler in a slog.Logger and makes several calls to the Logger's output methods.

The results function is invoked after all such calls. It should return a slice of map[string]any, one for each call to a Logger output method. The keys and values of the map should correspond to the keys and values of the Handler's output. Each group in the output should be represented as its own nested map[string]any. The standard keys slog.TimeKey, slog.LevelKey and slog.MessageKey should be used.

If the Handler outputs JSON, then calling encoding/json.Unmarshal with a `map[string]any` will create the right data structure.

If a Handler intentionally drops an attribute that is checked by a test, then the results function should check for its absence and add it to the map it returns.

This func is copied from https://github.com/golang/go/blob/go1.21.0/src/testing/slogtest/slogtest.go#L51 The only change we make is to change the log method from `.Info()` to `.Error()` We should try and update this code with every release of Go. See issue; https://github.com/golang/go/issues/61706#issuecomment-1674413648

func WithID added in v0.0.38

func WithID(ctx context.Context, l *slog.Logger) *slog.Logger

WithID returns a slog.Logger based on l, that includes a logID from ctx. If ctx does not contain a logID, one will be auto-generated.

Types

This section is empty.

Jump to

Keyboard shortcuts

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