slox

package module
v0.0.1 Latest Latest
Warning

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

Go to latest
Published: Sep 4, 2023 License: GPL-3.0 Imports: 2 Imported by: 2

README

slox

push GitHub release (latest SemVer) codecov CodeFactor GitHub go.mod Go version Go Reference

A simple context wrapper around builtin Go slog library.

Usage

package main

import (
	"context"
	"log/slog"

	"github.com/Vilsol/slox"
)

func main() {
	ctx := context.Background()
	ctx = slox.With(ctx, "foo", "bar")
	ctx = slox.WithGroup(ctx, "my-group")
	ctx = slox.With(ctx, slog.Int("one", 1), "hello", "world")
	slox.Info(ctx, "message")
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Debug

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

Debug logs at slog.LevelDebug.

Internally calls slog.Logger.DebugContext

func Enabled

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

Enabled reports whether context.Context emits log records at the given level.

Internally calls slog.Logger.Enabled

func Error

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

Error logs at slog.LevelError.

Internally calls slog.Logger.ErrorContext

func From

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

From returns the logger from the context

If the context does not contain a logger, a default logger is returned

func Info

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

Info logs at slog.LevelInfo.

Internally calls slog.Logger.InfoContext

func Into

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

Into wraps the provided logger in a context

func Log

func Log(ctx context.Context, level slog.Level, msg string, args ...any)

Log emits a log record with the current time and the given level and message. The Record's Attrs consist of the Logger's attributes followed by the Attrs specified by args.

The attribute arguments are processed as follows:

  • If an argument is an Attr, it is used as is.
  • If an argument is a string and this is not the last argument, the following argument is treated as the value and the two are combined into an Attr.
  • Otherwise, the argument is treated as a value with key "!BADKEY".

Internally calls slog.Logger.Log

func LogAttrs

func LogAttrs(ctx context.Context, level slog.Level, msg string, attrs ...slog.Attr)

LogAttrs is a more efficient version of slog.Logger.Log that accepts only Attrs.

Internally calls slog.Logger.LogAttrs

func Warn

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

Warn logs at slog.LevelWarn.

Internally calls slog.Logger.WarnContext

func With

func With(ctx context.Context, args ...any) context.Context

With returns a context.Context that includes the given attributes in each output operation. Arguments are converted to attributes as if by slog.Logger.Log.

Internally calls slog.Logger.With

func WithGroup

func WithGroup(ctx context.Context, name string) context.Context

WithGroup returns a context.Context that starts a group, if name is non-empty. The keys of all attributes added to the Logger will be qualified by the given name. (How that qualification happens depends on the slog.Handler.WithGroup method of the Logger's Handler.)

If name is empty, WithGroup returns the receiver.

Internally calls slog.Logger.WithGroup

Types

This section is empty.

Jump to

Keyboard shortcuts

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