ctxlog

package
v0.0.0-...-052fa94 Latest Latest
Warning

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

Go to latest
Published: Jan 14, 2026 License: Apache-2.0 Imports: 7 Imported by: 22

README

Package cloudeng.io/logging/ctxlog

import cloudeng.io/logging/ctxlog

Package ctxlog provides a context key and functions for logging to a context.

Functions

Func Debug
func Debug(ctx context.Context, msg string, args ...any)
Func Error
func Error(ctx context.Context, msg string, args ...any)
Func Info
func Info(ctx context.Context, msg string, args ...any)
Func Log
func Log(ctx context.Context, level slog.Level, msg string, args ...any)
Func LogDepth
func LogDepth(ctx context.Context, logger *slog.Logger, level slog.Level, depth int, msg string, args ...any)

LogDepth logs a message at the specified level with the caller information adjusted by the provided depth.

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

Logger returns the logger from the given context. If no logger is set, it returns a discard logger.

Func NewJSONLogger
func NewJSONLogger(ctx context.Context, w io.Writer, opts *slog.HandlerOptions) context.Context

NewJSONLogger returns a new context with a JSON logger.

Func NewLogLogger
func NewLogLogger(ctx context.Context, level slog.Level) *log.Logger

NewLogLogger returns a new standard library logger that logs to the provided context's logger at the specified level.

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

WithAttributes returns a new context with the embedded logger updated with the given logger attributes.

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

WithLogger returns a new context with the given logger.

Examples

ExampleLogger
ExampleNewJSONLogger

Documentation

Overview

Package ctxlog provides a context key and functions for logging to a context.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Debug

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

func Error

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

func Info

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

func Log

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

func LogDepth

func LogDepth(ctx context.Context, logger *slog.Logger, level slog.Level, depth int, msg string, args ...any)

LogDepth logs a message at the specified level with the caller information adjusted by the provided depth.

func Logger

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

Logger returns the logger from the given context. If no logger is set, it returns a discard logger.

Example
package main

import (
	"bytes"
	"context"

	"cloudeng.io/logging/ctxlog"
)

func main() {
	// Create a context with a JSON logger
	ctx := context.Background()
	buf := &bytes.Buffer{}
	ctx = ctxlog.NewJSONLogger(ctx, buf, nil)

	// Get logger from context and use it
	logger := ctxlog.Logger(ctx)
	logger.Info("hello world", "user", "alice")

	// Add attributes to logger
	ctx = ctxlog.WithAttributes(ctx, "requestID", "123")
	logger = ctxlog.Logger(ctx)
	logger.Info("processing request")

	// Output will be JSON logs with context attributes
}

func NewJSONLogger

func NewJSONLogger(ctx context.Context, w io.Writer, opts *slog.HandlerOptions) context.Context

NewJSONLogger returns a new context with a JSON logger.

Example
package main

import (
	"bytes"
	"context"
	"log/slog"

	"cloudeng.io/logging/ctxlog"
)

func main() {
	// Create a new context with JSON logger
	ctx := context.Background()
	buf := &bytes.Buffer{}
	opts := &slog.HandlerOptions{
		Level: slog.LevelDebug,
	}
	ctx = ctxlog.NewJSONLogger(ctx, buf, opts)

	// Use the logger
	logger := ctxlog.Logger(ctx)
	logger.Debug("debug message")
	logger.Info("info message")

	// Output will be JSON formatted logs
}

func NewLogLogger

func NewLogLogger(ctx context.Context, level slog.Level) *log.Logger

NewLogLogger returns a new standard library logger that logs to the provided context's logger at the specified level.

func Warn

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

func WithAttributes

func WithAttributes(ctx context.Context, attributes ...any) context.Context

WithAttributes returns a new context with the embedded logger updated with the given logger attributes.

func WithLogger

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

WithLogger returns a new context with the given logger.

Types

This section is empty.

Jump to

Keyboard shortcuts

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