diff --git a/lib/semantic/ansi_colors.rb b/lib/semantic/ansi_colors.rb index 517774b..924f0ea 100644 --- a/lib/semantic/ansi_colors.rb +++ b/lib/semantic/ansi_colors.rb @@ -56,5 +56,10 @@ module Semantic DARK_BG_BLUE = "\e[104m".freeze DARK_BG_MAGENTA = "\e[105m".freeze DARK_BG_CYAN = "\e[106m".freeze + + # helper methods + # -------------- + + def colorize(text, tint = color) = "#{tint}#{text}#{CLEAR}" end end diff --git a/lib/semantic/ansi_formatter.rb b/lib/semantic/ansi_formatter.rb index 7bf0317..e1d41eb 100644 --- a/lib/semantic/ansi_formatter.rb +++ b/lib/semantic/ansi_formatter.rb @@ -48,7 +48,7 @@ module Semantic def tags first_tag = log.tags.empty? ? 'none' : log.tags.first - colorize(centerize(first_tag, 10), AnsiColors::BG_GRAY + AnsiColors::DARK_TEXT_YELLOW) + colorize(centerize(first_tag, 10), BG_GRAY + DARK_TEXT_YELLOW) end def named_tags @@ -181,7 +181,7 @@ module Semantic def origin = colorize(centerize(log.name), TEXT_CYAN) def build_prefix(char) = "#{tags} #{origin} ╣#{colorize(char)}╠ " def build_terminus = "#{tags} #{origin} #{TERMINUS_STRING} " - def colorize(text, tint = color) = "#{tint}#{text}#{CLEAR}" + def centerize(text, max_length = CENTER_SIZE) = text.reverse.truncate(max_length).reverse.center(max_length) def stackisize(items) diff --git a/lib/semantic/log_subscriber.rb b/lib/semantic/log_subscriber.rb index 33d38b2..93e695d 100644 --- a/lib/semantic/log_subscriber.rb +++ b/lib/semantic/log_subscriber.rb @@ -1,9 +1,11 @@ module Semantic class LogSubscriber < ActiveSupport::LogSubscriber include SemanticLogger::Loggable + include AnsiColors # def logger = SemanticLogger['Rails'] EMPTY = 'none'.freeze + INTERNAL_PARAMS = %i[controller action format _method only_path].freeze def initialize(session_key) @session_key = session_key @@ -17,12 +19,22 @@ module Semantic SemanticLogger.tagged(session_value) do request = event.payload[:request] - logger.info("Started #{request.raw_request_method} #{request.filtered_path}") + path = colorize(request.filtered_path, BOLD) + logger.info("Started #{request.raw_request_method} #{path}") format = event.payload[:format] format = format.to_s.upcase if format.is_a?(Symbol) format = '*/*' if format.nil? + format = colorize(format, BOLD) logger.debug("Processing by #{event.payload[:controller]}##{event.payload[:action]} as #{format}") + + params = event.payload[:params].deep_symbolize_keys.except(*INTERNAL_PARAMS) + unless params.empty? + params = params.ai(ruby19_syntax: true, plain: true, multiline: false) + params.gsub!(/(\w+):/, "#{TEXT_CYAN}\\1#{CLEAR}:") + params.gsub!(/"(.*?)"/, "\"#{TEXT_YELLOW}\\1#{CLEAR}\"") + end + logger.debug("Parameters: #{params}") unless params.empty? end end @@ -38,13 +50,23 @@ module Semantic end additions << "GC: #{event.gc_time.round(1)}ms" - logger.debug("Processed in #{event.duration.round}ms (#{additions.join(' | ')})") - logger.info("Completed #{status} #{Rack::Utils::HTTP_STATUS_CODES[status]}") + if event.duration >= 1200 + logger.error process_duration(event, additions) + elsif event.duration >= 600 + logger.warn process_duration(event, additions) + elsif event.duration >= 250 + logger.info process_duration(event, additions) + elsif event.duration >= 80 + logger.debug process_duration(event, additions) + end + + logger.info("Completed #{colorize(status, BOLD)} #{Rack::Utils::HTTP_STATUS_CODES[status]}") end end private def session_value(event) = event.payload[:headers]['rack.session'].fetch(@session_key, EMPTY) + def process_duration(event, additions) = "Processed in #{event.duration.round}ms (#{additions.join(' | ')})" end end