<commit_before>require 'active_support/core_ext/class/attribute'
require 'active_support/log_subscriber'

module Lograge
  class RequestLogSubscriber < ActiveSupport::LogSubscriber
    def process_action(event)
      payload = event.payload

      data      = extract_request(payload)
      data.merge! extract_status(payload)
      data.merge! runtimes(event)
      data.merge! location(event)
      data.merge! custom_options(event)

      line = send(:"process_action_#{Lograge.log_format}", data)
      logger.send(Lograge.log_level, line)
    end

    LOGRAGE_FIELDS = [
      :method, :path, :format, :controller, :action, :status, :error,
      :duration, :view, :db, :location
    ]
    def process_action_lograge(data)
      fields  = LOGRAGE_FIELDS
      fields += (data.keys - LOGRAGE_FIELDS)

      event = fields.inject([]) do |message, key|
        next message unless data.has_key?(key)
        # Exactly preserve the previous output
        # Parsing this can be ambigious if the error messages contains
        # a single quote
        data[key] = "'#{data[key]}'" if key == :error
        # Ensure that we always have exactly two decimals
        data[key] = "%.2f" % data[key] if data[key].is_a? Float

        message << "#{key}=#{data[key]}"
        message
      end
      event.join(" ")
    end

    def process_action_logstash(data)
      event = LogStash::Event.new("@fields" => data)
      event.message = "#{data[:method]} #{data[:path]} #{data[:status} #{data[:duration]}s #{data[:controller]}##{data[:action]}"
      event.to_json
    end

    def process_action_graylog2(data)
      # Cloning because we don't want to mess with the original when mutating keys.
      my = data.clone

      base = {
        :short_message => "[#{my[:status]}] #{my[:method]} #{my[:path]} (#{my[:controller]}##{my[:action]})"<commit_msg>modify logstash message to match graylog style<commit_after>require 'active_support/core_ext/class/attribute'
require 'active_support/log_subscriber'

module Lograge
  class RequestLogSubscriber < ActiveSupport::LogSubscriber
    def process_action(event)
      payload = event.payload

      data      = extract_request(payload)
      data.merge! extract_status(payload)
      data.merge! runtimes(event)
      data.merge! location(event)
      data.merge! custom_options(event)

      line = send(:"process_action_#{Lograge.log_format}", data)
      logger.send(Lograge.log_level, line)
    end

    LOGRAGE_FIELDS = [
      :method, :path, :format, :controller, :action, :status, :error,
      :duration, :view, :db, :location
    ]
    def process_action_lograge(data)
      fields  = LOGRAGE_FIELDS
      fields += (data.keys - LOGRAGE_FIELDS)

      event = fields.inject([]) do |message, key|
        next message unless data.has_key?(key)
        # Exactly preserve the previous output
        # Parsing this can be ambigious if the error messages contains
        # a single quote
        data[key] = "'#{data[key]}'" if key == :error
        # Ensure that we always have exactly two decimals
        data[key] = "%.2f" % data[key] if data[key].is_a? Float

        message << "#{key}=#{data[key]}"
        message
      end
      event.join(" ")
    end

    def process_action_logstash(data)
      event = LogStash::Event.new("@fields" => data)
      event.message = "[#{data[:status]}] #{data[:method]} #{data[:path]} (#{data[:controller]}##{data[:action]})"
      event.to_json
    end

    def process_action_graylog2(data)
      # Cloning because we don't want to mess with the original when mutating keys.
      my = data.clone

      base = {
        :short_message => "[#{my[:status]}] #{my[:method]} #{my[:path]} (#{my[:controller]}##{my[:action]})"
{'fraction_non_alphanumeric': 0.13582571274878968, 'fraction_numerical': 0.0010758472296933835, 'mean_word_length': 3.132222222222222, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 7, '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': '2987196', 'n_tokens_mistral': 1158, 'n_tokens_neox': 1152, 'n_words': 276}