From 4e0fa372074bb60d20ad3ab3bfc5d576db873da5 Mon Sep 17 00:00:00 2001 From: philipch07 Date: Sat, 31 Jan 2026 15:25:53 -0500 Subject: [PATCH] Fix example and clarify readme --- README.md | 17 ++++++++++++++--- examples/example_json_logger.go | 15 +++++++-------- 2 files changed, 21 insertions(+), 11 deletions(-) diff --git a/README.md b/README.md index e03558b..c8e5c3d 100644 --- a/README.md +++ b/README.md @@ -6,7 +6,7 @@

The Pion logging library

Pion transport - join us on Discord Follow us on Bluesky + join us on Discord Follow us on Bluesky
GitHub Workflow Status Go Reference @@ -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 diff --git a/examples/example_json_logger.go b/examples/example_json_logger.go index d6d4c3c..2bd8587 100644 --- a/examples/example_json_logger.go +++ b/examples/example_json_logger.go @@ -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"} }