<commit_before>require "action_controller/log_subscriber"

module Sapience
  module Extensions
    module ActionController
      class LogSubscriber < ::ActiveSupport::LogSubscriber # rubocop:disable ClassLength
        # Log as debug to hide Processing messages in production
        def start_processing(event)
          controller_logger(event).debug { "Processing ##{event.payload[:action]}" }
        end

        def process_action(event) # rubocop:disable AbcSize, CyclomaticComplexity, PerceivedComplexity
          data      = request(event.payload)
          data.merge! runtimes(event)
          data.merge! exception(event.payload)
          info(data)
        end

        private

        def request(payload) # rubocop:disable AbcSize
          {
            method: payload[:method].upcase,
            request_path: request_path(payload),
            format: format(payload),
            status: payload[:status].to_i,
            controller: payload[:params]["controller"],
            action: payload[:params]["action"],
            host: Sapience.config.host,
            route: "#{payload[:params].delete("controller")}##{payload[:params]["action"]}",
            message: "Completed ##{payload[:params].delete("action")}",
            tags: Sapience.tags,
            params: payload[:params],
          }
        end

        def format(payload)
          if ::ActionPack::VERSION::MAJOR == 3 && ::ActionPack::VERSION::MINOR == 0
            payload[:formats].first
          else
            payload[:format]
          end
        end

        def runtimes(event)
          {
            total: event.duration,
            view: event.payload[:view_runtime],
            db: event.payload[:db_runtime],
          }.each_with_object({}) do |(name, runtime), runtimes|
            runtimes[:runtimes] ||= {}
            runtimes[:runtimes][name] = runtime.to_f.round(2) if runtime
            runtimes
          end
        end

        # Monkey patching to enable exception logging
        def exception(payload)
          if payload[:exception]
            exception, message = payload[:exception]
            status = ActionDispatch::ExceptionWrapper.status_code_for_exception(exception)
            backtrace = $ERROR_INFO.backtrace.first
            message = "#{exception}\n#{message}\n#{backtrace}"
            { status: status, error: message }
          else
            {}
          end
        end

        def request_path(payload)
          payload[:path].split("?").first
        end
      end
    end
  end
end
<commit_msg>[+AM] Use the default logger<commit_after>require "action_controller/log_subscriber"

module Sapience
  module Extensions
    module ActionController
      class LogSubscriber < ::ActiveSupport::LogSubscriber # rubocop:disable ClassLength
        # Log as debug to hide Processing messages in production
        def start_processing(event)
          debug { "Processing ##{event.payload[:action]}" }
        end

        def process_action(event) # rubocop:disable AbcSize, CyclomaticComplexity, PerceivedComplexity
          data      = request(event.payload)
          data.merge! runtimes(event)
          data.merge! exception(event.payload)
          info(data)
        end

        private

        def request(payload) # rubocop:disable AbcSize
          {
            method: payload[:method].upcase,
            request_path: request_path(payload),
            format: format(payload),
            status: payload[:status].to_i,
            controller: payload[:params]["controller"],
            action: payload[:params]["action"],
            host: Sapience.config.host,
            route: "#{payload[:params].delete("controller")}##{payload[:params]["action"]}",
            message: "Completed ##{payload[:params].delete("action")}",
            tags: Sapience.tags,
            params: payload[:params],
          }
        end

        def format(payload)
          if ::ActionPack::VERSION::MAJOR == 3 && ::ActionPack::VERSION::MINOR == 0
            payload[:formats].first
          else
            payload[:format]
          end
        end

        def runtimes(event)
          {
            total: event.duration,
            view: event.payload[:view_runtime],
            db: event.payload[:db_runtime],
          }.each_with_object({}) do |(name, runtime), runtimes|
            runtimes[:runtimes] ||= {}
            runtimes[:runtimes][name] = runtime.to_f.round(2) if runtime
            runtimes
          end
        end

        # Monkey patching to enable exception logging
        def exception(payload)
          if payload[:exception]
            exception, message = payload[:exception]
            status = ActionDispatch::ExceptionWrapper.status_code_for_exception(exception)
            backtrace = $ERROR_INFO.backtrace.first
            message = "#{exception}\n#{message}\n#{backtrace}"
            { status: status, error: message }
          else
            {}
          end
        end

        def request_path(payload)
          payload[:path].split("?").first
        end
      end
    end
  end
end

{'fraction_non_alphanumeric': 0.10919765166340509, 'fraction_numerical': 0.0011741682974559687, 'mean_word_length': 2.0568181818181817, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3630755', 'n_tokens_mistral': 1320, 'n_tokens_neox': 1304, 'n_words': 280}