ctxslog

package
v0.0.5 Latest Latest
Warning

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

Go to latest
Published: Feb 8, 2025 License: MIT Imports: 4 Imported by: 4

README

Slog with FluentBit compatible handler.

slog is a wrapper around the log/slog package. It provides a way to create a logger and add it to the context, and a way to retrieve the logger from the context.

The format of the logger stems from the proposed format for inlogs v2 - https://go/inlogsV2-proposed-format

Using context for passing the logger

This pkg provides functionality to pass the logger using context. To do so,

  1. Create the logger and get a context for it
import (
  "context"
  ligoslog "golnkd.in/nimbus/nimbus-pkg/v2/slog"
)

func main() {
	log := ligoslog.New()
	ctx := ligoslog.NewContext(context.Background(), log)
  ...
}
  1. Fetch the logger from the context and log
import (
  "context"
  ligoslog "golnkd.in/nimbus/nimbus-pkg/v2/slog"
)

func something(ctx context.Context) {
  log := ligoslog.FromContext(ctx)
  log.Info("Hello Nimbus!)
}

Documentation

Overview

package slog is a wrapper around the log/slog package. It provides a way to create a logger and add it to the context, and a way to retrieve the logger from the context.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func FromContext

func FromContext(ctx context.Context) *slog.Logger

FromContext returns the logger from the given context. If no logger is found, it returns a no-op logger.

func NewContext

func NewContext(ctx context.Context, logger *slog.Logger) context.Context

NewContext returns a new context with the given logger by copying the provided context and adding the logger to it.

func NewCustomHandler

func NewCustomHandler(logLevel slog.Level) *slog.HandlerOptions

NewCustomHandler returns a custom handler which adds source, replaces source file path with just the file name and its penultimate directory.

Types

This section is empty.

Jump to

Keyboard shortcuts

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