Rails Logging Best Practices for Observability
Improve application monitoring with structured and scalable Rails logging
Introduction
Effective logging is essential for maintaining observability in Rails applications. Proper logs help with debugging, performance monitoring, and security auditing while ensuring your system remains scalable.
In this guide, we will cover:
✔️ Why logging matters in Rails
✔️ Setting up structured logging
✔️ Using log levels effectively
✔️ Centralizing and analyzing logs
✔️ Performance optimizations for log storage
1. Why Logging Matters in Rails
Logging in Rails helps with:
✅ Debugging – Identify and fix bugs quickly
✅ Performance Monitoring – Track slow queries and response times
✅ Security & Auditing – Detect anomalies and unauthorized access
✅ Observability – Gain insights into application behavior
By following best practices, you can avoid log noise and capture meaningful insights.
2. Configuring Rails Logging
Setting Log Levels
Rails provides five log levels:
:debug
– Detailed information for development:info
– General application events:warn
– Potential issues:error
– Application failures:fatal
– Critical failures requiring immediate action
Set the log level in config/environments/production.rb
:
Rails.application.configure do
config.log_level = :info
end
🚀 Best Practice:
✔️ Use :debug
in development for detailed logs
✔️ Use :info
in production for clean logs
✔️ Use :error
or :fatal
for critical alerts
3. Using Structured Logging for Better Insights
Why Structured Logs?
Plain text logs are hard to parse. Structured logs (JSON format) make it easier for log aggregation tools to analyze logs.
Enable JSON Logging in Rails
Install the oj
gem for optimized JSON serialization:
gem install oj
Modify config/environments/production.rb
:
require "oj"
Rails.application.configure do
config.log_formatter = Logger::Formatter.new
logger = ActiveSupport::Logger.new(STDOUT)
logger.formatter = proc do |severity, timestamp, progname, message|
{
level: severity,
time: timestamp.iso8601,
message: message,
pid: Process.pid
}.to_json + "\n"
end
config.logger = logger
end
✅ Now your logs will be structured for better observability!
4. Adding Contextual Metadata to Logs
Include User & Request Information
Enhance logs by adding user details, request IDs, and IP addresses:
class ApplicationController < ActionController::Base
before_action :set_request_log_info
private
def set_request_log_info
Rails.logger.info({
request_id: request.request_id,
user_id: current_user&.id,
ip_address: request.remote_ip,
user_agent: request.user_agent
}.to_json)
end
end
✅ Why? Improves traceability when debugging production issues
5. Centralized Logging with Log Aggregators
Why Centralized Logging?
Local logs get lost when scaling across multiple servers or containers. Use log aggregation tools like:
✔️ Logstash – Collect and parse logs
✔️ Elasticsearch – Index and search logs
✔️ Grafana Loki – Lightweight and scalable log monitoring
✔️ Datadog or New Relic – Full observability suite
Example: Sending Logs to Logstash
Modify config/environments/production.rb
:
require "socket"
Rails.application.configure do
logger = ActiveSupport::Logger.new(TCPSocket.new("logstash-server", 5044))
config.logger = logger
end
✅ Now logs are centralized for better searchability!
6. Handling Sensitive Data in Logs
🚨 NEVER log confidential information!
Rails provides a way to filter sensitive parameters:
Rails.application.configure do
config.filter_parameters += [:password, :credit_card, :api_key]
end
✅ Why? Protects user privacy and ensures security compliance
7. Monitoring Slow Queries & Performance Bottlenecks
Track Slow ActiveRecord Queries
Enable query logging in config/environments/production.rb
:
ActiveRecord::Base.logger = Logger.new(STDOUT)
Find slow queries using:
tail -f log/production.log | grep "ActiveRecord"
🔍 Use Query Monitoring Tools:
✔️ Bullet Gem – Detects N+1 queries
✔️ Rack Mini Profiler – Tracks slow requests
✔️ New Relic APM – Provides real-time insights
8. Log Rotation to Prevent Disk Overload
Large logs can fill up disk space and slow down applications.
Enable log rotation in Rails:
rails log:clear
Or configure logrotate
in production:
/var/www/myapp/log/*.log {
daily
rotate 7
compress
missingok
notifempty
}
✅ Why? Ensures logs don’t consume excessive disk space
Conclusion
By following these Rails logging best practices, you can:
✔️ Improve observability & debugging
✔️ Reduce log noise with structured logs
✔️ Monitor slow queries & errors
✔️ Secure logs by filtering sensitive data
✔️ Scale logging with centralized aggregation
🚀 Next Steps: Integrate logs with Grafana Loki for real-time visualization!