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, tag_format: '[%{time} %{level}] ', time_format: '%Y/%m/%d %H:%M:%S', tag_block: nil) ⇒ LoGspot
constructor
A new instance of LoGspot.
- #raw(message) ⇒ Object
- #tagged(tag, &block) ⇒ Object
- #tagged_list(tag, *args, &block) ⇒ Object
- #top(&block) ⇒ Object
- #untagged(&block) ⇒ Object
- #value(level, v, options = {}) ⇒ Object
Constructor Details
#initialize(file_or_file_name = STDOUT, wrapper: nil, tag_format: '[%{time} %{level}] ', time_format: '%Y/%m/%d %H:%M:%S', tag_block: 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, tag_format: '[%{time} %{level}] ', time_format: '%Y/%m/%d %H:%M:%S', tag_block: nil) wrapper = ->(output, data) { base = tag_block ? tag_block.(Time.current, level) : tag_format % { time: Time.current.strftime(time_format), level: level } if data[:space] base = ' ' * base.length end output.puts(message: "#{base}#{data[:message]}") } @raw_output = @file = Output::File.new(file_or_file_name) @top_output = @output = Output::Wrap.new(wrapper, @file) @level = nil end |
Instance Method Details
#finalize ⇒ Object
100 101 102 |
# File 'lib/logspot.rb', line 100 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 |
#raw(message) ⇒ Object
90 91 92 |
# File 'lib/logspot.rb', line 90 def raw() raw_output.write() 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}#{}", space: false)) first = false else output.puts(data.merge(message: "#{base}#{}", space: true)) end end end |
#top(&block) ⇒ Object
84 85 86 87 88 |
# File 'lib/logspot.rb', line 84 def top(&block) previous_output, @output = output, top_output block.call @output = previous_output end |
#untagged(&block) ⇒ Object
78 79 80 81 82 |
# File 'lib/logspot.rb', line 78 def untagged(&block) previous_output, @output = output, output.inner_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 |