An open API service indexing awesome lists of open source software.

https://github.com/islamghany/logy

A structured logging package for Go applications built on top of `slog`. It provides JSON-formatted logs with additional features like trace ID support, service name injection, and custom event handlers.
https://github.com/islamghany/logy

go golang logging

Last synced: 8 months ago
JSON representation

A structured logging package for Go applications built on top of `slog`. It provides JSON-formatted logs with additional features like trace ID support, service name injection, and custom event handlers.

Awesome Lists containing this project

README

          

# Logy

A structured logging package for Go applications built on top of `slog`. It provides JSON-formatted logs with additional features like trace ID support, service name injection, and custom event handlers.

## Features

- JSON-formatted structured logging
- Context-aware logging
- Trace ID support
- Service name injection
- Source file and line information
- Custom event handlers
- Multiple log levels (Debug, Info, Warn, Error)
- Thread-safe logging

## Installation

```bash
go get github.com/islamghany/logy
```

## Usage

### Basic Usage

```go
package main

import (
"context"
"os"

"github.com/islamghany/logy"
)

func main() {
// Create a new logger with stdout output and Info level
logger := logy.New(os.Stdout, logy.LevelInfo, "my-service", nil)

ctx := context.Background()

// Log messages with different levels
logger.Debug(ctx, "Debug message")
logger.Info(ctx, "Info message", "key", "value")
logger.Warn(ctx, "Warning message")
logger.Error(ctx, "Error message")
}
```

### With Trace ID

```go
package main

import (
"context"
"os"

"github.com/islamghany/logy"
)

func main() {
// Create a trace ID function
traceIDFunc := func(ctx context.Context) string {
// Extract trace ID from context
return "trace-123"
}

logger := logy.New(os.Stdout, logy.LevelInfo, "my-service", traceIDFunc)

ctx := context.Background()
logger.Info(ctx, "Message with trace ID")
}
```

### With Custom Event Handlers

```go
package main

import (
"context"
"os"

"github.com/islamghany/logy"
)

func main() {
events := logy.Events{
Info: func(ctx context.Context, r logy.Record) {
// Custom handling for Info level logs
fmt.Printf("Custom Info handler: %s\n", r.Message)
},
}

logger := logy.NewWithEvents(os.Stdout, logy.LevelInfo, "my-service", nil, events)

ctx := context.Background()
logger.Info(ctx, "Message with custom handler")
}
```

### Log Levels

```go
const (
LevelDebug = Level(slog.LevelDebug)
LevelInfo = Level(slog.LevelInfo)
LevelWarn = Level(slog.LevelWarn)
LevelError = Level(slog.LevelError)
)
```

## Output Format

The logger produces JSON-formatted output with the following structure:

```json
{
"time": "2024-01-01T12:00:00Z",
"level": "INFO",
"msg": "message",
"service": "my-service",
"file": "main.go:42",
"trace_id": "trace-123",
"key": "value"
}
```

## License

MIT License - see LICENSE file for details