Module: LogBox

Defined in:
lib/log_box.rb,
lib/log_box/version.rb

Defined Under Namespace

Classes: Configuration

Constant Summary collapse

DEFAULT_TAG =
:thread
VERSION =
"0.0.3"

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.configurationObject

Returns the value of attribute configuration.



17
18
19
# File 'lib/log_box.rb', line 17

def configuration
  @configuration
end

Class Method Details

.configure {|configuration| ... } ⇒ Object

Yields:



20
21
22
23
# File 'lib/log_box.rb', line 20

def self.configure
  self.configuration ||= Configuration.new
  yield(configuration)
end

.discard(tag = nil) ⇒ Object



92
93
94
95
96
97
# File 'lib/log_box.rb', line 92

def self.discard(tag = nil)
  return unless logger

  tag ||= default_tag
  log_box.delete tag
end

.displayObject



99
100
101
# File 'lib/log_box.rb', line 99

def self.display
  pp log_box
end

.flush(options = {}) ⇒ Object

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

}



82
83
84
85
86
87
88
89
90
# 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 = {}) ⇒ Object

Following hash is stored into Thread.current

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

:thread=>
 [15:38:21 -0800, :log=>"Hello",
  15:38:23 -0800, :log=>"Hello2"]

}



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# 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_boxObject



103
104
105
# File 'lib/log_box.rb', line 103

def self.log_box
  Thread.current[:_log_box]
end