Skip to content

Instantly share code, notes, and snippets.

@botanicus
Last active February 12, 2016 13:28
Show Gist options
  • Save botanicus/d8b0206249eae7f66921 to your computer and use it in GitHub Desktop.
Save botanicus/d8b0206249eae7f66921 to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby
require 'elasticsearch'
require 'time' # To get Time#iso8601.
#### Testing in development ###
#
# Install & configure:
#
# $ brew install elasticsearch logstash kibana
# $ vim /usr/local/etc/elasticsearch/elasticsearch.yml [1]
# $ mkdir -p /usr/local/logstash/patterns.d/
# $ cat > /usr/local/etc/logstash/logstash.conf [1]
#
# Run it:
#
# $ elasticsearch
# $ kibana # http://localhost:5601/
# $ logstash agent -f /usr/local/etc/logstash/logstash.conf
#
# Miscelaneous:
#
# - Set $DEBUG to true (ruby -d elk_integration_poc.rb).
#
# [1] Based on http://www.websightdesigns.com/wiki/ELK_Stack_on_Mac_OS_X_Yosemite
class ELKLogger
@@elasticsearch_config = {log: true}
def initialize(index_format)
@index_format = index_format
end
def info(message)
log(:info, message)
end
protected
def log(level, message)
payload = payload(level, message)
puts "~ Sending #{payload} to #{index_name}." if $DEBUG
elasticsearch_client.index(index: index_name, type: true, body: payload)
end
def payload(level, message)
{level: level, message: message, :@timestamp => timestamp}
end
# Everything must be UTC.
def timestamp
Time.now.utc.iso8601
end
def index_name
# Nick: This is the most common format so keep it
# like this unless there is a reason to change it.
timestamp = Time.now.utc.strftime("%Y.%m.%d")
@index_format % {timestamp: timestamp}
end
def elasticsearch_client
@client ||= Elasticsearch::Client.new(@@elasticsearch_config)
end
end
# Ruby Core.
logger = ELKLogger.new("ruby-core-%{timestamp}")
logger.info("Hello world!")
# Jobs.
logger = ELKLogger.new("ruby-worker-airbrake-%{timestamp}")
logger.info("Hello world!")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment