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

2 months ago
  1. module Semantic
  2. module Subscribers
  3. # LogSubscriber for event_group :action_controller
  4. class ActionController < LogSubscriber
  5. include AnsiColors
  6. INTERNAL_PARAMS = %i[controller action format _method only_path].freeze
  7. DEFAULT_DEV_HOSTS = ['127.0.0.1', 'localhost'].freeze
  8. TERMINUS_STRING = '╙─╜'.freeze
  9. def initialize(session_key)
  10. super(:controller)
  11. @session_key = session_key
  12. @transactions = {}
  13. end
  14. def start_processing(event)
  15. session_value = session_value(event)
  16. @transactions[event.transaction_id] = session_value # preserve session_value to help finish_processing
  17. SemanticLogger.tagged(session_value) do
  18. request = event.payload[:request]
  19. path = colorize(request.filtered_path, BOLD)
  20. dimensions = Semantic::FancyDimensions.new(rails: '╓─╖', before: 1)
  21. if defined?(@previously_redirect) && @previously_redirect
  22. dimensions = Semantic::FancyDimensions.new(rails: '╓║╖', before: 0)
  23. @previously_redirect = false
  24. end
  25. logger.info("Started #{request.raw_request_method} #{path}", dimensions:)
  26. format = event.payload[:format]
  27. format = format.to_s.upcase if format.is_a?(Symbol)
  28. format = '*/*' if format.nil?
  29. format = colorize(format, BOLD)
  30. logger.debug("Processing by #{event.payload[:controller]}##{event.payload[:action]} as #{format}")
  31. params = event.payload[:params].deep_symbolize_keys.except(*INTERNAL_PARAMS)
  32. unless params.empty?
  33. params = params.ai(ruby19_syntax: true, plain: true, multiline: false)
  34. params.gsub!(/(\w+):/, "#{TEXT_CYAN}\\1#{CLEAR}:")
  35. params.gsub!(/"(.*?)"/, "\"#{TEXT_YELLOW}\\1#{CLEAR}\"")
  36. end
  37. logger.debug("Parameters: #{params}") unless params.empty?
  38. end
  39. end
  40. def process_action(event)
  41. session_value = @transactions.delete(event.transaction_id) # delete previous session_value from start_processing
  42. SemanticLogger.tagged(session_value) do
  43. payload = event.payload
  44. additions = ::ActionController::Base.log_process_action(payload)
  45. status = payload[:status]
  46. if status.nil? && (exception_class_name = payload[:exception]&.first)
  47. status = ::ActionDispatch::ExceptionWrapper.status_code_for_exception(exception_class_name)
  48. end
  49. additions << "GC: #{event.gc_time.round(1)}ms"
  50. if event.duration >= 1200
  51. logger.error process_duration(event, additions)
  52. elsif event.duration >= 600
  53. logger.warn process_duration(event, additions)
  54. elsif event.duration >= 300
  55. logger.info process_duration(event, additions)
  56. # elsif event.duration >= 100
  57. else
  58. logger.debug process_duration(event, additions)
  59. end
  60. status_family = status / 100
  61. dimensions = case status_family
  62. when 2
  63. Semantic::FancyDimensions.new(rails: TERMINUS_STRING)
  64. when 3, 5
  65. Semantic::FancyDimensions.new(rails: '╙║╜')
  66. when 4
  67. Semantic::FancyDimensions.new(rails: '╙╨╜')
  68. end
  69. logger.info("Completed #{colorize(status, BOLD)} #{Rack::Utils::HTTP_STATUS_CODES[status]}", dimensions:)
  70. logger.info(' ', dimensions: Semantic::FancyDimensions.new(rails: ' ║ ')) if status_family == 3
  71. logger.info(' ', dimensions: Semantic::FancyDimensions.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