Skip to content

Instantly share code, notes, and snippets.

@shah743
Forked from mikechau/00_README.md
Created November 20, 2020 19:28
Show Gist options
  • Save shah743/b21b9a541de2592e77de2e13b7a90166 to your computer and use it in GitHub Desktop.
Save shah743/b21b9a541de2592e77de2e13b7a90166 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