slogger

package module
v1.3.1 Latest Latest
Warning

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

Go to latest
Published: Sep 10, 2023 License: MIT Imports: 5 Imported by: 0

README

Build Status Coverage Report Go Reference

slogger

Package slogger implements supporting functionality related to the log/slog package.

For more details, you may review the package documentation here.

Usage

package main

import (
	"context"

	"github.com/azazeal/slogger"
)

func main() {
	// grab a reference to a slog.Logger configured by the $LOG_LEVEL & $LOG_FORMAT environment
	// variables.
	logger := slogger.FromEnv()

	// store that reference to a top-level Context that propagates throughout the program
	ctx := slogger.NewContext(context.Background(), logger)

	// pass the Context, and therefore the slog.Logger, around; log as needed
	doSomething(ctx)
	doSomethingElse(ctx)
}

func doSomething(ctx context.Context) {
	logger := slogger.FromContext(ctx)

	logger.Warn("did something")
}

func doSomethingElse(ctx context.Context) {
	logger := slogger.FromContext(ctx)

	logger.Info("did something else")
}

Documentation

Overview

Package slogger implements functionality around the slog package.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func FromContext

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

FromContext returns the slog.Logger reference the provided context.Context carries. In case the provided context.Context carries no slog.Logger reference, calling FromContext is a passthrough call to slog.Default.

func FromEnv

func FromEnv() *slog.Logger

FromEnv returns a reference to a slog.Logger that'll write records to os.Stderr with verbosity and format configured by the LOG_LEVEL & LOG_FORMAT environment variables, respectively. It's shorthand for calling FromEnvWithWriter with os.Stderr as the argument.

func FromEnvWithWriter added in v1.1.0

func FromEnvWithWriter(w io.Writer) *slog.Logger

FromEnvWithWriter returns a reference to a slog.Logger that'll write records to the provided io.Writer with verbosity and format configured by the LOG_LEVEL & LOG_FORMAT environment variables, respectively.

func NewContext

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

NewContext derives a context.Context from ctx that carries the provided slog.Logger reference. Callers may retrieve the reference to the provided slog.Logger by calling FromContext on the returned context.Context.

Types

This section is empty.

Jump to

Keyboard shortcuts

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