Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 64 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,70 @@
</p>
<br>

## Features

- **Text Logging**: Traditional text-based logging with customizable prefixes and levels
- **JSON Logging**: Structured JSON logging using Go's `slog` library (Go 1.21+)
- **Level-based Filtering**: Support for TRACE, DEBUG, INFO, WARN, ERROR, and DISABLED levels
- **Scope-based Configuration**: Different log levels for different scopes/components
- **Environment Variable Configuration**: Configure log levels via environment variables
- **Thread-safe**: All logging operations are thread-safe

## Usage

### Text Logging (Default)

```go
import "github.com/pion/logging"

// Create a logger factory
factory := logging.NewDefaultLoggerFactory()

// Create loggers for different scopes
apiLogger := factory.NewLogger("api")
dbLogger := factory.NewLogger("database")

// Log messages
apiLogger.Info("API server started")
apiLogger.Debug("Processing request")
dbLogger.Error("Database connection failed")
```

### JSON Logging

```go
import "github.com/pion/logging"

// Create a JSON logger factory
factory := logging.NewJSONLoggerFactory()

// Create loggers for different scopes
apiLogger := factory.NewLogger("api")
dbLogger := factory.NewLogger("database")

// Log messages with structured data
apiLogger.Info("API server started")
apiLogger.Debug("Processing request", "method", "GET", "path", "/users")
dbLogger.Error("Database connection failed", "error", "connection timeout")
```

### Environment Variable Configuration

Set environment variables to configure log levels:

```bash
# Enable all log levels
export PION_LOG_TRACE=all
export PION_LOG_DEBUG=all
export PION_LOG_INFO=all
export PION_LOG_WARN=all
export PION_LOG_ERROR=all

# Enable specific scopes
export PION_LOG_DEBUG=api,database
export PION_LOG_INFO=feature1,feature2
```

### Roadmap
The library is used as a part of our WebRTC implementation. Please refer to that [roadmap](https://github.com/pion/webrtc/issues/9) to track our major milestones.

Expand Down
41 changes: 41 additions & 0 deletions examples/example_json_logger.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
// SPDX-FileCopyrightText: 2023 The Pion community <https://pion.ly>
// SPDX-License-Identifier: MIT

package main

import (
"os"

"github.com/pion/logging"
)

func main() {
// Create a JSON logger factory
factory := logging.NewJSONLoggerFactory(logging.WithJSONWriter(os.Stdout))

// Create loggers for different scopes
apiLogger := factory.NewLogger("api")
dbLogger := factory.NewLogger("database")
authLogger := factory.NewLogger("auth")

// Log some messages
apiLogger.Info("API server started")
apiLogger.Debugf("Processing request method=%s path=%s", "GET", "/users")
apiLogger.Warnf("Rate limit approaching requests=%d limit=%d", 95, 100)

dbLogger.Info("Database connection established")
dbLogger.Debugf("Executing query query=%q duration_ms=%d", "SELECT * FROM users", 15)

authLogger.Errorf("Authentication failed user_id=%s reason=%s", "12345", "invalid_token")
authLogger.Infof("User logged in user_id=%s ip=%s", "67890", "192.168.1.100")

// nolint:lll
// Example output will be JSON formatted like:
// {"time":"2023-12-07T10:30:00Z","level":"INFO","msg":"API server started","scope":"api"}
// {"time":"2023-12-07T10:30:00Z","level":"DEBUG","msg":"Processing request","scope":"api","method":"GET","path":"/users"}
// {"time":"2023-12-07T10:30:00Z","level":"WARN","msg":"Rate limit approaching","scope":"api","requests":95,"limit":100}
// {"time":"2023-12-07T10:30:00Z","level":"INFO","msg":"Database connection established","scope":"database"}
// {"time":"2023-12-07T10:30:00Z","level":"DEBUG","msg":"Executing query","scope":"database","query":"SELECT * FROM users","duration_ms":15}
// {"time":"2023-12-07T10:30:00Z","level":"ERROR","msg":"Authentication failed","scope":"auth","user_id":"12345","reason":"invalid_token"}
// {"time":"2023-12-07T10:30:00Z","level":"INFO","msg":"User logged in","scope":"auth","user_id":"67890","ip":"192.168.1.100"}
}
Loading
Loading