slog

package
v0.3.0 Latest Latest
Warning

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

Go to latest
Published: Apr 4, 2026 License: Apache-2.0 Imports: 7 Imported by: 0

README

CI Go Report Card GoDoc

slog

import "github.com/go-coldbrew/log/loggers/slog"

Package slog provides a BaseLogger implementation for log/slog.

Example

This example shows how to use the slog backend with JSON output. The slog backend is the default and recommended backend.

package main

import (
	"context"

	"github.com/go-coldbrew/log"
	"github.com/go-coldbrew/log/loggers"

	cbslog "github.com/go-coldbrew/log/loggers/slog"
)

func main() {
	// Create a slog-backed logger with JSON output
	logger := cbslog.NewLogger(
		loggers.WithJSONLogs(true),
		loggers.WithCallerInfo(true),
	)

	// Set as the global logger
	log.SetLogger(log.NewLogger(logger))

	// Log normally — output goes through slog's JSONHandler
	ctx := context.Background()
	log.Info(ctx, "msg", "service started", "port", 8080)
}

Example (Text Output)

This example shows how to configure the slog backend with text output and a custom log level.

package main

import (
	"context"

	"github.com/go-coldbrew/log"
	"github.com/go-coldbrew/log/loggers"

	cbslog "github.com/go-coldbrew/log/loggers/slog"
)

func main() {
	logger := cbslog.NewLogger(
		loggers.WithJSONLogs(false),
		loggers.WithLevel(loggers.DebugLevel),
	)
	log.SetLogger(log.NewLogger(logger))

	ctx := context.Background()
	log.Debug(ctx, "msg", "debug message", "detail", "verbose")
}

Index

func NewLogger

func NewLogger(options ...loggers.Option) loggers.BaseLogger

NewLogger returns a BaseLogger implementation backed by log/slog.

func NewLoggerWithHandler

func NewLoggerWithHandler(handler slog.Handler, options ...loggers.Option) loggers.BaseLogger

NewLoggerWithHandler returns a BaseLogger implementation backed by the provided slog.Handler. Use this when you need a custom handler (e.g., for testing or custom output formats). Note: SetLevel updates the internally tracked level. Both this level and the provided handler's own level filtering apply; the stricter one wins.

Generated by gomarkdoc

Documentation

Overview

Package slog provides a BaseLogger implementation for log/slog.

Example

This example shows how to use the slog backend with JSON output. The slog backend is the default and recommended backend.

package main

import (
	"context"

	"github.com/go-coldbrew/log"
	"github.com/go-coldbrew/log/loggers"

	cbslog "github.com/go-coldbrew/log/loggers/slog"
)

func main() {
	// Create a slog-backed logger with JSON output
	logger := cbslog.NewLogger(
		loggers.WithJSONLogs(true),
		loggers.WithCallerInfo(true),
	)

	// Set as the global logger
	log.SetLogger(log.NewLogger(logger))

	// Log normally — output goes through slog's JSONHandler
	ctx := context.Background()
	log.Info(ctx, "msg", "service started", "port", 8080)
}
Example (TextOutput)

This example shows how to configure the slog backend with text output and a custom log level.

package main

import (
	"context"

	"github.com/go-coldbrew/log"
	"github.com/go-coldbrew/log/loggers"

	cbslog "github.com/go-coldbrew/log/loggers/slog"
)

func main() {
	logger := cbslog.NewLogger(
		loggers.WithJSONLogs(false),
		loggers.WithLevel(loggers.DebugLevel),
	)
	log.SetLogger(log.NewLogger(logger))

	ctx := context.Background()
	log.Debug(ctx, "msg", "debug message", "detail", "verbose")
}

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewLogger

func NewLogger(options ...loggers.Option) loggers.BaseLogger

NewLogger returns a BaseLogger implementation backed by log/slog.

func NewLoggerWithHandler

func NewLoggerWithHandler(handler slog.Handler, options ...loggers.Option) loggers.BaseLogger

NewLoggerWithHandler returns a BaseLogger implementation backed by the provided slog.Handler. Use this when you need a custom handler (e.g., for testing or custom output formats). Note: SetLevel updates the internally tracked level. Both this level and the provided handler's own level filtering apply; the stricter one wins.

Types

This section is empty.

Jump to

Keyboard shortcuts

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