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.

124 lines
4.8 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
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
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. DEFAULT_DEV_HOSTS = ['127.0.0.1', 'localhost'].freeze
  8. TERMINUS_STRING = '╙─╜'.freeze
  9. def initialize(session_key)
  10. @session_key = session_key
  11. @transactions = {}
  12. super()
  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::AnsiDimensions.new(rails: '╓─╖', before: 1)
  21. if defined?(@previously_redirect) && @previously_redirect
  22. dimensions = Semantic::AnsiDimensions.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 finish_processing(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 >= 250
  55. logger.info process_duration(event, additions)
  56. elsif event.duration >= 80
  57. logger.debug process_duration(event, additions)
  58. end
  59. status_family = status / 100
  60. dimensions = case status_family
  61. when 2
  62. Semantic::AnsiDimensions.new(rails: TERMINUS_STRING, after: 1)
  63. when 3
  64. Semantic::AnsiDimensions.new(rails: '╙║╜')
  65. when 4
  66. Semantic::AnsiDimensions.new(rails: '╙╨╜')
  67. when 5
  68. Semantic::AnsiDimensions.new(rails: '╙║╜')
  69. end
  70. logger.info("Completed #{colorize(status, BOLD)} #{Rack::Utils::HTTP_STATUS_CODES[status]}", dimensions:)
  71. logger.info(' ', dimensions: Semantic::AnsiDimensions.new(rails: ' ║ ')) if status_family == 3
  72. logger.info(' ', dimensions: Semantic::AnsiDimensions.new(rails: '╓║╖')) if status_family == 5
  73. end
  74. end
  75. def redirect_to(event)
  76. SemanticLogger.tagged(@transactions[event.transaction_id]) do
  77. location = capture_path(event.payload[:location])
  78. logger.debug("Redirected to #{colorize(location, BOLD)}")
  79. end
  80. @previously_redirect = true
  81. end
  82. def any_hook(event)
  83. SemanticLogger.tagged(@transactions[event.transaction_id]) do
  84. logger.warn("action_controller hook=<#{event.name.split('.')[0]}> needs a proper message handling!",
  85. event.payload.keys)
  86. end
  87. end
  88. private
  89. def redirect_regex
  90. return @redirect_regex if defined?(@redirect_regex)
  91. options = Rails.application.routes.default_url_options
  92. dev_hosts = DEFAULT_DEV_HOSTS + Array.wrap(options[:host])
  93. dev_hosts_or = dev_hosts.uniq.join('|')
  94. dev_from = "http://(?:#{dev_hosts_or}):#{options[:port]}(.*)"
  95. @redirect_regex = /^#{dev_from}/
  96. end
  97. def capture_path(url)
  98. m = redirect_regex.match(url)
  99. m.nil? ? url : m[1]
  100. end
  101. def session_value(event) = event.payload[:headers]['rack.session'].fetch(@session_key, nil)
  102. def process_duration(event, additions) = "Processed in #{event.duration.round}ms (#{additions.join(' | ')})"
  103. end
  104. end