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.

51 lines
1.8 KiB

1 month ago
1 month ago
1 month ago
1 month ago
  1. module Semantic
  2. class LogSubscriber < ActiveSupport::LogSubscriber
  3. include SemanticLogger::Loggable
  4. # def logger = SemanticLogger['Rails']
  5. EMPTY = 'none'.freeze
  6. def initialize(session_key)
  7. @session_key = session_key
  8. @transactions = {}
  9. super()
  10. end
  11. def start_processing(event)
  12. session_value = session_value(event)
  13. @transactions[event.transaction_id] = session_value # preserve session_value to help finish_processing
  14. SemanticLogger.tagged(session_value) do
  15. request = event.payload[:request]
  16. logger.info("Started #{request.raw_request_method} #{request.filtered_path}")
  17. format = event.payload[:format]
  18. format = format.to_s.upcase if format.is_a?(Symbol)
  19. format = '*/*' if format.nil?
  20. logger.debug("Processing by #{event.payload[:controller]}##{event.payload[:action]} as #{format}")
  21. end
  22. end
  23. def finish_processing(event)
  24. session_value = @transactions.delete(event.transaction_id) # get previous session_value from start_processing
  25. SemanticLogger.tagged(session_value) do
  26. payload = event.payload
  27. additions = ActionController::Base.log_process_action(payload)
  28. status = payload[:status]
  29. if status.nil? && (exception_class_name = payload[:exception]&.first)
  30. status = ActionDispatch::ExceptionWrapper.status_code_for_exception(exception_class_name)
  31. end
  32. additions << "GC: #{event.gc_time.round(1)}ms"
  33. logger.info("Completed #{status} #{Rack::Utils::HTTP_STATUS_CODES[status]} in #{event.duration.round}ms " \
  34. "(#{additions.join(' | ')})")
  35. end
  36. end
  37. private
  38. def session_value(event) = event.payload[:headers]['rack.session'].fetch(@session_key, EMPTY)
  39. end
  40. end