Ruby on Rails
Ship logs from Ruby on Rails to Logstash
Follow the steps below to send your observability data to Logit.io
Logs
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.
Install Integration
(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:
rails server
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.
(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 pages
.
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>
The 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.
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:
require 'logstash-logger'
Add TCP-TLS Logging
The following code needs to be added to the home
method in pages_controller
, this code does the actual logging:
tcp_logger = LogStashLogger.new(
type: :tcp,
host: '@logstash.host',
port: "@logstash.sslPort:strip_quotes",
ssl_enable: true)
file_logger = LogStashLogger.new(
type: :file,
path: 'log/logstashtest.log',
sync: true)
file_logger.error message
tcp_logger.error message
The host value in the example above should be the Logstash endpoint and the port is the Port Number of the Stack where the logging data is required to be sent to. They are found in the Logstash Inputs area of Logstash.
This code sets up an instance of the logstash server logger and also a
logstash file logger (which writes to a file called logstashtest.log
).
The message
variable, which is set earlier in the code, is sent to
both the server and the file as an error message.
Below is the full code for the pages_controller
with the TCP-TLS logging now added:
require 'logstash-logger'
class PagesController < ApplicationController
def home
message = "Logging test!"
@greeting = message
tcp_logger = LogStashLogger.new(
type: :tcp,
host: '@logstash.host',
port: "@logstash.sslPort:strip_quotes",
ssl_enable: true
)
file_logger = LogStashLogger.new(
type: :file,
path: 'log/logstashtest.log',
sync: true)
file_logger.error message
tcp_logger.error message
end
end
The user needs to start the server ($ rails server
) and navigate to
http://localhost:3000
, a log is written to the logstashtest.log
file
and the stack on the server (which can be viewed using Kibana). The
logstashtest.log
file is found in the sample_app\log
folder.
Check Logit.io for your logs
Data should now have been sent to your Stack.
View My DataIf you don't see take a look at How to diagnose no data in Stack below for how to diagnose common issues.
How to diagnose no data in Stack
If you don't see data appearing in your stack after following this integration, take a look at the troubleshooting guide for steps to diagnose and resolve the problem or contact our support team and we'll be happy to assist.
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 frontend and backend 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 (opens in a new tab), 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.