Class: FeldtRuby::Logger

Inherits:
Object show all
Defined in:
lib/feldtruby/logger.rb

Overview

Simplest possible logger only prints to STDOUT.

Direct Known Subclasses

MongoDBLogger

Constant Summary collapse

DefaultParams =
{
  :verbose => false,
  :printFrequency => 0.3  # Minimum seconds between consecutive messages printed for the same event type
}
UnixEpoch =
Time.at(0)

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(io = STDOUT, params = DefaultParams) ⇒ Logger

Returns a new instance of Logger.



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/feldtruby/logger.rb', line 19

def initialize(io = STDOUT, params = DefaultParams)

  @start_time = Time.now

  @params = DefaultParams.clone.update(params)

  self.verbose = @params[:verbose]

  self.print_frequency = @params[:printFrequency]

  @ios = []

  add_io io

  setup_data_store

  @last_time_printed_for_event_type = Hash.new(UnixEpoch)

end

Instance Attribute Details

#start_timeObject (readonly)

Returns the value of attribute start_time.



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

def start_time
  @start_time
end

Instance Method Details

#add_io(io) ⇒ Object

Add one more io stream to which events are logged.



69
70
71
72
# File 'lib/feldtruby/logger.rb', line 69

def add_io io
  @ios << io
  @ios.uniq
end

#add_output_file(filename) ⇒ Object



74
75
76
77
78
79
80
81
# File 'lib/feldtruby/logger.rb', line 74

def add_output_file(filename)
  @output_ios ||= []
  @output_ios << File.open(filename, "w")
  add_io @output_ios.last
  ObjectSpace.define_finalizer(self) do
    @output_ios.each {|fh| fh.close}
  end
end

#elapsed_time(t = Time.now) ⇒ Object

Return the elapsed time since the logger was started.



55
56
57
# File 'lib/feldtruby/logger.rb', line 55

def elapsed_time t = Time.now
  t - @start_time
end

#io_puts(message, time = Time.now) ⇒ Object

Puts the given message on the io stream(s) stamped with the given time.



158
159
160
161
162
163
164
165
166
167
168
# File 'lib/feldtruby/logger.rb', line 158

def io_puts message, time = Time.now

  return unless @verbose

  elapsed_str = Time.human_readable_timestr elapsed_time(time)

  s = time.strftime("\n%H:%M.%S (#{elapsed_str}), ") + message

  @ios.each {|io| io.puts s}

end

#log(message) ⇒ Object

Log a message event.



125
126
127
# File 'lib/feldtruby/logger.rb', line 125

def log message
  log_event "___default___", {"d" => {"m" => message}}, message
end

#log_counter(eventType, message = nil) ⇒ Object

Log a counter event, i.e. update the (local) count of how many times this event has happened.



101
102
103
104
105
106
107
108
# File 'lib/feldtruby/logger.rb', line 101

def log_counter eventType, message = nil
  if message
    log_event eventType, nil, message
  else
    # We count it even if should not log it
    @counts[eventType] += 1
  end
end

#log_data(eventType, data, message = nil, tagWithData = false) ⇒ Object

Log a data event.



116
117
118
119
120
121
122
# File 'lib/feldtruby/logger.rb', line 116

def log_data eventType, data, message = nil, tagWithData = false
  if tagWithData
    dstr = data.keys.map {|k| "#{k}: #{data[k]}"}.join("\n  ")
    message += "\n  #{dstr}"
  end
  log_event eventType, {"d" => data}, message
end

#log_event(eventType, event, message = nil) ⇒ Object

Log the event and print the message, if any. This simplest logger only prints, it never saves the event.



131
132
133
134
135
136
137
138
139
140
141
# File 'lib/feldtruby/logger.rb', line 131

def log_event eventType, event, message = nil

  @counts[eventType] += 1

  if message
    print_message_if_needed message, eventType, (eventType == "___default___")
  end

  event

end

#log_value(eventType, value, message = nil) ⇒ Object

Log a value event.



111
112
113
# File 'lib/feldtruby/logger.rb', line 111

def log_value eventType, value, message = nil
  log_event eventType, {"v" => value}, message
end

#num_events(eventType = nil) ⇒ Object

Number of events of eventType we have seen so far.



46
47
48
49
50
51
52
# File 'lib/feldtruby/logger.rb', line 46

def num_events eventType = nil
  if eventType == nil
    @counts.values.sum
  else
    @counts[eventType]
  end
end

Set the minimum time between printing successive messages of the same type.



64
65
66
# File 'lib/feldtruby/logger.rb', line 64

def print_frequency=(seconds = 1.0)
  @print_frequency = @params[:printFrequency] = seconds
end


143
144
145
146
147
148
149
150
151
152
153
154
155
# File 'lib/feldtruby/logger.rb', line 143

def print_message_if_needed message, eventType, skipCheck = false
  time = Time.now.utc

  # We only print if enough time since last time we printed. This way
  # we avoid "flooding" the user with log messages of the same type.
  if skipCheck || (time - @last_time_printed_for_event_type[eventType]) >= @print_frequency

    io_puts message, time

    @last_time_printed_for_event_type[eventType] = time

  end
end

#setup_data_storeObject

Set up the internal data store.



40
41
42
43
# File 'lib/feldtruby/logger.rb', line 40

def setup_data_store
  # Nothing is saved by this simplest Logger, we just count them
  @counts = Hash.new(0)
end

#verbose=(flag) ⇒ Object



59
60
61
# File 'lib/feldtruby/logger.rb', line 59

def verbose=(flag)
  @verbose = @params[:verbose] = flag
end