Tagged Logging vs Structured Logging
Jeff Dwyer
Posted on September 13, 2023
Understanding Tagged vs. Structured Logging
What's Tagged Logging?
Tagged logging allows you to prepend tags to your log messages. For instance, using ActiveSupport::TaggedLogging, you can tag logs with "Auth API" or user IDs.
class Permissions
def get(user)
logger.tagged("Auth API") do
uri = URI("https://auth.example.com/permissions/?user=#{user}")
logger.info { "Fetching #{uri}" }
permissions = Permissions.parse(Net::HTTP.get(uri))
logger.info { "Got permissions #{permissions.to_s}" }
end
end
end
# Log Output
#[Auth API] Fetching https://auth.example.com/permissions/?user=bob
#[Auth API] Got permissions admin, writer, reader
Pros:
- Enhances log readability.
- Avoids repetition by tagging once for multiple log lines.
Cons:
- Not machine-friendly. While the text version looks good, the JSON format can be confusing, especially when searching for specific tags.
Structured Logging: A Better Alternative?
Structured logging provides a more organized way to log messages. Instead of tagging, you structure your logs with key-value pairs.
Example:
log.info "hello", user: "1", team: "The best team"
Output:
Text Log:
INFO hello user=1, team=The Best Team
JSON Log:
{severity: INFO, message: "hello", user: 1, team: "The Best Team"}
This approach is more machine-readable and fits better with modern log aggregators.
Libraries Supporting Structured Logging:
Note: Lograge offers structured logs for Rails requests but lacks custom structuring.
Why I Wrote This
I'm building a dynamic log library and wanted to understand the best approaches to structured and tagged logging.
In Summary:
- Tagged logging is user-friendly but not machine-friendly.
- Structured logging offers a more organized and machine-readable approach.
- Tagged logging does have nice nesting behavior, though you can achieve that with the semantic logger gem as well.
💖 💪 🙅 🚩
Jeff Dwyer
Posted on September 13, 2023
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.
Related
codenewbie My Journey Through Ruby Event Manager, Hangman, and Tackling Coolant Leaks
November 29, 2024