sloggergo

package module
v0.0.1 Latest Latest
Warning

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

Go to latest
Published: Feb 6, 2026 License: MIT Imports: 10 Imported by: 0

README

sloggergo

Go structured logger with zero dependencies by default.

Features

  • Zero Dependency: Core library has 0 external dependencies.
  • Extensible: Interface-based usage for Sinks and Formatters.
  • Async Support: Native asynchronous logging with buffering.
  • Observability Ready: Built-in support (via standard library HTTP) for Elasticsearch, Loki, and Datadog.

Usage

package main

import (
    "github.com/godeh/sloggergo"
    "github.com/godeh/sloggergo/sink"
)

func main() {
    // Create a new logger writing to stdout
    log := sloggergo.New(
        sloggergo.WithLevel(sloggergo.InfoLevel),
        sloggergo.WithSink(sink.NewStdout()),
    )
    defer log.Close()

    log.Info("Hello world")
}

Examples

Check the examples directory for more usage scenarios:

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type AsyncLogger

type AsyncLogger struct {
	*Logger
	// contains filtered or unexported fields
}

AsyncLogger wraps a Logger with async capabilities.

func NewAsync

func NewAsync(logger *Logger, opts ...AsyncOption) *AsyncLogger

NewAsync creates a new async logger.

func (*AsyncLogger) BufferLen

func (a *AsyncLogger) BufferLen() int

BufferLen returns the current buffer length.

func (*AsyncLogger) Close

func (a *AsyncLogger) Close() error

Close flushes and closes the async logger.

func (*AsyncLogger) Debug

func (a *AsyncLogger) Debug(msg string, keyvals ...slog.Attr)

Debug logs a debug message asynchronously.

func (*AsyncLogger) DebugContext

func (a *AsyncLogger) DebugContext(ctx context.Context, msg string, keyvals ...slog.Attr)

DebugContext logs a debug message asynchronously with context.

func (*AsyncLogger) Error

func (a *AsyncLogger) Error(msg string, keyvals ...slog.Attr)

Error logs an error message asynchronously.

func (*AsyncLogger) ErrorContext

func (a *AsyncLogger) ErrorContext(ctx context.Context, msg string, keyvals ...slog.Attr)

ErrorContext logs an error message asynchronously with context.

func (*AsyncLogger) Fatal

func (a *AsyncLogger) Fatal(msg string, keyvals ...slog.Attr)

Fatal logs a fatal message (runs synchronously for safety).

func (*AsyncLogger) Flush

func (a *AsyncLogger) Flush()

Flush waits for all buffered logs to be written.

func (*AsyncLogger) Info

func (a *AsyncLogger) Info(msg string, keyvals ...slog.Attr)

Info logs an info message asynchronously.

func (*AsyncLogger) InfoContext

func (a *AsyncLogger) InfoContext(ctx context.Context, msg string, keyvals ...slog.Attr)

InfoContext logs an info message asynchronously with context.

func (*AsyncLogger) IsFull

func (a *AsyncLogger) IsFull() bool

IsFull returns true if the buffer is full.

func (*AsyncLogger) Warn

func (a *AsyncLogger) Warn(msg string, keyvals ...slog.Attr)

Warn logs a warning message asynchronously.

func (*AsyncLogger) WarnContext

func (a *AsyncLogger) WarnContext(ctx context.Context, msg string, keyvals ...slog.Attr)

WarnContext logs a warning message asynchronously with context.

type AsyncOption

type AsyncOption func(*AsyncLogger)

AsyncOption configures an AsyncLogger.

func WithBufferSize

func WithBufferSize(size int) AsyncOption

WithBufferSize sets the buffer size for async logging.

func WithSampling

func WithSampling(config *SamplingConfig) AsyncOption

WithSampling enables log sampling.

func WithShutdownTimeout

func WithShutdownTimeout(d time.Duration) AsyncOption

WithShutdownTimeout sets the timeout for graceful shutdown.

func WithWorkers

func WithWorkers(n int) AsyncOption

WithWorkers sets the number of async workers.

type ContextExtractor

type ContextExtractor func(ctx context.Context) []slog.Attr

ContextExtractor extracts attributes from a context.

type ErrorHandler

type ErrorHandler func(error)

ErrorHandler is a function that handles errors from sinks.

type Hook

type Hook func(ctx context.Context, entry *formatter.Entry) error

Hook is a function that can intercept and modify log entries. It returns an error if the entry should be dropped or if an error occurred.

type Level

type Level int

Level represents the severity of a log entry.

const (
	DebugLevel Level = iota
	InfoLevel
	WarnLevel
	ErrorLevel
	FatalLevel
)

func ParseLevel

func ParseLevel(s string) Level

ParseLevel parses a string into a Level.

func (Level) String

func (l Level) String() string

type Logger

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

Logger is the main logging interface.

func New

func New(opts ...Option) *Logger

New creates a new logger with the given options.

func NewFromConfig

func NewFromConfig(path string) (*Logger, error)

NewFromConfig creates a new logger from a YAML configuration file.

func NewFromConfigStruct

func NewFromConfigStruct(cfg *config.Config) (*Logger, error)

NewFromConfigStruct creates a new logger from a config struct.

func (*Logger) AddSink

func (l *Logger) AddSink(s sink.Sink)

AddSink adds a new sink to the logger.

func (*Logger) Close

func (l *Logger) Close() error

Close closes all sinks.

func (*Logger) Debug

func (l *Logger) Debug(msg string, keyvals ...slog.Attr)

Debug logs a debug message.

func (*Logger) DebugContext

func (l *Logger) DebugContext(ctx context.Context, msg string, keyvals ...slog.Attr)

DebugContext logs a debug message with context.

func (*Logger) Error

func (l *Logger) Error(msg string, keyvals ...slog.Attr)

Error logs an error message.

func (*Logger) ErrorContext

func (l *Logger) ErrorContext(ctx context.Context, msg string, keyvals ...slog.Attr)

ErrorContext logs an error message with context.

func (*Logger) Fatal

func (l *Logger) Fatal(msg string, keyvals ...slog.Attr)

Fatal logs a fatal message and exits.

func (*Logger) FatalContext

func (l *Logger) FatalContext(ctx context.Context, msg string, keyvals ...slog.Attr)

FatalContext logs a fatal message with context.

func (*Logger) Info

func (l *Logger) Info(msg string, keyvals ...slog.Attr)

Info logs an info message.

func (*Logger) InfoContext

func (l *Logger) InfoContext(ctx context.Context, msg string, keyvals ...slog.Attr)

InfoContext logs an info message with context.

func (*Logger) SetLevel

func (l *Logger) SetLevel(level Level)

SetLevel changes the minimum log level.

func (*Logger) Shutdown

func (l *Logger) Shutdown(reason string)

Shutdown logs application shutdown

func (*Logger) Warn

func (l *Logger) Warn(msg string, keyvals ...slog.Attr)

Warn logs a warning message.

func (*Logger) WarnContext

func (l *Logger) WarnContext(ctx context.Context, msg string, keyvals ...slog.Attr)

WarnContext logs a warning message with context.

func (*Logger) With

func (l *Logger) With(keyvals ...any) *Logger

With returns a new logger with additional fields.

type Option

type Option func(*Logger)

Option configures a Logger.

func WithCaller

func WithCaller(enabled bool) Option

WithCaller enables caller information in log entries.

func WithContextExtractor

func WithContextExtractor(extractor ContextExtractor) Option

WithContextExtractor sets a context extractor.

func WithErrorHandler

func WithErrorHandler(handler ErrorHandler) Option

WithErrorHandler sets the error handler for the logger.

func WithFields

func WithFields(fields map[string]any) Option

WithFields adds default fields to all log entries.

func WithHook

func WithHook(hook Hook) Option

WithHook adds a hook to the logger.

func WithLevel

func WithLevel(level Level) Option

WithLevel sets the minimum log level.

func WithSink

func WithSink(s sink.Sink) Option

WithSink adds a sink to the logger.

func WithTimeFormat

func WithTimeFormat(format string) Option

WithTimeFormat sets the time format for log entries.

type SampledLogger

type SampledLogger struct {
	*Logger
	// contains filtered or unexported fields
}

SampledLogger wraps a logger with sampling.

func NewSampled

func NewSampled(logger *Logger, config *SamplingConfig) *SampledLogger

NewSampled creates a logger with sampling.

func (*SampledLogger) Debug

func (s *SampledLogger) Debug(msg string, keyvals ...slog.Attr)

Debug logs with sampling.

func (*SampledLogger) Error

func (s *SampledLogger) Error(msg string, keyvals ...slog.Attr)

Error always logs (no sampling for errors).

func (*SampledLogger) Fatal

func (s *SampledLogger) Fatal(msg string, keyvals ...slog.Attr)

Fatal always logs (no sampling for fatal).

func (*SampledLogger) Info

func (s *SampledLogger) Info(msg string, keyvals ...slog.Attr)

Info logs with sampling.

func (*SampledLogger) Warn

func (s *SampledLogger) Warn(msg string, keyvals ...slog.Attr)

Warn logs with sampling.

type SamplingConfig

type SamplingConfig struct {
	Initial    int           // Log first N entries per interval
	Thereafter int           // Then log every N-th entry
	Interval   time.Duration // Sampling interval
}

SamplingConfig configures log sampling.

Directories

Path Synopsis
examples
async command
basic command
error_handling command
file command
json command

Jump to

Keyboard shortcuts

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