<commit_before>require 'thread'

class LogStuff
  NAMESPACE = :log

  def self.use_logstasher?
    LogStasher.enabled?
  end

  def self.get_thread_current(name)
    Thread.current[NAMESPACE] ||= {
        :current_fields => {},
        :current_tags => Set.new
    }
    Thread.current[NAMESPACE][name].dup
  end

  def self.set_thread_current(name, value)
    Thread.current[NAMESPACE] ||= {
        :current_fields => {},
        :current_tags => Set.new
    }
    Thread.current[NAMESPACE][name] = value.dup
  end

  def self.log(severity = 'info', *args, &block)
    return unless block_given?

    if self.use_logstasher?
      return unless LogStasher.logger.send("#{severity}?")
    else
      return unless Rails.logger.send("#{severity}?")
    end

    local_fields = {}
    local_tags   = Set.new
    args.each do |arg|
      case arg
        when Hash
          local_fields.merge!(arg)
        when Symbol
          local_tags.add(arg)
        when Array
          local_tags.merge(arg)
      end
    end

    if self.use_logstasher?
      msg = yield

      event = LogStash::Event.new('@source' => LogStasher.source,
                                  '@severity' => severity,
                                  'message' => msg,
                                  '@tags' => get_thread_current(:current_tags).merge(local_tags),
                                  '@fields' => get_thread_current(:current_fields).merge(local_fields)
      )
      LogStasher.logger << event.to_json + "\n"
    else
      Rails.logger.send(severity, &block)
    end
  end

  %w( fatal error warn info debug ).each do |severity|
    eval <<-EOM, nil, __FILE__, __LINE__ + 1
      def self.#{severity}(*args, &block)
        self.log(:#{severity}, *args, &block )
      end
    EOM
  end

  def self.tag(*tags, &block)
    original_tags = get_thread_current(:current_tags)
    current_tags = original_tags.dup + tags.flatten
    set_thread_current(:current_tags, current_tags)
    yield<commit_msg>Updated log_stuff<commit_after>require 'thread'

class LogStuff
  NAMESPACE = :log

  def self.use_logstasher?
    LogStasher.enabled?
  end

  def self.get_thread_current(name)
    Thread.current[NAMESPACE] ||= {
        :current_fields => {},
        :current_tags => Set.new
    }
    Thread.current[NAMESPACE][name].dup
  end

  def self.set_thread_current(name, value)
    Thread.current[NAMESPACE] ||= {
        :current_fields => {},
        :current_tags => Set.new
    }
    Thread.current[NAMESPACE][name] = value.dup
  end

  def self.log(severity = 'info', *args, &block)
    return unless block_given?

    if self.use_logstasher?
      return unless LogStasher.logger.send("#{severity}?")

      msg = yield
      event = build_event(*args, msg)
      LogStasher.logger << event.to_json + "\n"
    else
      return unless Rails.logger.send("#{severity}?")
      Rails.logger.send(severity, &block)
    end
  end

  def self.build_event(*args, msg)
    local_fields, local_tags = compile_fields_and_tags(args)

    LogStash::Event.new('@source' => LogStasher.source,
                        '@severity' => severity,
                        'message' => msg,
                        '@tags' => get_thread_current(:current_tags).merge(local_tags),
                        '@fields' => get_thread_current(:current_fields).merge(local_fields)
    )
  end

  def self.compile_fields_and_tags(args)
    local_fields = {}
    local_tags = Set.new
    args.each do |arg|
      case arg
        when Hash
          local_fields.merge!(arg)
        when Symbol
          local_tags.add(arg)
        when Array
          local_tags.merge(arg)
      end
    end
    return local_fields, local_tags
  end

  %w( fatal error warn info debug ).each do |severity|
    eval <<-EOM, nil, __FILE__, __LINE__ + 1
      def self.#{severity}(*args, &block)
        self.log(:#{severity}, *args, &block )
      end
    EOM
  end

  def self.tag(*tags, &block)
    original_tags = get_thread_current(:current_tags)
    current_tags = original_tags.dup + tags.flatten
    set_thread_current(:current_tags, current_tags)
    yield
{'fraction_non_alphanumeric': 0.11711711711711711, 'fraction_numerical': 0.0004869734599464329, 'mean_word_length': 2.3452768729641695, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 21, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1019029', 'n_tokens_mistral': 1357, 'n_tokens_neox': 1320, 'n_words': 257}