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 |
# 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, data[:level]) : tag_format % { time: Time.current.strftime(time_format), level: data[:level] } if data[:space] base = ' ' * uncolorize_str(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) end |
Instance Method Details
#finalize ⇒ Object
99 100 101 |
# File 'lib/logspot.rb', line 99 def finalize @file.finalize end |
#hash(h, &block) ⇒ Object
45 46 47 48 49 50 |
# File 'lib/logspot.rb', line 45 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
89 90 91 |
# File 'lib/logspot.rb', line 89 def raw() raw_output.write() end |
#tagged(tag, &block) ⇒ Object
18 19 20 21 22 23 24 25 26 |
# File 'lib/logspot.rb', line 18 def tagged(tag, &block) wrap_output(block) do |output, data| base = tag if space = data[:space] base = ' ' * uncolorize_str(base).length end output.puts(data.merge(message: "#{base}#{data[:message]}", space: space)) end end |
#tagged_list(tag, *args, &block) ⇒ Object
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/logspot.rb', line 28 def tagged_list(tag, *args, &block) first = true wrap_output(block, *args) do |output, data| = data[:message] base = tag if data[:space] || !first base = ' ' * uncolorize_str(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 |
#top(&block) ⇒ Object
83 84 85 86 87 |
# File 'lib/logspot.rb', line 83 def top(&block) previous_output, @output = output, top_output block.call @output = previous_output end |
#untagged(&block) ⇒ Object
77 78 79 80 81 |
# File 'lib/logspot.rb', line 77 def untagged(&block) previous_output, @output = output, output.inner_output block.call @output = previous_output end |
#value(level, v, options = {}) ⇒ Object
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 |
# File 'lib/logspot.rb', line 52 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 |