logem

package module
v0.0.3 Latest Latest
Warning

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

Go to latest
Published: Feb 27, 2023 License: Apache-2.0 Imports: 8 Imported by: 0

README

Logem

Go Doc License

Logem, short for "Let's Log em!", is an opinionated handler wrapper for slog.

It combines a LevelHandler and a TraceHandler in a single, simple to use handler.

Disclaimer

This package is not suited for production use right now. It is heavily being worked on.

However feel free to inspire yourself from it or fork it.

Installation

go get -u github.com/mgjules/logem

Usage

package main

import (
  "context"
  "os"

  "github.com/mgjules/logem"
  "golang.org/x/exp/slog"
)

func main() {
  // Init OTEL trace provider.
  // initTraceProvider()

  // Create logger using logem.Handler.
  logger := slog.New(
    logem.NewHandler(
      slog.NewTextHandler(os.Stdout),
      logem.WithMinLevel(slog.LevelInfo),
      logem.WithStackTrace(true),
      logem.WithTraceID(true),
      logem.WithSpanID(true),
    ),
  )
  slog.SetDefault(logger)

  // Use logger to log messages, etc.
  // Please ensure that the context being passed has proper trace information.
  ctx := context.TODO()
  logger.WithContext(ctx).Info("hello", "count", 3)
}

Stability

This project follows SemVer strictly and is not yet v1.

Breaking changes might be introduced until v1 is released.

This project follows the Go Release Policy. Each major version of Go is supported until there are two newer major releases.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Handler

type Handler struct {
	// contains filtered or unexported fields
}

Handler wraps an slog.Handler to add trace support.

func NewHandler

func NewHandler(h slog.Handler, opts ...Option) *Handler

NewHandler returns a TraceHandler.

func (*Handler) Enabled

func (h *Handler) Enabled(ctx context.Context, level slog.Level) bool

Enabled is a simple wrapper method around h.handler.Enabled.

func (*Handler) Handle

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

Handle is a simple wrapper method around h.handler.Handle.

func (*Handler) Handler

func (h *Handler) Handler() slog.Handler

Handler returns the Handler wrapped by h.

func (*Handler) WithAttrs

func (h *Handler) WithAttrs(attrs []slog.Attr) slog.Handler

WithAttrs is a simple wrapper method around h.handler.WithAttrs.

func (*Handler) WithGroup

func (h *Handler) WithGroup(name string) slog.Handler

WithGroup is a simple wrapper method around h.handler.WithGroup.

type Option

type Option func(h *Handler)

Option applies optional configuration for a Handler.

func WithMinLevel

func WithMinLevel(level slog.Leveler) Option

WithMinLevel sets the minimum level on which log messages are handled.

func WithSpanID

func WithSpanID(on bool) Option

WithSpanID adds the Span ID to log messages.

func WithStackTrace

func WithStackTrace(on bool) Option

WithStackTrace adds a stack trace to traced log messages.

func WithTraceID

func WithTraceID(on bool) Option

WithTraceID adds the Trace ID to log messages.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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