<commit_before>require 'logger'

module Postal
  class AppLogger < Logger


    def self.greylog_notifier
      @greylog_notifier ||= Postal.config.logging.greylog ? GELF::Notifier.new(Postal.config.logging.greylog.host, Postal.config.logging.greylog.port) : nil

    end

    def initialize(log_name, *args)
      @log_name = log_name
      super(*args)
      self.formatter = LogFormatter.new
    end

    def add(severity, message = nil, progname = nil)
      super
      if n = self.class.greylog_notifier
        begin
          if message.nil?
            message = block_given? ? yield : progname
          end
          message = message.to_s.force_encoding('UTF-8').scrub
          message_without_ansi = message.gsub(/\e\[([\d\;]+)?m/, '') rescue message
          n.notify!(:short_message => message_without_ansi, :log_name => @log_name, :facility => 'postal', :application_name => 'postal', :process_name => ENV['PROC_NAME'], :pid => Process.pid)
        rescue => e
          # Can't log this to GELF. Soz.
          if defined?(Raven)
            Raven.capture_exception(e)
          end
        end
      end
      true
    end
  end<commit_msg>only submit entries to greylog that apply to the current level<commit_after>require 'logger'

module Postal
  class AppLogger < Logger

    def self.greylog_notifier
      @greylog_notifier ||= Postal.config.logging.greylog ? GELF::Notifier.new(Postal.config.logging.greylog.host, Postal.config.logging.greylog.port) : nil

    end

    def initialize(log_name, *args)
      @log_name = log_name
      super(*args)
      self.formatter = LogFormatter.new
    end

    def add(severity, message = nil, progname = nil)
      super
      if severity >= @level && n = self.class.greylog_notifier
        begin
          if message.nil?
            message = block_given? ? yield : progname
          end
          message = message.to_s.force_encoding('UTF-8').scrub
          message_without_ansi = message.gsub(/\e\[([\d\;]+)?m/, '') rescue message
          n.notify!(:short_message => message_without_ansi, :log_name => @log_name, :facility => 'postal', :application_name => 'postal', :process_name => ENV['PROC_NAME'], :pid => Process.pid)
        rescue => e
          # Can't log this to GELF. Soz.
          if defined?(Raven)
            Raven.capture_exception(e)
          end
        end
      end
      true
    end
  end
{'fraction_non_alphanumeric': 0.11589958158995815, 'fraction_numerical': 0.0008368200836820083, 'mean_word_length': 2.415714285714286, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 18, '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': '6684826', 'n_tokens_mistral': 777, 'n_tokens_neox': 751, 'n_words': 165}