About

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.

Creating a sample application (Optional)

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.

Adding sample code (Optional)

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.

Setting up the 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'

Adding 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: 'your-logstash-host', 
                                port: your-port,
                                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 are 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: 'your-logstash-host', 
                                        port: your-port,
                                        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

Adding TCP 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: 'your-logstash-host', 
                                port: your-port)

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 are 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 logging now added:

require 'logstash-logger'

class PagesController < ApplicationController
    def home
        message = "Logging test!"
        @greeting = message
               
        tcp_logger = LogStashLogger.new(type: :tcp,
                                        host: 'your-logstash-host',
                                        port: your-port
        )

        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