Setup Logstash on DigitalOcean with a Rails App hosted on Heroku : Full SSL

Logstash is a part of the ELK (Elasticsearch — Kibana — Logstash) stack, from Elastic. Its a centralized log drain for all your logs from various sources.

I couldn’t find any nice tutorials on setting up Logstash on a DigitalOcean droplet, and sending logs to it from a Heroku Rails App, over TCP with SSL. Certificate creation also seems poorly documented online.

So let’s get to it.

DigitalOcean Droplet Setup

Use this Github Repo to setup basic packages.


The tutorial is pretty good, provided you already have mailgun configured.

Certificates/Logstash Setup & Installation:

A word of caution!

I spent nearly 6–8 hours on all this and eventually realized that there is no simple tool to automate the process. (6–8 hours is a long time for me)

I created a Github Repository, that you can find below, that does all the heavy lifting for you. I would strongly suggest you use it, rather than tinker with OpenSSL on your own.

More importantly, I’ve included a simple sh script to check whether Logstash responds to your client over SSL. It took me a long time to figure out how to even test the SSL connection.

Full instructions on my Github Repo.

Rails Heroku Logger Setup


gem “lograge”
gem ‘logstash-event’
gem ‘logstash-logger’

Configure Rails App For Heroku

I’m outlining my configuration, but it can get messy, primarily because I used Rails.logger.(debug/error) all throughout my application. So it became necessary to extend Rails.logger to output its stuff to Logstash in a json friendly format.

Create a module in your /lib directory:

This simplifies setting up the SSL Connection.

module SslModule    class Context

def self.certificates_path
def self.get_context
ssl_context =
ssl_context.cert ="# {certificates_path}/ssl_certificates/host/host.crt"))
ssl_context.ca_file = "#{certificates_path}/ssl_certificates/ca/ca.crt"
ssl_context.key ="#{certificates_path}/ssl_certificates/host/host.key"))
ssl_context.verify_mode = OpenSSL::SSL::VERIFY_PEER
ssl_context.ssl_version = :SSLv23
def self.get_machine_ip
ip = Socket.ip_address_list.detect{|intf| intf.ipv4_private?}
def self.get_logstash_ip
machine_ip = get_machine_ip
#puts "machine ip is :#{machine_ip}"
#puts "ip address is #{ENV['IP_ADDRESS']}"
ENV["LOGSTASH_SERVER_IP_ADDRESSES"].split(",").each do |ip|
if machine_ip == ip
return ip
return ""
##should check eth for the ip address or hit
## if more than one ip address is specified.
## if no IP_ADDRESS is specified, defaults to eth / or
def self.logstash_logger
ls_ip = get_logstash_ip
#puts "ls ip is #{ls_ip}" :tcp, host: get_logstash_ip, port: ENV["LOGSTASH_PORT"], ssl_context: get_context)

Create an initializer:

Rails.logger = ::SslModule::Context.logstash_logger
Delayed::Worker.logger = Rails.logger

Add the following to your environment files:

Rails.application.configure do
config.use_logstash = false
config.lograge.enabled = false

config.lograge.logger = Rails.logger
config.lograge.formatter =
config.log_tags = [:uuid]
config.lograge.custom_payload do |controller|
user_id: controller.current_user.try(:id),
request_id: controller.request.request_id ||[:request_id],
params: controller.request.params.to_s

This should get everything working, and you should see the logs popping up in your logstash server.

Comment if you face any problems!

All the best, this is not easy.

Post-Graduate in Clinical Pathology, Lab Director - Pathofast, Computer Vision Enthusiast, Founder

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store