logger

package module
v0.0.3 Latest Latest
Warning

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

Go to latest
Published: Nov 10, 2025 License: Apache-2.0 Imports: 5 Imported by: 11

README

SDC logger

sdc logo

Usage Example

This package exposes functions to help with logging using the logr.Logger interface, mainly regarding context-based passing of the logger instance.

Any logging implementation which implements logr.Logger can be used, and there are many implementations which provide the logr.Logger interface (zapr, logr, ...).

It also contains a few preset log levels corresponding to levels provided by slog.

package main

import (
	"context"
	"log/slog"

	"github.com/go-logr/logr"
	logf "github.com/sdcio/logger"
)

func main() {
	log := logr.FromSlogHandler(slog.NewJSONHandler(os.Stdout, nil))
	logf.SetDefaultLogger(log)

	ctx := logf.IntoContext(context.Background(), log)

	log.Info("first log message", "key", "value")

    RunContext(ctx)

    log = log.WithValues("additionalkey", "key2")
    ctx = logf.IntoContext(ctx, log)
    RunContext(ctx)
}

func RunContext(ctx context.Context) {
    log := logf.FromContext(ctx)
    log.Info("log from context logger")
}

Join us

Have questions, ideas, bug reports or just want to chat? Come join our discord server.

License and Code of Conduct

Code is under the Apache License 2.0, documentation is CC BY 4.0.

The SDC project is following the CNCF Code of Conduct. More information and links about the CNCF Code of Conduct are here.

Documentation

Index

Constants

View Source
const (
	VDebug = -int(slog.LevelDebug)
	VTrace = 8
)

slog uses negative levels to increase verbosity, but logr uses positive There is a mapping such that calling log.V(x) will translate this to call slog's handler with Level(-x). We therefore need to use increasingly positive values to imply increased log level

Variables

View Source
var DefaultLogger logr.Logger

Functions

func FromContext

func FromContext(ctx context.Context) logr.Logger

func IntoContext

func IntoContext(ctx context.Context, logger logr.Logger) context.Context

func ReplaceTimeAttr

func ReplaceTimeAttr(_ []string, a slog.Attr) slog.Attr

func SetDefaultLogger

func SetDefaultLogger(logger logr.Logger)

Types

This section is empty.

Jump to

Keyboard shortcuts

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