Sha256: 3eb9d2dba84dec49b0b3f166f4ede9f85026bdcaf039dea5da4a6ced2a96340e

Contents?: true

Size: 994 Bytes

Versions: 6

Compression:

Stored size: 994 Bytes

Contents

require "cabin"
require "json"

# Wrap Ruby stdlib's logger. This allows you to output to a normal ruby logger
# with Cabin. Since Ruby's Logger has a love for strings alone, this 
# wrapper will convert the data/event to json before sending it to Logger.
class Cabin::Outputs::StdlibLogger
  public
  def initialize(logger)
    @logger = logger
    @logger.level = logger.class::DEBUG
  end # def initialize

  # Receive an event
  public
  def <<(event)
    if !event.include?(:level)
      event[:level] = :info
    end
    method = event[:level].downcase.to_sym || :info
    event.delete(:level)

    data = event.clone
    # delete things from the 'data' portion that's not really data.
    data.delete(:message)
    data.delete(:timestamp)
    message = "#{event[:message]} #{data.to_json}"

    #p [@logger.level, logger.class::DEBUG]
    # This will call @logger.info(data) or something similar.
    @logger.send(method, message)
  end # def <<
end # class Cabin::Outputs::StdlibLogger

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
cabin-0.4.4 lib/cabin/outputs/stdlib-logger.rb
cabin-0.4.3 lib/cabin/outputs/stdlib-logger.rb
cabin-0.4.2 lib/cabin/outputs/stdlib-logger.rb
cabin-0.4.1 lib/cabin/outputs/stdlib-logger.rb
cabin-0.3.8 lib/cabin/outputs/stdlib-logger.rb
cabin-0.3.7 lib/cabin/outputs/stdlib-logger.rb