Ruby on Rails
Ship logs from Ruby on Rails to Logstash
This guide was created using
Ruby On Rails (v 2.2) but you can use any version of ruby that is compatible.
This guide also uses
logstash-logger -v 0.26.1. There are instructions for installing this gem in this guide.
Follow this step by step guide to get 'logs' from your system to Logit.io:
Step 1 - (Optional) Creating an Application
A sample application will be created that will send logs to Logstash.
Create a folder that will host your application on your machine.
Using your CMD prompt or Terminal Editor, enter the following command:
rails new sample_app
The new application, which is called
sample_app, has been created. Navigate to the new application folder.
The final part of the setup is to enter the following command into CMD prompt or Terminal:
This starts the server. Opening a browser and navigating to
http://localhost:3000 will display the Rails splash page, showing that the setup was successful.
Step 2 - (Optional) Adding Sample Code
The next step is to create a controller where the code will be entered for sending a log to Logstash. The following command, will create a controller called
rails generate controller pages
The new controller has been created in the app/controllers folder with the name
pages_controller.rb. Open this file using a text editor, the controller is blank with just the following text contained inside the file.
class PagesController < ApplicationController end
An action called
home should be added to the controller. Some text will be added to ensure that the application is running correctly. The code inside the controller will look as follows:
class PagesController < ApplicationController def home message = "Logging test!" @greeting = message end end
Next a view needs to be created for the action. Copy the file
mailer.text.erb from the app/views/layouts folder into the app/views/pages folder. Rename the copied file to
home.html.erb. Open the copied file with a text editor, remove all existing content and replace with the following code:
<!DOCTYPE html> <html> <h1>Ruby logger test app!</h1> <p><%= @greeting %></p> </html>
home.html.erb file is a simple view that will be displayed after the code in the home method of the pages controller has actioned. A root request must be added to ensure that this happens and is to be added to
routes.rb located in the config folder:
config/routes.rb. Opening the file will reveal that currently there are comments within, any comments should be removed (they start with #) and replaced by the following:
root to: 'pages#home'
A Refresh of the browser (or restart of the server if it was stopped previously, using the command:
rails server) will display the new page.
Step 3 - Setup Logstash Logger Plugin
Using CMD Prompt or Terminal editor, the user must be in the sample_app directory and enter the following command:
gem install logstash-logger -v 0.26.1
There is a file in the top level directory of the application called
Gemfile. When opening this file with a text editor many references to different gems can be seen e.g.
gem 'rails', '~> 5.2.1'
A reference is required for the newly installed
logstash-logger gem and needs to be added, this is done as follows:
# Use logstash-logger gem 'logstash-logger', '~> 0.26.1'
A reference is also required at the very top of the
pages_controller. The code to do this is as follows:
Step 4 - Add TCP-TLS Logging
Step 5 - Add TCP Logging
Step 6 - Check Logit.io for your logs
Now you should view your data:
If you don't see logs take a look at How to diagnose no data in Stack below for how to diagnose common issues.
Step 7 - how to diagnose no data in Stack
If you don't see data appearing in your Stack after following the steps, visit the Help Centre guide for steps to diagnose no data appearing in your Stack or Chat to support now.
Step 8 - Ruby Logging Overview
Ruby is an open sources object oriented programming language created in the mid 90s by Yukihiro Matsumoto. It is used by some of the web’s most popular sites including Shopify, Twitch, Twitter, Airbnb and Github.
Ruby is well known for being easily comprehensible and has a syntax comparable to that of C and Java, it is also equally suited for front end and back end development. Ruby also supports the majority of operating systems including Linux, Windows & Mac.
Ruby log events and errors can often be seen in two common locations, inline with the program’s execution and in separate log files in an output such as /var/log/ruby.log
Ruby log levels include the following five statuses listed in decreasing priority order; FATAL, ERROR, WARN, INFO and DEBUG
For live debugging, being able to see your errors in the program’s execution is useful but for longer term log management an external solution is required for efficient processing, parsing and reporting.
Thanks to our ELK as a Service platform, Logit.io makes parsing and managing your logs from Ruby easy and also provides actionable insights that can be used by your entire engineering and development team. Our platform is able to centralise all of your logs across numerous programming languages, tools, and cloud services that you use daily.
If you need any more assistance in analysing your Ruby logs we're here to help. Feel free to get in touch by reaching out to our support team via Intercom and we will be happy to assist.