log

package
v0.1.12 Latest Latest
Warning

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

Go to latest
Published: Oct 21, 2024 License: MIT Imports: 9 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 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