lib/semantic_logger/appender/base.rb in semantic_logger-2.15.0 vs lib/semantic_logger/appender/base.rb in semantic_logger-2.16.0

- old
+ new

@@ -30,15 +30,15 @@ # Replace this formatter by supplying a Block to the initializer # Generates logs of the form: # 2011-07-19 14:36:15.660 D [1149:ScriptThreadProcess] Rails -- Hello World def default_formatter Proc.new do |log| - tags = log.tags.collect { |tag| "[#{tag}]" }.join(" ") + " " if log.tags && (log.tags.size > 0) + tags = log.tags.collect { |tag| "[#{tag}]" }.join(' ') + ' ' if log.tags && (log.tags.size > 0) message = log.message.to_s.dup - message << " -- " << log.payload.inspect if log.payload - message << " -- Exception: " << "#{log.exception.class}: #{log.exception.message}\n#{(log.exception.backtrace || []).join("\n")}" if log.exception + message << ' -- ' << log.payload.inspect unless log.payload.nil? || log.payload.empty? + message << ' -- Exception: ' << "#{log.exception.class}: #{log.exception.message}\n#{(log.exception.backtrace || []).join("\n")}" if log.exception duration_str = log.duration ? "(#{'%.1f' % log.duration}ms) " : '' "#{SemanticLogger::Appender::Base.formatted_time(log.time)} #{log.level.to_s[0..0].upcase} [#{$$}:#{'%.50s' % log.thread_name}] #{tags}#{duration_str}#{log.name} -- #{message}" end @@ -50,30 +50,35 @@ # # 2011-07-19 14:36:15.660 D [1149:ScriptThreadProcess] Rails -- Hello World def self.colorized_formatter Proc.new do |log| colors = SemanticLogger::Appender::AnsiColors - tags = log.tags.collect { |tag| "[#{colors::CYAN}#{tag}#{colors::CLEAR}]" }.join(' ') + ' ' if log.tags && (log.tags.size > 0) + tags = log.tags.collect { |tag| "[#{colors::CYAN}#{tag}#{colors::CLEAR}]" }.join(' ') + ' ' if log.tags && (log.tags.size > 0) message = log.message.to_s.dup - message << " -- " << log.payload.inspect if log.payload - message << " -- Exception: " << "#{colors::BOLD}#{log.exception.class}: #{log.exception.message}#{colors::CLEAR}\n#{(log.exception.backtrace || []).join("\n")}" if log.exception + unless log.payload.nil? || log.payload.empty? + payload = log.payload + payload = (defined?(AwesomePrint) && payload.respond_to?(:ai)) ? payload.ai(multiline: false) : payload.inspect + message << ' -- ' << payload + end + message << ' -- Exception: ' << "#{colors::BOLD}#{log.exception.class}: #{log.exception.message}#{colors::CLEAR}\n#{(log.exception.backtrace || []).join("\n")}" if log.exception duration_str = log.duration ? "(#{colors::BOLD}#{'%.1f' % log.duration}ms#{colors::CLEAR}) " : '' - level_color = case log.level - when :trace - colors::MAGENTA - when :debug - colors::GREEN - when :info - colors::CYAN - when :warn - colors::BOLD - when :error, :fatal - colors::RED - end + level_color = + case log.level + when :trace + colors::MAGENTA + when :debug + colors::GREEN + when :info + colors::CYAN + when :warn + colors::BOLD + when :error, :fatal + colors::RED + end "#{SemanticLogger::Appender::Base.formatted_time(log.time)} #{level_color}#{colors::BOLD}#{log.level.to_s[0..0].upcase}#{colors::CLEAR} [#{$$}:#{'%.30s' % log.thread_name}] #{tags}#{duration_str}#{level_color}#{log.name}#{colors::CLEAR} -- #{message}" end end @@ -133,6 +138,6 @@ end end end end -end \ No newline at end of file +end