Skip to content

A flexible Go logging library enhancing log/slog with color support, structured logging, context awareness, and optimized performance.

License

Notifications You must be signed in to change notification settings

AlonMell/grovelog

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

9 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

GroveLog

GroveLog is a flexible logging library for Go, built on top of the standard log/slog package. It provides enhanced formatting options, color support, group handling, context-aware logging, and optimized performance.

Go Version Go Reference Go Report Card License: MIT

Features

  • Multiple output formats: JSON, Plain Text, and Colored Text
  • Thread-safe for concurrent use
  • Efficient memory usage with buffer pooling
  • Support for structured logging with attributes
  • Advanced grouping of attributes with nesting
  • Context-aware logging with attribute propagation
  • Error wrapping with context preservation
  • Compatible with the standard log/slog interface
  • Customizable time formats

Installation

go get github.com/AlonMell/grovelog

Quick Start

package main

import (
    "os"
    "log/slog"

    "github.com/AlonMell/grovelog"
)

func main() {
    // Create a color logger with INFO level
    opts := grovelog.NewOptions(slog.LevelInfo, "", grovelog.Color)
    logger := grovelog.NewLogger(os.Stdout, opts)

    // Simple logging
    logger.Info("Hello, GroveLog!")

    // With attributes
    logger.Info("User logged in",
        "user_id", 1234,
        "source", "api")

    // With groups
    dbLogger := logger.WithGroup("database")
    dbLogger.Info("Query executed",
        "query", "SELECT * FROM users",
        "duration_ms", 42)
}

Output Formats

GroveLog supports three output formats:

JSON Format

opts := grovelog.NewOptions(slog.LevelInfo, "", grovelog.JSON)
logger := grovelog.NewLogger(os.Stdout, opts)
logger.Info("Hello JSON", "key", "value")

Output:

{"time":"2025-04-07T10:30:45.123456789Z","level":"INFO","msg":"Hello JSON","key":"value"}

Plain Text Format

opts := grovelog.NewOptions(slog.LevelInfo, "", grovelog.Plain)
logger := grovelog.NewLogger(os.Stdout, opts)
logger.Info("Hello Plain", "key", "value")

Output:

time=2025-04-07T10:30:45.123456789Z level=INFO msg="Hello Plain" key=value

Color Format

opts := grovelog.NewOptions(slog.LevelInfo, "", grovelog.Color)
logger := grovelog.NewLogger(os.Stdout, opts)
logger.Info("Hello Color", "key", "value")

Output:

[10:30:45.123] INFO: Hello Color {"key":"value"}

Advanced Usage

Custom Time Format

opts := grovelog.NewOptions(slog.LevelInfo, "2006-01-02 15:04:05", grovelog.Color)
logger := grovelog.NewLogger(os.Stdout, opts)

Nested Groups

apiLogger := logger.WithGroup("api")
userLogger := apiLogger.WithGroup("users")
userLogger.Info("User created", "id", 1001, "email", "user@example.com")

Output:

[10:30:45.123] INFO: User created {"api.users.id":1001,"api.users.email":"user@example.com"}

Group and Attributes

authLogger := logger.WithGroup("auth").With("service", "oauth")
authLogger.Info("Token generated", "expires_in", 3600)

Performance

GroveLog is designed with performance in mind:

  • Buffer pooling to reduce memory allocations
  • Efficient JSON serialization
  • Minimal lock contention for thread safety

Development

Testing, Linting, Coverage, Benchmarks

make help

License

MIT License - see the LICENSE file for details.

About

A flexible Go logging library enhancing log/slog with color support, structured logging, context awareness, and optimized performance.

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published