Sha256: 9be1ca3d2c5a3b379b5bc73a5490d3ada08964d2efa9b0bf03ce3f2efbb663d5

Contents?: true

Size: 1.24 KB

Versions: 22

Compression:

Stored size: 1.24 KB

Contents

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.")

Version data entries

22 entries across 20 versions & 6 rubygems

Version Path
logstash-filter-zabbix-0.1.2 vendor/bundle/jruby/1.9/gems/cabin-0.8.1/examples/sample.rb
logstash-filter-zabbix-0.1.1 vendor/bundle/jruby/1.9/gems/cabin-0.8.1/examples/sample.rb
ivanvc-logstash-input-s3-3.1.1.4 vendor/local/gems/cabin-0.8.1/examples/sample.rb
ivanvc-logstash-input-s3-3.1.1.3 vendor/local/gems/cabin-0.8.1/examples/sample.rb
ivanvc-logstash-input-s3-3.1.1.2 vendor/local/gems/cabin-0.8.1/examples/sample.rb
cabin-0.9.0 examples/sample.rb
able-neo4j-1.0.0 vendor/bundle/jruby/1.9/gems/cabin-0.7.1/examples/sample.rb
cabin-0.8.1 examples/sample.rb
cabin-0.8.0 examples/sample.rb
logstash-input-beats-2.0.2 vendor/jruby/1.9/gems/cabin-0.7.2/examples/sample.rb
logstash-input-beats-2.0.2 vendor/jruby/1.9/gems/logstash-codec-json-2.0.3/vendor/gems/cabin-0.7.2/examples/sample.rb
logstash-input-beats-2.0.2 vendor/jruby/1.9/gems/cabin-0.7.1/examples/sample.rb
logstash-codec-json-2.0.3 vendor/gems/cabin-0.7.2/examples/sample.rb
cabin-0.7.2 examples/sample.rb
logstash-input-beats-0.9.2 vendor/jruby/1.9/gems/cabin-0.7.1/examples/sample.rb
logstash-input-beats-0.9.1 vendor/jruby/1.9/gems/cabin-0.7.1/examples/sample.rb
cabin-0.7.1 examples/sample.rb
cabin-0.6.1 examples/sample.rb
cabin-0.6.0 examples/sample.rb
cabin-0.5.0 examples/sample.rb