adapters

package
v0.6.1 Latest Latest
Warning

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

Go to latest
Published: Dec 4, 2025 License: Apache-2.0 Imports: 13 Imported by: 0

Documentation

Overview

Package adapters provides adapters for various logging systems. and utilities for Adapter implementations.

Index

Constants

View Source
const (
	// NL represents the newline character.
	NL byte = '\n'
	// SP represents the space character.
	SP byte = ' '
	// TAB represents the tab character.
	TAB byte = '\t'
	// CR represents the return character.
	CR byte = '\r'
)

Variables

This section is empty.

Functions

Types

type AttrMap

type AttrMap map[string]any

AttrMap is a pooled map for adapters to efficiently convert slog.Attr to map[string]any for example JSON encoding. It uses a sync.Pool to reuse maps, reducing allocation overhead. Use this instead of creating new map[string]any instances in adapters that frequently build structures from log attributes. Always call Free() when done to return the map to the pool, ensuring efficient reuse. Oversized maps (capacity > 32) are discarded to prevent excessive memory retention.

Example:

attrs := []slog.Attr{slog.String("key", "value")}
m := logging.AttrGroupToMap(attrs)
data, _ := m.MarshalJSON()
m.Free()

func AttrGroupToMap

func AttrGroupToMap(gv []slog.Attr) *AttrMap

AttrGroupToMap converts a slice of slog.Attr to an AttrMap, handling nested groups recursively. The resulting map is suitable for e.g. JSON encoding.

func NewAttrMap

func NewAttrMap() *AttrMap

NewAttrMap retrieves an AttrMap from the pool with an initial capacity of 10. Always call Free() when done to return the map to the pool.

func (*AttrMap) Free

func (m *AttrMap) Free()

Free returns the AttrMap to the pool if its capacity is ≤ 32, resetting its contents. Larger maps are discarded to prevent excessive memory use.

func (*AttrMap) Get

func (m *AttrMap) Get(key string) any

Get retrieves the value for the given key, returning nil if the key does not exist.

func (*AttrMap) Len

func (m *AttrMap) Len() int

Len returns the number of key-value pairs in the AttrMap.

func (*AttrMap) MarshalJSON

func (m *AttrMap) MarshalJSON() ([]byte, error)

MarshalJSON implements json.Marshaler, encoding the AttrMap to JSON.

func (*AttrMap) Reset

func (m *AttrMap) Reset()

Reset clears the AttrMap's contents without returning it to the pool.

func (*AttrMap) Set

func (m *AttrMap) Set(key string, value any) *AttrMap

Set adds or updates a key-value pair in the AttrMap, returning itself for chaining.

type ConsoleAdapter

type ConsoleAdapter struct {
	// contains filtered or unexported fields
}

func (*ConsoleAdapter) BatchHandle

func (c *ConsoleAdapter) BatchHandle(records []logging.Record) error

func (*ConsoleAdapter) Dispose

func (c *ConsoleAdapter) Dispose() error

Dispose is noop

func (*ConsoleAdapter) Enabled

func (c *ConsoleAdapter) Enabled(_ context.Context, l slog.Level) bool

Enabled reports whether the adapter handles records at the given level, based on the configured minimum level.

func (*ConsoleAdapter) Flush

func (c *ConsoleAdapter) Flush() error

Flush is noop

func (*ConsoleAdapter) HTTP

func (c *ConsoleAdapter) HTTP(_ context.Context, method string, statusCode int, path string, record slog.Record) error

func (*ConsoleAdapter) HTTPBatchHandle

func (c *ConsoleAdapter) HTTPBatchHandle(records []logging.HttpRecord) error

func (*ConsoleAdapter) Handle

func (c *ConsoleAdapter) Handle(_ context.Context, record slog.Record) error

Handle processes a log record

func (*ConsoleAdapter) Ready

func (c *ConsoleAdapter) Ready()

Ready is noop

func (*ConsoleAdapter) WithAttrs

func (c *ConsoleAdapter) WithAttrs(attrs []slog.Attr) slog.Handler

WithAttrs returns a new slog.Handler with the specified attributes added. The original adapter is not modified.

func (*ConsoleAdapter) WithGroup

func (c *ConsoleAdapter) WithGroup(name string) slog.Handler

WithGroup returns a new slog.Handler with the specified group name. The original adapter is not modified.

type ConsoleAdapterTheme

type ConsoleAdapterTheme struct {
	TimeLabel  ansicolor.Style
	SourceLink ansicolor.Style
	Attrs      ansicolor.Style

	LevelHappy   ansicolor.Style
	LevelTrace   ansicolor.Style
	LevelDebug   ansicolor.Style
	LevelInfo    ansicolor.Style
	LevelNotice  ansicolor.Style
	LevelSuccess ansicolor.Style
	LevelNotImpl ansicolor.Style
	LevelWarn    ansicolor.Style
	LevelDepr    ansicolor.Style
	LevelError   ansicolor.Style
	LevelOut     ansicolor.Style
	LevelBUG     ansicolor.Style
	// contains filtered or unexported fields
}

ConsoleAdapterTheme defines styling for ConsoleAdapter log output, using ansicolor.Style for levels, timestamps, attributes, and source links. Each level has a dedicated style, and the theme is built before use to format level strings with consistent padding.

func ConsoleAdapterDefaultTheme

func ConsoleAdapterDefaultTheme() ConsoleAdapterTheme

ConsoleAdapterDefaultTheme returns a default theme with predefined ANSI color styles for levels, timestamps, attributes, and source links. It must be built before use with Build() when using it outside of the happy logging package. ConsoleAdapter will call it when building the adapter.

func (ConsoleAdapterTheme) Build

Build composes the theme. It returns the built theme, ready for use by ConsoleAdapter.

func (ConsoleAdapterTheme) HttpMethod

func (t ConsoleAdapterTheme) HttpMethod(method string, code int) string

func (ConsoleAdapterTheme) LevelString

func (t ConsoleAdapterTheme) LevelString(lvl logging.Level) string

LevelString returns the styled string representation of a logging level, padded to a consistent width. For unrecognized levels, it falls back to the level's string representation with default formatting.

Directories

Path Synopsis
console module

Jump to

Keyboard shortcuts

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