log

package
v0.0.64 Latest Latest
Warning

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

Go to latest
Published: Jul 11, 2023 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(w io.Writer, maxMsgs int) func(ctx context.Context) *slog.Logger

New returns a function that returns an slog.Logger The logger is backed by a 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(os.Stdout, 1000)

	hey := func(ctx context.Context) {
		logger := l(ctx)

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

	hey(context.Background())

	// 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