module LogBox

Constants

DEFAULT_TAG
VERSION

Attributes

configuration[RW]

Public Class Methods

configure() { |configuration| ... } click to toggle source
# File lib/log_box.rb, line 20
def self.configure
  self.configuration ||= Configuration.new
  yield(configuration)
end
discard(tag = nil) click to toggle source
# File lib/log_box.rb, line 92
def self.discard(tag = nil)
  return unless logger

  tag ||= default_tag
  log_box.delete tag
end
display() click to toggle source
# File lib/log_box.rb, line 99
def self.display
  pp log_box
end
flush(options = {}) click to toggle source

Following log is stored into fluentd: {

"_id" : ObjectId("52c4a1f4e1eef37b9900001a"),
"tag" : "thread",
"logs" : [
    {
        "time" : "2014-01-01 15:16:43 -0800",
        "log" : "Hi-ho",
    }
],
"time" : ISODate("2014-01-01T23:17:01.000Z")

}

# File lib/log_box.rb, line 82
def self.flush(options = {})
  return unless logger

  o = { tag: default_tag }.merge(options).symbolize_keys
  tag = o[:tag]
  o[:logs] = log_box[tag]
  flush_to_fluentd o
  discard tag
end
log(obj, options = {}) click to toggle source

Following hash is stored into Thread.current {

:new_tag =>
[{:time=>2014-01-01 15:38:15 -0800, :log=>"Hi-ho", :priority=>3}],

:thread=>
 [{:time=>2014-01-01 15:38:21 -0800, :log=>"Hello"},
  {:time=>2014-01-01 15:38:23 -0800, :log=>"Hello2"}]

}

# File lib/log_box.rb, line 45
  def self.log(obj, options = {})
    return unless logger

    o = { tag: default_tag,
      time: current_time,
      log: obj.is_a?(String) ? obj : obj.inspect
    }.merge(options).symbolize_keys

=begin
    o = { tag: default_tag, time: current_time }.merge(options).symbolize_keys
    if obj.is_a?(String)
      o[:log] = obj
    elsif obj.class < ActiveRecord::Base
      o = o.merge(class: obj.class.to_s).merge(obj.attributes)
    elsif obj.is_a?(Hash)
      o.merge!(obj)
    else
      o[:log] = obj.inspect
    end
=end
    tag = o.delete :tag
    init_log_box_tag_if_not tag
    log_box[tag] << o
  end
log_box() click to toggle source
# File lib/log_box.rb, line 103
def self.log_box
  Thread.current[:_log_box]
end

Private Class Methods

current_time() click to toggle source
# File lib/log_box.rb, line 118
def self.current_time
  Time.now
end
default_tag() click to toggle source
# File lib/log_box.rb, line 110
def self.default_tag
  self.configuration.default_tag || :thread
end
flush_to_fluentd(result) click to toggle source
# File lib/log_box.rb, line 141
def self.flush_to_fluentd(result)
  logger.post 'log_box', result if logger
end
init_log_box() click to toggle source
# File lib/log_box.rb, line 122
def self.init_log_box
  Thread.current[:_log_box] = {}  # key: tag, value: Array of Hash
end
init_log_box_if_not() click to toggle source
# File lib/log_box.rb, line 126
def self.init_log_box_if_not
  init_log_box if log_box.nil?
end
init_log_box_tag(tag = nil) click to toggle source
# File lib/log_box.rb, line 136
def self.init_log_box_tag(tag = nil)
  tag ||= default_tag
  log_box[tag] = []
end
init_log_box_tag_if_not(tag = nil) click to toggle source
# File lib/log_box.rb, line 130
def self.init_log_box_tag_if_not(tag = nil)
  tag ||= default_tag
  init_log_box_if_not
  log_box[tag] ||= []
end
logger() click to toggle source
# File lib/log_box.rb, line 114
def self.logger
  self.configuration.logger
end