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

DigitalOcean Droplet Setup

Postfix-Mailgun-Setup

Certificates/Logstash Setup & Installation:

A word of caution!

Rails Heroku Logger Setup

Gems

Configure Rails App For Heroku

module SslModule    class Context

def self.certificates_path
Rails.root.join("config","certificates","certificates")
end
def self.get_context
ssl_context = OpenSSL::SSL::SSLContext.new()
ssl_context.cert = OpenSSL::X509::Certificate.new(File.open("# {certificates_path}/ssl_certificates/host/host.crt"))
ssl_context.ca_file = "#{certificates_path}/ssl_certificates/ca/ca.crt"
ssl_context.key = OpenSSL::PKey::RSA.new(File.open("#{certificates_path}/ssl_certificates/host/host.key"))
ssl_context.verify_mode = OpenSSL::SSL::VERIFY_PEER
ssl_context.ssl_version = :SSLv23
ssl_context
end
def self.get_machine_ip
ip = Socket.ip_address_list.detect{|intf| intf.ipv4_private?}
ip.ip_address
end
def self.get_logstash_ip
machine_ip = get_machine_ip
#puts "machine ip is :#{machine_ip}"
#puts "ip address is #{ENV['IP_ADDRESS']}"
return ENV["LOGSTASH_SERVER_IP_ADDRESS"] unless ENV["LOGSTASH_SERVER_IP_ADDRESS"].blank?
unless ENV["LOGSTASH_SERVER_IP_ADDRESSES"].blank?
ENV["LOGSTASH_SERVER_IP_ADDRESSES"].split(",").each do |ip|
if machine_ip == ip
return ip
end
end
end
return "0.0.0.0"
end
##should check eth for the ip address or hit 0.0.0.0
## if more than one ip address is specified.
## if no IP_ADDRESS is specified, defaults to eth / or 0.0.0.0
def self.logstash_logger
ls_ip = get_logstash_ip
#puts "ls ip is #{ls_ip}"
LogStashLogger.new(type: :tcp, host: get_logstash_ip, port: ENV["LOGSTASH_PORT"], ssl_context: get_context)
end
end
end
#logger.rb
Rails.logger = ::SslModule::Context.logstash_logger
Delayed::Worker.logger = Rails.logger
#development.rb
Rails.application.configure do
config.use_logstash = false
config.lograge.enabled = false

config.lograge.logger = Rails.logger
config.lograge.formatter = Lograge::Formatters::Logstash.new
config.log_tags = [:uuid]
config.lograge.custom_payload do |controller|
{
host: controller.request.host,
user_id: controller.current_user.try(:id),
request_id: controller.request.request_id || RequestStore.store[:request_id],
params: controller.request.params.to_s
}
end
end

--

--

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