Introduction
LogStruct adds JSON structured logging to any Rails app. Simply add the gem to your Gemfile and add an initializer to configure it. Now your Rails app prints beautiful JSON logs to STDOUT.
Features
- JSON logging enabled by default in production and test environments.
- Sets up Lograge for structured request logging.
- Uses Semantic Logger as the logging framework.
- Automatic integration with many Rails components and third-party gems.
- Customizable error handling and reporting with sensible, production-ready defaults.
- Sensitive data scrubbing and param filtering for security and privacy.
- Host authorization response app for logging "blocked host" security violations.
- Rack middleware for logging errors, IP Spoofing, CSRF violations, etc.
- Type checking with Sorbet.
Logging with LogStruct
# Log a simple string
Rails.logger.info "User signed in"
# Log a hash with custom fields
Rails.logger.info({
event: "user_login",
user_id: user.id,
ip_address: "192.168.1.1",
custom_field: "any value you want"
})
This approach is ideal for most applications. No knowledge of Sorbet is required.