Sha256: cf8acb484a6b74eaa626f8025c837d7ccb8748816310f197708a675cf15ca4dc

Contents?: true

Size: 1.32 KB

Versions: 19

Compression:

Stored size: 1.32 KB

Contents

require "rubygems"
require "cabin"
require "logger"

# 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 of stdlib Logger instances that are wrapped smartly to 
# log JSON and call the right Logger method (Logger#info, etc).
@logger.subscribe(Logger.new(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.")

Version data entries

19 entries across 19 versions & 1 rubygems

Version Path
cabin-0.4.2 examples/sample.rb
cabin-0.4.1 examples/sample.rb
cabin-0.3.8 examples/sample.rb
cabin-0.3.7 examples/sample.rb
cabin-0.3.6 examples/sample.rb
cabin-0.3.2 examples/sample.rb
cabin-0.3.1 examples/sample.rb
cabin-0.3.0 examples/sample.rb
cabin-0.2.3 examples/sample.rb
cabin-0.2.2 examples/sample.rb
cabin-0.2.1 examples/sample.rb
cabin-0.1.8 examples/sample.rb
cabin-0.1.7 examples/sample.rb
cabin-0.1.6 examples/sample.rb
cabin-0.1.5 examples/sample.rb
cabin-0.1.3 examples/sample.rb
cabin-0.1.2 examples/sample.rb
cabin-0.1.1 examples/sample.rb
cabin-0.1.0 examples/sample.rb