You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

71 lines
2.7 KiB

3 months ago
3 months ago
3 months ago
3 months ago
3 months ago
3 months ago
3 months ago
3 months ago
3 months ago
3 months ago
3 months ago
  1. module Semantic
  2. class LogSubscriber < ActiveSupport::LogSubscriber
  3. include SemanticLogger::Loggable
  4. include AnsiColors
  5. # def logger = SemanticLogger['Rails']
  6. INTERNAL_PARAMS = %i[controller action format _method only_path].freeze
  7. def initialize(session_key)
  8. @session_key = session_key
  9. @transactions = {}
  10. super()
  11. end
  12. def start_processing(event)
  13. session_value = session_value(event)
  14. @transactions[event.transaction_id] = session_value # preserve session_value to help finish_processing
  15. SemanticLogger.tagged(session_value) do
  16. request = event.payload[:request]
  17. path = colorize(request.filtered_path, BOLD)
  18. logger.info("Started #{request.raw_request_method} #{path}")
  19. format = event.payload[:format]
  20. format = format.to_s.upcase if format.is_a?(Symbol)
  21. format = '*/*' if format.nil?
  22. format = colorize(format, BOLD)
  23. logger.debug("Processing by #{event.payload[:controller]}##{event.payload[:action]} as #{format}")
  24. params = event.payload[:params].deep_symbolize_keys.except(*INTERNAL_PARAMS)
  25. unless params.empty?
  26. params = params.ai(ruby19_syntax: true, plain: true, multiline: false)
  27. params.gsub!(/(\w+):/, "#{TEXT_CYAN}\\1#{CLEAR}:")
  28. params.gsub!(/"(.*?)"/, "\"#{TEXT_YELLOW}\\1#{CLEAR}\"")
  29. end
  30. logger.debug("Parameters: #{params}") unless params.empty?
  31. end
  32. end
  33. def finish_processing(event)
  34. session_value = @transactions.delete(event.transaction_id) # get previous session_value from start_processing
  35. SemanticLogger.tagged(session_value) do
  36. payload = event.payload
  37. additions = ActionController::Base.log_process_action(payload)
  38. status = payload[:status]
  39. if status.nil? && (exception_class_name = payload[:exception]&.first)
  40. status = ActionDispatch::ExceptionWrapper.status_code_for_exception(exception_class_name)
  41. end
  42. additions << "GC: #{event.gc_time.round(1)}ms"
  43. if event.duration >= 1200
  44. logger.error process_duration(event, additions)
  45. elsif event.duration >= 600
  46. logger.warn process_duration(event, additions)
  47. elsif event.duration >= 250
  48. logger.info process_duration(event, additions)
  49. elsif event.duration >= 80
  50. logger.debug process_duration(event, additions)
  51. end
  52. logger.info("Completed #{colorize(status, BOLD)} #{Rack::Utils::HTTP_STATUS_CODES[status]}")
  53. end
  54. end
  55. private
  56. def session_value(event) = event.payload[:headers]['rack.session'].fetch(@session_key, nil)
  57. def process_duration(event, additions) = "Processed in #{event.duration.round}ms (#{additions.join(' | ')})"
  58. end
  59. end