lib/berkeley_library/logging/formatters.rb in berkeley_library-logging-0.2.2 vs lib/berkeley_library/logging/formatters.rb in berkeley_library-logging-0.2.3
- old
+ new
@@ -3,10 +3,14 @@
module BerkeleyLibrary
module Logging
module Formatters
class << self
+
+ # See https://stackoverflow.com/a/14693789/27358
+ ANSI_7C1_RE = %r{\x1B(?:[@-Z\\-_]|\[[0-?]*[ -/]*[@-~])}.freeze
+
def new_json_formatter
Bunyan.new
end
def new_readable_formatter
@@ -22,15 +26,15 @@
return message if message.is_a?(Hash)
{ msg: message }
end
- def decolorize(message)
+ def strip_ansi_escapes(message)
return unless message
- return message.uncolorize if message.is_a?(String)
- return message.map { |v| decolorize(v) } if message.is_a?(Array)
- return message.transform_values { |v| decolorize(v) } if message.is_a?(Hash)
+ return message.gsub(ANSI_7C1_RE, '') if message.is_a?(String)
+ return message.map { |v| strip_ansi_escapes(v) } if message.is_a?(Array)
+ return message.transform_values { |v| strip_ansi_escapes(v) } if message.is_a?(Hash)
message
end
end
@@ -85,10 +89,10 @@
include Ougai::Logging::Severity
include ErrorCauseSerializer
def _call(severity, time, progname, data)
original_data = Formatters.ensure_hash(data)
- decolorized_data = Formatters.decolorize(original_data)
+ decolorized_data = Formatters.strip_ansi_escapes(original_data)
# Ougai::Formatters::Bunyan replaces the human-readable severity string
# with a numeric level, so we add it here as a separate attribute
severity = ensure_human_readable(severity)
merged_data = { severity: severity }.merge(decolorized_data)