Skip to content

Instantly share code, notes, and snippets.

@mikechau
Last active November 2, 2023 23:53
Show Gist options
  • Star 19 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
  • Save mikechau/c08555f8a3dbec5393d8 to your computer and use it in GitHub Desktop.
Save mikechau/c08555f8a3dbec5393d8 to your computer and use it in GitHub Desktop.
Logging Rails 4 to Syslog, formatted w/ Logstash

Logging Rails 4 to Syslog, formatted w/ Logstash

Getting Started

Steps to get Rails 4 saving its output to Syslog via Rsyslog. This assumes you are on CentOS, but should be pretty adaptable to any other distribution. Ruby 2.0+ is also required.

  1. Add the gems lograge and logstash-event to your Gemfile. Feel free to remove from production if you'd like to test it in development as well or something.
  2. Update production.rb with the lograge settings and set the logger to Syslog::Logger.
  3. Add the conf files to /etc/rsyslog.d/. /etc/rsyslog.conf should have $IncludeConfig /etc/rsyslog.d/*.conf enabled, so it will load any additional configs from /etc/rsyslog.conf.

Sources

# Gemfile
group :production do
# LOGGING
gem 'lograge'
gem 'logstash-event'
end
# /etc/rsyslog.d/my-app-name.conf
!my-app-name
*.* /var/log/my-app-name.log
require 'syslog/logger'
Rails.application.configure do
config.lograge.enabled = true
config.lograge.formatter = Lograge::Formatters::Logstash.new
config.logger = Syslog::Logger.new('my-app-name')
end
# /etc/rsyslog.d/settings.conf
$MaxMessageSize 5m
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment