slogmanager

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Feb 23, 2025 License: MIT Imports: 4 Imported by: 0

README

slogmanager


Concurrent-Safe Manager for Slog with Multiple Writers

A Go library providing simple writers setup for Slog

Features

  • Multiple Writers: Configure different outputs (console, files, buffers)
  • Concurrent-Safe: Thread-safe writer operations
  • Format Support: JSON and text logging formats
  • Dynamic Configuration: Add/remove writers at runtime
  • Handler Customization: Create custom handlers for specialized logging

Installation

go get github.com/nzb3/slogmanager

Usage

Basic Example
package main

import (
	"bytes"
	"github.com/nzb3/slogmanager"
	"log/slog"
)

func main() {
	manager := slogmanager.New()
	
	// Add console writer
	consoleWriter := slogmanager.NewWriter(&bytes.Buffer{}, slogmanager.WithTextFormat())
	manager.AddWriter("console", consoleWriter)
	
	slog.Info("System initialized", "status", "ready")
}
JSON Formatting
jsonWriter := slogmanager.NewWriter(
    &bytes.Buffer{},
    slogmanager.WithJSONFormat(),
)
manager.AddWriter("json-output", jsonWriter)

slog.Info("System initialized", "status", "ready")
// Output:
// {"time":"2025-02-22T23:49:00Z","level":"INFO","msg":"Data processed","items":42}
Text Formatting
textWriter := slogmanager.NewWriter(
    &bytes.Buffer{},
    slogmanager.WithTextFormat(),
)
manager.AddWriter("text-output", textWriter)

slog.Info("System initialized", "status", "ready")
// Output:
// 2025/02/22 23:49:00 INFO request completed method=GET duration=150ms

Configuration Options

Option Description
WithJSONFormat() JSON-structured logs
WithTextFormat() Human-readable text format
WithLevel(level slog.Level) Set minimum log level
WithHandlerOpts(opts *slog.HandlerOptions) Custom handler options

License

MIT License - See LICENSE for details.

Documentation

Overview

Package slogmanager provides a centralized logging management system using slog. It supports multiple writers and concurrent access to logging facilities.

Package writer provides a customizable writer implementation for structured logging with support for both JSON and text formats using the slog package.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Manager

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

Manager is the central structure for managing multiple log writers and handlers. It provides thread-safe operations for managing logging configuration.

func New

func New() *Manager

New creates and initializes a new Manager instance. It initializes an empty map of writers that can be populated later.

Returns:

  • *Manager: A new manager instance ready for use.

func (*Manager) AddWriter

func (m *Manager) AddWriter(name string, writer *Writer)

AddWriter registers a new writer with the manager. It's thread-safe to add a writer and after updates logger

Parameters:

  • name: The name of writer to add to the manager
  • writer: The writer instance to add to the manager.

func (*Manager) Logger

func (m *Manager) Logger() *slog.Logger

Logger returns the current logger instance from the manager. It provides thread-safe access to the logger.

Returns:

  • *slog.Logger: The current logger instance.

func (*Manager) RemoveWriter

func (m *Manager) RemoveWriter(name string)

RemoveWriter unregisters a writer from the manager. It's thread-safe and automatically updates the logger configuration after removing the writer

Parameters:

  • name: The writer's name to remove from the manager.

func (*Manager) Writers

func (m *Manager) Writers() map[string]*Writer

Writers returns the current writers map from the manager. It provides thread-safe access to the writers.

Returns:

  • map[string]*Writer: The current writers.

type Option

type Option func(*writerConfig)

Option defines the function type for writer configuration options. It follows the functional options pattern for flexible and extensible configuration.

func WithJSONFormat

func WithJSONFormat() Option

WithJSONFormat returns an Option that sets the writer format to JSON. This is useful when structured logging output is needed.

Returns:

  • Option: A function that sets UseJSON to true when applied.

func WithSlogHandlerOptions

func WithSlogHandlerOptions(h *slog.HandlerOptions) Option

WithSlogHandlerOptions returns an Option that sets custom slog handler options. These options can configure various aspects of log handling like level filtering, time formats, and source file information.

Parameters:

  • h: Pointer to slog.HandlerOptions containing desired handler configuration

Returns:

  • Option: A function that applies the provided handler options when used.

func WithTextFormat

func WithTextFormat() Option

WithTextFormat returns an Option that sets the writer format to text. This is the default format if no format option is specified.

Returns:

  • Option: A function that sets UseJSON to false when applied.

type Writer

type Writer struct {
	Config *writerConfig
	io.Writer
}

Writer represents a custom writer that implements io.Writer interface and includes configuration options for logging format and handling.

func NewWriter

func NewWriter(writer io.Writer, opts ...Option) *Writer

NewWriter creates a new Writer instance with specified options. It uses the functional options pattern for flexible configuration.

Parameters:

  • writer: An io.Writer interface implementation for actual writing
  • opts: Variable number of Option functions for configuration

Returns:

  • *Writer: A configured Writer instance.

Jump to

Keyboard shortcuts

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