You can send your Rails logs to Loggly over syslog. We’re using the lograge gem because it offers better formatting of logs, but it’s not required. We will automatically parse JSON and specific types of rails logs. For alternatives, please see the Advanced Options section below.
Rails Logging Setup
- Configure syslog daemon
Run our automatic configure-linux script below to setup Rails logging and send the logs to Loggly through your syslog daemon.
curl -O https://www.loggly.com/install/configure-linux.sh sudo bash configure-linux.sh -a SUBDOMAIN -u USERNAME
- SUBDOMAIN: your account subdomain that you created when you signed up for Loggly
- USERNAME: your Loggly username
- Configure rsyslog to send Rails app logs to Loggly
Open your ruby configuration file for rsyslog or create a new one:
sudo vim /etc/rsyslog.d/21-rails.conf
Paste this configuration into the file then save it.
#provides UDP syslog reception $ModLoad imudp $UDPServerRun 514 #Add a tag for Rails events $template LogglyFormatRails,"<%pri%>%protocol-version% %timestamp:::date-rfc3339% %HOSTNAME% %app-name% %procid% %msgid% [TOKEN@41058 tag=\"Rails\"] %msg%\n" local7.* @@logs-01.loggly.com:514;LogglyFormatRails local7.* ~ #discards the messages so they don't end up in /var/log/syslog
- TOKEN: your customer token from the source setup page
Then restart rsyslog so the changes take effect
sudo service rsyslog restart
- Configure Rails application to send Logs
Open Gemfile in your application. It should be present in the root folder of your application.
Add the following statements to the Gemfile to include the syslogger and lograge gems in your application
gem 'syslogger', '~> 1.6.0' gem 'lograge','~> 0.3.1'
Install these gems in RVM using the following command
Add the following code in your <environment>.rb config file. You can find it in the config/environments folder present in your application directory structure. You can select .rb file on the basis of your current environment setup. It can be development.rb, test.rb or production.rb
require 'syslogger' config.logger = Syslogger.new("APPNAME", Syslog::LOG_PID, Syslog::LOG_LOCAL7) config.lograge.enabled = true config.lograge.formatter = Lograge::Formatters::Json.new
- APPNAME: your application name
- Send a Test Event
Now use the logger in your application.
logger.info "Hello from Rails!"
Restart your rails application and visit a page so that it starts sending events to Loggly.
- Verify Events
Search Loggly for events with the Rails tag over the past 20 minutes. It may take a few minutes to index the event. If it doesn’t work, see the troubleshooting section below.
Click on one of the logs to show a list of Rails fields (see screenshot below). If you don’t see them, please check that you are using one of our automatically parsed formats.
Advanced Rails Logging Options
- Syslogger Gem – Rails library for sending application logs to syslog
- Logglier Rails Gem – A ruby gem to send logs over HTTP/S
- Automated Parsing – Describes which types of rails logs we parse automatically
- Lograge – A gem to format rails logs more nicely as JSON
- Loggly Libraries Catalog – New libraries are added to our catalog
- Search or post your own Rails logging questions in the community forum.
Troubleshooting Rails Logs
If you don’t see any data show up in the verification step, then check for these common problems.
Check syslogger configuration:
- Wait a few minutes in case indexing needs to catch up
- Check to see if the 01-ruby-logger.conf is properly created and placed in the proper location. It should be in the /etc/rsyslog.d folder.
- Check to UDP reception is enabled on port 514 in /etc/rsyslog.conf file.
- See our HTTP Troubleshooting Guide to verify HTTP events are being sent to Loggly.
- See our Rsyslog Troubleshooting Guide if the files are not being sent to Loggly
Still Not Working?
- Search or post your own Rails log questions in the community forum.