ctxlog

package module
v0.0.0-...-127b745 Latest Latest
Warning

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

Go to latest
Published: Aug 27, 2024 License: MIT Imports: 1 Imported by: 1

README

ctxlog

Go Reference

An agnostic, context-based wrapper for loggers. Adapted from the package of the same name by Zikaeroh.

By default, ctxlog comes with support for the following out of the box:

Using

zap

import (
    "context"

    "github.com/holedaemon/ctxlog"
    "github.com/holedaemon/ctxlog/ctxzap"
)

func main() {
    logger := ctxzap.New(false, nil)
    ctx := ctxlog.WithLogger(context.Background(), logger)

    ctxlog.Info(ctx, "hello", zap.String("cool", "argument"))
}

log/slog

import (
    "context"

    "github.com/holedaemon/ctxlog"
    "github.com/holedaemon/ctxlog/ctxslog"
)

func main() {
    logger := ctxslog.New(false, nil)
    ctx := ctxlog.WithLogger(context.Background(), logger)

    ctxlog.Info(ctx, "hello", "cool", "arg")
    ctxlog.Info(ctx, "hello", slog.String("cool", "arg"))
}

Extending

If you want to extend ctxlog to support a different logger, ctxlog exposes the Logger interface, which can be implemented.

type Logger interface {
    Debug(msg string, fields ...any)
    Info(msg string, fields ...any)
    Warn(msg string, fields ...any)
    Error(msg string, fields ...any)
    Fatal(msg string, fields ...any)
}

LICENSE

LICENSE

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Debug

func Debug(ctx context.Context, msg string, fields ...any)

Debug logs at the debug level.

func Error

func Error(ctx context.Context, msg string, fields ...any)

Error logs at the error level.

func Fatal

func Fatal(ctx context.Context, msg string, fields ...any)

Fatal logs at the fatal level.

func Info

func Info(ctx context.Context, msg string, fields ...any)

Info logs at the info level.

func Warn

func Warn(ctx context.Context, msg string, fields ...any)

Warn logs at the warn level.

func WithLogger

func WithLogger(ctx context.Context, logger Logger) context.Context

WithLogger adds a logger to the given context.

Types

type Logger

type Logger interface {
	Debug(msg string, fields ...any)
	Info(msg string, fields ...any)
	Warn(msg string, fields ...any)
	Error(msg string, fields ...any)
	Fatal(msg string, fields ...any)
}

Logger represents a logger that is used by ctxlog.

func FromContext

func FromContext(ctx context.Context) Logger

FromContext retrieves a logger from the given context. If the context does not contain a logger, a nop logger is returned.

Directories

Path Synopsis
Package ctxexpslog wraps slog's Logger type for use in the ctxlog package.
Package ctxexpslog wraps slog's Logger type for use in the ctxlog package.
Package ctxslog wraps slog's Logger type for use in the ctxlog package.
Package ctxslog wraps slog's Logger type for use in the ctxlog package.
Package ctxzap wraps zap's Logger type for use in the ctxlog package.
Package ctxzap wraps zap's Logger type for use in the ctxlog package.
internal

Jump to

Keyboard shortcuts

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