slog

package
v1.70.1 Latest Latest
Warning

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

Go to latest
Published: Nov 21, 2024 License: Apache-2.0, BSD-3-Clause, Apache-2.0 Imports: 7 Imported by: 0

Documentation

Overview

Package slog provides functions to correlate logs and traces using log/slog package (https://pkg.go.dev/log/slog).

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewJSONHandler

func NewJSONHandler(w io.Writer, opts *slog.HandlerOptions) slog.Handler

NewJSONHandler is a convenience function that returns a *slog.JSONHandler logger enhanced with tracing information.

Example
package main

import (
	"context"
	"log/slog"
	"os"

	slogtrace "gopkg.in/DataDog/dd-trace-go.v1/contrib/log/slog"
	"gopkg.in/DataDog/dd-trace-go.v1/ddtrace/tracer"
)

func main() {
	// start the DataDog tracer
	tracer.Start()
	defer tracer.Stop()

	// create the application logger
	logger := slog.New(slogtrace.NewJSONHandler(os.Stdout, nil))

	// start a new span
	span, ctx := tracer.StartSpanFromContext(context.Background(), "ExampleNewJSONHandler")
	defer span.Finish()

	// log a message using the context containing span information
	logger.Log(ctx, slog.LevelInfo, "this is a log with tracing information")
}
Output:

func WrapHandler

func WrapHandler(h slog.Handler) slog.Handler

WrapHandler enhances the given logger handler attaching tracing information to logs.

Example
package main

import (
	"context"
	"log/slog"
	"os"

	slogtrace "gopkg.in/DataDog/dd-trace-go.v1/contrib/log/slog"
	"gopkg.in/DataDog/dd-trace-go.v1/ddtrace/tracer"
)

func main() {
	// start the DataDog tracer
	tracer.Start()
	defer tracer.Stop()

	// create the application logger
	myHandler := slog.NewJSONHandler(os.Stdout, nil)
	logger := slog.New(slogtrace.WrapHandler(myHandler))

	// start a new span
	span, ctx := tracer.StartSpanFromContext(context.Background(), "ExampleWrapHandler")
	defer span.Finish()

	// log a message using the context containing span information
	logger.Log(ctx, slog.LevelInfo, "this is a log with tracing information")
}
Output:

Types

This section is empty.

Jump to

Keyboard shortcuts

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