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.

115 lines
4.6 KiB

  1. module Semantic
  2. module Subscribers
  3. class ActionController < LogSubscriber
  4. include AnsiColors
  5. INTERNAL_PARAMS = %i[controller action format _method only_path].freeze
  6. DEFAULT_DEV_HOSTS = ['127.0.0.1', 'localhost'].freeze
  7. TERMINUS_STRING = '╙─╜'.freeze
  8. def initialize(session_key)
  9. @session_key = session_key
  10. @transactions = {}
  11. super(:action_controller)
  12. end
  13. def start_processing(event)
  14. session_value = session_value(event)
  15. @transactions[event.transaction_id] = session_value # preserve session_value to help finish_processing
  16. SemanticLogger.tagged(session_value) do
  17. request = event.payload[:request]
  18. path = colorize(request.filtered_path, BOLD)
  19. dimensions = Semantic::AnsiDimensions.new(rails: '╓─╖', before: 1)
  20. if defined?(@previously_redirect) && @previously_redirect
  21. dimensions = Semantic::AnsiDimensions.new(rails: '╓║╖', before: 0)
  22. @previously_redirect = false
  23. end
  24. logger.info("Started #{request.raw_request_method} #{path}", dimensions:)
  25. format = event.payload[:format]
  26. format = format.to_s.upcase if format.is_a?(Symbol)
  27. format = '*/*' if format.nil?
  28. format = colorize(format, BOLD)
  29. logger.debug("Processing by #{event.payload[:controller]}##{event.payload[:action]} as #{format}")
  30. params = event.payload[:params].deep_symbolize_keys.except(*INTERNAL_PARAMS)
  31. unless params.empty?
  32. params = params.ai(ruby19_syntax: true, plain: true, multiline: false)
  33. params.gsub!(/(\w+):/, "#{TEXT_CYAN}\\1#{CLEAR}:")
  34. params.gsub!(/"(.*?)"/, "\"#{TEXT_YELLOW}\\1#{CLEAR}\"")
  35. end
  36. logger.debug("Parameters: #{params}") unless params.empty?
  37. end
  38. end
  39. def finish_processing(event)
  40. session_value = @transactions.delete(event.transaction_id) # delete previous session_value from start_processing
  41. SemanticLogger.tagged(session_value) do
  42. payload = event.payload
  43. additions = ::ActionController::Base.log_process_action(payload)
  44. status = payload[:status]
  45. if status.nil? && (exception_class_name = payload[:exception]&.first)
  46. status = ::ActionDispatch::ExceptionWrapper.status_code_for_exception(exception_class_name)
  47. end
  48. additions << "GC: #{event.gc_time.round(1)}ms"
  49. if event.duration >= 1200
  50. logger.error process_duration(event, additions)
  51. elsif event.duration >= 600
  52. logger.warn process_duration(event, additions)
  53. elsif event.duration >= 300
  54. logger.info process_duration(event, additions)
  55. elsif event.duration >= 100
  56. logger.debug process_duration(event, additions)
  57. end
  58. status_family = status / 100
  59. dimensions = case status_family
  60. when 2
  61. Semantic::AnsiDimensions.new(rails: TERMINUS_STRING)
  62. when 3
  63. Semantic::AnsiDimensions.new(rails: '╙║╜')
  64. when 4
  65. Semantic::AnsiDimensions.new(rails: '╙╨╜')
  66. when 5
  67. Semantic::AnsiDimensions.new(rails: '╙║╜')
  68. end
  69. logger.info("Completed #{colorize(status, BOLD)} #{Rack::Utils::HTTP_STATUS_CODES[status]}", dimensions:)
  70. logger.info(' ', dimensions: Semantic::AnsiDimensions.new(rails: ' ║ ')) if status_family == 3
  71. logger.info(' ', dimensions: Semantic::AnsiDimensions.new(rails: '╓║╖')) if status_family == 5
  72. end
  73. end
  74. def redirect_to(event)
  75. location = capture_path(event.payload[:location])
  76. logger.debug("Redirected to #{colorize(location, BOLD)}")
  77. @previously_redirect = true
  78. end
  79. private
  80. def redirect_regex
  81. return @redirect_regex if defined?(@redirect_regex)
  82. options = Rails.application.routes.default_url_options
  83. dev_hosts = DEFAULT_DEV_HOSTS + Array.wrap(options[:host])
  84. dev_hosts_or = dev_hosts.uniq.join('|')
  85. dev_from = "http://(?:#{dev_hosts_or}):#{options[:port]}(.*)"
  86. @redirect_regex = /^#{dev_from}/
  87. end
  88. def capture_path(url)
  89. m = redirect_regex.match(url)
  90. m.nil? ? url : m[1]
  91. end
  92. def session_value(event) = event.payload[:headers]['rack.session'].fetch(@session_key, nil)
  93. def process_duration(event, additions) = "Processed in #{event.duration.round}ms (#{additions.join(' | ')})"
  94. end
  95. end
  96. end