lib/logspot.rb in logspot-0.6.1 vs lib/logspot.rb in logspot-0.6.2
- old
+ new
@@ -5,11 +5,11 @@
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
+ 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)
@@ -17,12 +17,12 @@
end
def tagged(tag, &block)
wrap_output(block) do |output, data|
base = tag
- if space = !!data[:space]
- base = ' ' * base.length
+ if space = data[:space]
+ base = ' ' * uncolorize_str(base).length
end
output.puts(data.merge(message: "#{base}#{data[:message]}", space: space))
end
end
@@ -30,11 +30,11 @@
first = true
wrap_output(block, *args) do |output, data|
message = data[:message]
base = tag
if data[:space] || !first
- base = ' ' * base.length
+ base = ' ' * uncolorize_str(base).length
end
if first
output.puts(data.merge(message: "#{base}#{message}", space: false))
first = false
else
@@ -113,7 +113,11 @@
def wrap_output(block, *args, &wrapper)
previous_output, @output = output, Output::Wrap.new(wrapper, output)
res = block.call(*args)
@output = previous_output
res
+ end
+
+ def uncolorize_str(str)
+ str.gsub(/\033\[[;0-9]+m/, '')
end
end