lib/uu/logger_stderr.rb in uu-0.2.1 vs lib/uu/logger_stderr.rb in uu-0.2.2

- old
+ new

@@ -5,19 +5,10 @@ require 'json' module UU class LoggerStderr < Logger class Formatter - DECORATION = { - 'UNKNOWN' => { color: :red, mode: :underline }, - 'FATAL' => { color: :red, mode: :underline }, - 'ERROR' => :red, - 'WARN' => :yellow, - 'INFO' => :default, - 'DEBUG' => :light_black, - }.freeze - def initialize(context) @context = context end attr_reader :context @@ -30,11 +21,11 @@ context_msg = context.empty? ? '' : "#{context.to_json} " "#{short_severity}]#{time_format}" \ "[#{loc[:filename]}##{loc[:method]}:#{loc[:lineno]}] " \ "#{context_msg}" \ - "#{msg}\n".colorize(DECORATION[severity]) + "#{msg}\n" end def meaningful_location location = find_location { @@ -57,10 +48,26 @@ PATHS.none? { |path| location_.path.end_with?(path) } end end end + class FormatterColor < Formatter + DECORATION = { + 'UNKNOWN' => { color: :red, mode: :underline }, + 'FATAL' => { color: :red, mode: :underline }, + 'ERROR' => :red, + 'WARN' => :yellow, + 'INFO' => :default, + 'DEBUG' => :light_black, + }.freeze + + def call(severity, time, _progname, msg) + super.colorize(DECORATION[severity]) + end + end + def initialize(context) - super($stderr, formatter: Formatter.new(context)) + formatter = $stderr.tty? ? FormatterColor : Formatter + super($stderr, formatter: formatter.new(context)) end end end