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
17 changes: 14 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<h4 align="center">The Pion logging library</h4>
<p align="center">
<a href="https://pion.ly"><img src="https://img.shields.io/badge/pion-logging-gray.svg?longCache=true&colorB=brightgreen" alt="Pion transport"></a>
<a href="https://discord.gg/PngbdqpFbt"><img src="https://img.shields.io/badge/join-us%20on%20discord-gray.svg?longCache=true&logo=discord&colorB=brightblue" alt="join us on Discord"></a> <a href="https://bsky.app/profile/pion.ly"><img src="https://img.shields.io/badge/follow-us%20on%20bluesky-gray.svg?longCache=true&logo=bluesky&colorB=brightblue" alt="Follow us on Bluesky"></a>
<a href="https://discord.gg/PngbdqpFbt"><img src="https://img.shields.io/badge/join-us%20on%20discord-gray.svg?longCache=true&logo=discord&colorB=brightblue" alt="join us on Discord"></a> <a href="https://bsky.app/profile/pion.ly"><img src="https://img.shields.io/badge/follow-us%20on%20bluesky-gray.svg?longCache=true&logo=bluesky&colorB=brightblue" alt="Follow us on Bluesky"></a>
<br>
<img alt="GitHub Workflow Status" src="https://img.shields.io/github/actions/workflow/status/pion/logging/test.yaml">
<a href="https://pkg.go.dev/github.com/pion/logging"><img src="https://pkg.go.dev/badge/github.com/pion/logging.svg" alt="Go Reference"></a>
Expand Down Expand Up @@ -58,9 +58,20 @@ apiLogger := factory.NewLogger("api")
dbLogger := factory.NewLogger("database")

// Log messages with structured data
// Note that you can pass in a single string like this:
apiLogger.Info("API server started")
apiLogger.Debug("Processing request", "method", "GET", "path", "/users")
dbLogger.Error("Database connection failed", "error", "connection timeout")

// Or you can pass in multiple strings with printf style formatting.
// NOTE: Key-value pairs are NOT logged as separate JSON fields.
// As a result, they are just appended to the msg field.
// This example shows how to pass in multiple strings:
apiLogger.Debugf("Processing request method=%s path=%s", "GET", "/users")
// outputs this:
// {"time":"2026-01-31T15:28:58.751640584-05:00","level":"DEBUG","msg":"Processing request method=GET path=/users","scope":"api"}

dbLogger.Errorf("Database connection failed reason=%s", "connection timeout")
// outputs this:
// {"time":"2026-01-31T15:28:58.751658047-05:00","level":"ERROR","msg":"Database connection failed reason=connection timeout","scope":"database"}
```

### Environment Variable Configuration
Expand Down
15 changes: 7 additions & 8 deletions examples/example_json_logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,11 @@ func main() {
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"}
// {"time":"2026-01-31T15:28:21.275282663-05:00","level":"INFO","msg":"API server started","scope":"api"}
// {"time":"2026-01-31T15:28:21.275387414-05:00","level":"DEBUG","msg":"Processing request method=GET path=/users","scope":"api"}
// {"time":"2026-01-31T15:28:21.275407888-05:00","level":"WARN","msg":"Rate limit approaching requests=95 limit=100","scope":"api"}
// {"time":"2026-01-31T15:28:21.275426935-05:00","level":"INFO","msg":"Database connection established","scope":"database"}
// {"time":"2026-01-31T15:28:21.275446032-05:00","level":"DEBUG","msg":"Executing query query=\"SELECT * FROM users\" duration_ms=15","scope":"database"}
// {"time":"2026-01-31T15:28:21.27546512-05:00","level":"ERROR","msg":"Authentication failed user_id=12345 reason=invalid_token","scope":"auth"}
// {"time":"2026-01-31T15:28:21.275483988-05:00","level":"INFO","msg":"User logged in user_id=67890 ip=192.168.1.100","scope":"auth"}
}
Loading