Documentation
¶
Overview ¶
Package adapters provides adapters for various logging systems. and utilities for Adapter implementations.
Index ¶
- Constants
- func NewBufferedConsoleAdapter(w io.Writer, theme ConsoleAdapterTheme) *logging.AdapterComposer[*logging.BufferedAdapter[*ConsoleAdapter]]
- func NewConsoleAdapter(w io.Writer, theme ConsoleAdapterTheme) *logging.AdapterComposer[*ConsoleAdapter]
- type AttrMap
- type ConsoleAdapter
- func (c *ConsoleAdapter) BatchHandle(records []logging.Record) error
- func (c *ConsoleAdapter) Dispose() error
- func (c *ConsoleAdapter) Enabled(_ context.Context, l slog.Level) bool
- func (c *ConsoleAdapter) Flush() error
- func (c *ConsoleAdapter) HTTP(_ context.Context, method string, statusCode int, path string, ...) error
- func (c *ConsoleAdapter) HTTPBatchHandle(records []logging.HttpRecord) error
- func (c *ConsoleAdapter) Handle(_ context.Context, record slog.Record) error
- func (c *ConsoleAdapter) Ready()
- func (c *ConsoleAdapter) WithAttrs(attrs []slog.Attr) slog.Handler
- func (c *ConsoleAdapter) WithGroup(name string) slog.Handler
- type ConsoleAdapterTheme
Constants ¶
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 ¶
func NewBufferedConsoleAdapter ¶
func NewBufferedConsoleAdapter(w io.Writer, theme ConsoleAdapterTheme) *logging.AdapterComposer[*logging.BufferedAdapter[*ConsoleAdapter]]
func NewConsoleAdapter ¶
func NewConsoleAdapter(w io.Writer, theme ConsoleAdapterTheme) *logging.AdapterComposer[*ConsoleAdapter]
Types ¶
type AttrMap ¶
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 ¶
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 ¶
Get retrieves the value for the given key, returning nil if the key does not exist.
func (*AttrMap) MarshalJSON ¶
MarshalJSON implements json.Marshaler, encoding the AttrMap to JSON.
type ConsoleAdapter ¶
type ConsoleAdapter struct {
// contains filtered or unexported fields
}
func (*ConsoleAdapter) BatchHandle ¶
func (c *ConsoleAdapter) BatchHandle(records []logging.Record) error
func (*ConsoleAdapter) Enabled ¶
Enabled reports whether the adapter handles records at the given level, based on the configured minimum level.
func (*ConsoleAdapter) HTTPBatchHandle ¶
func (c *ConsoleAdapter) HTTPBatchHandle(records []logging.HttpRecord) error
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 ¶
func (t ConsoleAdapterTheme) Build() ConsoleAdapterTheme
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.