Class: FeldtRuby::Logger
Overview
Simplest possible logger only prints to STDOUT.
Direct Known Subclasses
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
-
#start_time ⇒ Object
readonly
Returns the value of attribute start_time.
Instance Method Summary collapse
-
#add_io(io) ⇒ Object
Add one more io stream to which events are logged.
- #add_output_file(filename) ⇒ Object
-
#elapsed_time(t = Time.now) ⇒ Object
Return the elapsed time since the logger was started.
-
#initialize(io = STDOUT, params = DefaultParams) ⇒ Logger
constructor
A new instance of Logger.
-
#io_puts(message, time = Time.now) ⇒ Object
Puts the given message on the io stream(s) stamped with the given time.
-
#log(message) ⇒ Object
Log a message event.
-
#log_counter(eventType, message = nil) ⇒ Object
Log a counter event, i.e.
-
#log_data(eventType, data, message = nil, tagWithData = false) ⇒ Object
Log a data event.
-
#log_event(eventType, event, message = nil) ⇒ Object
Log the event and print the message, if any.
-
#log_value(eventType, value, message = nil) ⇒ Object
Log a value event.
-
#num_events(eventType = nil) ⇒ Object
Number of events of eventType we have seen so far.
-
#print_frequency=(seconds = 1.0) ⇒ Object
Set the minimum time between printing successive messages of the same type.
- #print_message_if_needed(message, eventType, skipCheck = false) ⇒ Object
-
#setup_data_store ⇒ Object
Set up the internal data store.
- #verbose=(flag) ⇒ Object
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_time ⇒ Object (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 , time = Time.now return unless @verbose elapsed_str = Time.human_readable_timestr elapsed_time(time) s = time.strftime("\n%H:%M.%S (#{elapsed_str}), ") + @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 log_event "___default___", {"d" => {"m" => }}, 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, = nil if log_event eventType, nil, 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, = nil, tagWithData = false if tagWithData dstr = data.keys.map {|k| "#{k}: #{data[k]}"}.join("\n ") += "\n #{dstr}" end log_event eventType, {"d" => data}, 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, = nil @counts[eventType] += 1 if , 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, = nil log_event eventType, {"v" => value}, 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 |
#print_frequency=(seconds = 1.0) ⇒ Object
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 |
#print_message_if_needed(message, eventType, skipCheck = false) ⇒ Object
143 144 145 146 147 148 149 150 151 152 153 154 155 |
# File 'lib/feldtruby/logger.rb', line 143 def , 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 , time @last_time_printed_for_event_type[eventType] = time end end |
#setup_data_store ⇒ Object
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 |