Learn more  » Push, build, and install  RubyGems npm packages Python packages Maven artifacts PHP packages Go Modules Bower components Debian packages RPM packages NuGet packages

vistahigherlearning / logstash   deb

Repository URL to install this package:

/ opt / logstash / vendor / bundle / jruby / 1.9 / gems / cabin-0.6.1 / examples / sample.rb

require "rubygems"
require "cabin"

# Logging::... is something I'm implemented and experimenting with.
@logger = Cabin::Channel.new

# A logging channel can have any number of subscribers.
# Any subscriber is simply expected to respond to '<<' and take a single
# argument (the event)
# Special case handling of stdlib Logger and IO objects comes for free, though.
@logger.subscribe(STDOUT)

# You can store arbitrary key-value pairs in the logging channel. 
# These are emitted with every event.
@logger[:program] = "sample program"

def foo(val)
  # A context is something that lets you modify key-value pieces in the
  # logging channel and gives you a trivial way to undo the changes later.
  context = @logger.context()
  context[:foo] = val
  context[:example] = 100

  # The point of the context above is to save context so that the bar() method 
  # and it's logging efforts can include said context.
  timer = @logger.time("Timing bar")
  bar()
  timer.stop   # logs the result.

  @logger.time("Another bar timer") do
    bar()
  end

  # Clearing this context will exactly undo the changes made to the logger by
  # this context.
  context.clear()
end

def bar
  @logger.info("bar bar bar!")
  sleep(rand * 2)
end

foo("Hello")
@logger.info("All done.")