lib/logspot.rb in logspot-0.4.0 vs lib/logspot.rb in logspot-0.5.0
- old
+ new
@@ -1,20 +1,20 @@
require_relative 'initialize'
class LoGspot
LOG_LEVELS = %w(DEBUG INFO WARN ERROR FATAL)
- def initialize(file_or_file_name = STDOUT, wrapper = nil)
+ 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 = "[#{Time.now.strftime('%Y/%m/%d %H:%M:%S')} #{level}] "
+ 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]}")
}
- @file = Output::File.new(file_or_file_name)
- @output = @original_output = Output::Wrap.new(wrapper, @file)
+ @raw_output = @file = Output::File.new(file_or_file_name)
+ @top_output = @output = Output::Wrap.new(wrapper, @file)
@level = nil
end
def tagged(tag, &block)
wrap_output(block) do |output, data|
@@ -74,15 +74,27 @@
end
end
end
def untagged(&block)
- previous_output, @output = output, original_output
+ previous_output, @output = output, output.inner_output
block.call
@output = previous_output
end
+ def top(&block)
+ previous_output, @output = output, top_output
+ block.call
+ @output = previous_output
+ end
+
+ def raw(&block)
+ previous_output, @output = output, raw_output
+ block.call
+ @output = previous_output
+ end
+
LOG_LEVELS.each do |level|
define_method(level.downcase) do |*args, &block|
write(level, *args, &block)
end
end
@@ -91,10 +103,10 @@
@file.finalize
end
private
- attr_reader :original_output, :output, :level
+ attr_reader :raw_output, :top_output, :output, :level
def write(l, *args, &block)
@level = l
output.puts(message: args[0], args: args, arg_block: block)
end