Class: LoGspot
- Inherits:
-
Object
- Object
- LoGspot
- Defined in:
- lib/logspot.rb
Constant Summary collapse
- LOG_LEVELS =
%w(DEBUG INFO WARN ERROR FATAL)
Instance Method Summary collapse
- #finalize ⇒ Object
- #hash(h, &block) ⇒ Object
-
#initialize(file_or_file_name = STDOUT, wrapper = nil) ⇒ LoGspot
constructor
A new instance of LoGspot.
- #tagged(tag, &block) ⇒ Object
- #tagged_list(tag, *args, &block) ⇒ Object
- #untagged(&block) ⇒ Object
- #value(level, v, options = {}) ⇒ Object
Constructor Details
#initialize(file_or_file_name = STDOUT, wrapper = nil) ⇒ LoGspot
Returns a new instance of LoGspot.
6 7 8 9 10 11 12 13 14 15 16 17 |
# File 'lib/logspot.rb', line 6 def initialize(file_or_file_name = STDOUT, wrapper = nil) wrapper = ->(output, data) { base = "[#{Time.now.strftime('%Y/%m/%d %H:%M:%S')} #{level}] " if data[:space] base = ' ' * base.length end output.puts(message: "#{base}#{data[:message]}") } @file = Output::File.new(file_or_file_name) @output = @original_output = Output::Wrap.new(wrapper, @file) @level = nil end |
Instance Method Details
#finalize ⇒ Object
90 91 92 |
# File 'lib/logspot.rb', line 90 def finalize @file.finalize end |
#hash(h, &block) ⇒ Object
46 47 48 49 50 51 |
# File 'lib/logspot.rb', line 46 def hash(h, &block) len = (h.keys.map(&:length).max || 0) + 2 h.each do |key, value| tagged_list("#{key}: ".ljust(len), key, value, &block) end end |
#tagged(tag, &block) ⇒ Object
19 20 21 22 23 24 25 26 27 |
# File 'lib/logspot.rb', line 19 def tagged(tag, &block) wrap_output(block) do |output, data| base = tag if space = !!data[:space] base = ' ' * base.length end output.puts(data.merge(message: "#{base}#{data[:message]}", space: space)) end end |
#tagged_list(tag, *args, &block) ⇒ Object
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/logspot.rb', line 29 def tagged_list(tag, *args, &block) first = true wrap_output(block, *args) do |output, data| = data[:message] base = tag if data[:space] || !first base = ' ' * base.length end if first output.puts(data.merge(message: "#{base}#{message}", space: false)) first = false else output.puts(data.merge(message: "#{base}#{message}", space: true)) end end end |
#untagged(&block) ⇒ Object
78 79 80 81 82 |
# File 'lib/logspot.rb', line 78 def untagged(&block) previous_output, @output = output, original_output block.call @output = previous_output end |
#value(level, v, options = {}) ⇒ Object
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 |
# File 'lib/logspot.rb', line 53 def value(level, v, = {}) if v.is_a?(Hash) hash(v) do |k, v| value(level, v, ) end elsif v.is_a?(Array) h = Hash[v.map.with_index do |v, i| [i.to_s, v] end] value(level, h, ) else if p = [:split_proc] p.call(v.to_s).each do |line| send(level, line) end elsif max = [:max_columns] v.to_s.split('').each_slice(max).map { |x| x.join('') }.each do |line| send(level, line) end else send(level, v) end end end |