ctxlog

package
v0.0.0-...-58bca6d Latest Latest
Warning

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

Go to latest
Published: Apr 17, 2025 License: Apache-2.0 Imports: 3 Imported by: 1

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 Context
func Context(ctx context.Context, logger *slog.Logger) context.Context

ContextWithLogger returns a new context with the given logger.

Func ContextWith
func ContextWith(ctx context.Context, attributes ...any) context.Context

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

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

LoggerFromContext 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.

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 Context

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

ContextWithLogger returns a new context with the given logger.

func ContextWith

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

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

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 Logger

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

LoggerFromContext 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.ContextWith(ctx, "requestID", "123")
	logger = ctxlog.Logger(ctx)
	logger.Info("processing request")

	// Output will be JSON logs with context attributes
}
Output:

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
}
Output:

func Warn

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

Types

This section is empty.

Jump to

Keyboard shortcuts

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