contextslogger

package module
v1.1.2 Latest Latest
Warning

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

Go to latest
Published: Nov 21, 2023 License: MIT Imports: 2 Imported by: 1

README

contextslogger

import "github.com/induzo/gocom/contextslogger"

contextslogger is a package that provides a context key for a logger.

Index

func FromContext

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

FromContext returns the Logger stored in ctx by NewContext, or the default Logger if there is none.

Example

package main

import (
	"context"
	"io"
	"log/slog"

	"github.com/induzo/gocom/contextslogger"
)

func main() {
	textHandler := slog.NewTextHandler(io.Discard, nil)
	logger := slog.New(textHandler)

	ctxWithLogger := contextslogger.NewContext(context.Background(), logger)

	retrievedLogger := contextslogger.FromContext(ctxWithLogger)

	_ = retrievedLogger
}

func NewContext

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

NewContext returns a context that contains the given Logger. Use FromContext to retrieve the Logger.

Example

package main

import (
	"context"
	"io"
	"log/slog"

	"github.com/induzo/gocom/contextslogger"
)

func main() {
	textHandler := slog.NewTextHandler(io.Discard, nil)
	logger := slog.New(textHandler)

	_ = contextslogger.NewContext(context.Background(), logger)
}

Generated by gomarkdoc

Documentation

Overview

contextslogger is a package that provides a context key for a logger.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func FromContext

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

FromContext returns the Logger stored in ctx by NewContext, or the default Logger if there is none.

Example
package main

import (
	"context"
	"io"
	"log/slog"

	"github.com/induzo/gocom/contextslogger"
)

func main() {
	textHandler := slog.NewTextHandler(io.Discard, nil)
	logger := slog.New(textHandler)

	ctxWithLogger := contextslogger.NewContext(context.Background(), logger)

	retrievedLogger := contextslogger.FromContext(ctxWithLogger)

	_ = retrievedLogger
}
Output:

func NewContext

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

NewContext returns a context that contains the given Logger. Use FromContext to retrieve the Logger.

Example
package main

import (
	"context"
	"io"
	"log/slog"

	"github.com/induzo/gocom/contextslogger"
)

func main() {
	textHandler := slog.NewTextHandler(io.Discard, nil)
	logger := slog.New(textHandler)

	_ = contextslogger.NewContext(context.Background(), logger)
}
Output:

Types

This section is empty.

Jump to

Keyboard shortcuts

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