<commit_before>require 'time'

# TODO: Register custom formats
#
# @example The Yell default fomat
#   Yell::Formatter.register(:default)
#
# @example The Ruby standard logger format
#   Yell::Formatter.register(:stdlogger, "%l, [%d #%p] %5L -- : %m", "%Y-%m-%dT%H:%M:%S.%6N")
#
module Yell #:nodoc:

  # No format on the log message
  #
  # @example
  #   logger = Yell.new STDOUT, :format => false
  #   logger.info "Hello World!"
  #   #=> "Hello World!"
  NoFormat = "%m"

  # Default Format
  #
  # @example
  #   logger = Yell.new STDOUT, :format => Yell::DefaultFormat
  #   logger.info "Hello World!"
  #   #=> "2012-02-29T09:30:00+01:00 [ INFO] 65784 : Hello World!"
  #   #    ^                         ^       ^       ^
  #   #    ISO8601 Timestamp         Level   Pid     Message
  DefaultFormat = "%d [%5L] %p : %m"

  # Basic Format
  #
  # @example
  #   logger = Yell.new STDOUT, :format => Yell::BasicFormat
  #   logger.info "Hello World!"
  #   #=> "I, 2012-02-29T09:30:00+01:00 : Hello World!"
  #   #    ^  ^                          ^
  #   #    ^  ISO8601 Timestamp          Message
  #   #    Level (short)
  BasicFormat = "%l, %d : %m"

  # Extended Format
  #
  # @example
  #   logger = Yell.new STDOUT, :format => Yell::ExtendedFormat
  #   logger.info "Hello World!"
  #   #=> "2012-02-29T09:30:00+01:00 [ INFO] 65784 localhost : Hello World!"
  #   #    ^                          ^      ^     ^           ^
  #   #    ISO8601 Timestamp          Level  Pid   Hostname    Message
  ExtendedFormat  = "%d [%5L] %p %h : %m"


  # The +Formatter+ provides a handle to configure your log message style.
  class Formatter

    Table = {
      "m" => "message(event.messages)",    # Message
      "l" => "level(event.level, 1)",      # Level (short), e.g.'I', 'W'
      "L" => "level(event.level)",         # Level, e.g. 'INFO', 'WARN'
      "d" => "date(event.time)",           # ISO8601 Timestamp
      "h" => "event.hostname",             # Hostname
      "p" => "event.pid",                  # PID
      "P" => "event.progname",             # Progname
      "t" => "event.thread_id",            # Thread ID
      "F" => "event.file",                 # Path with filename where the logger was called
      "f" => "File.basename(event.file)",  # Filename where the loger was called
      "M" => "event.method",               # Method name where the logger was called
      "n" => "event.line",                 # Line where the logger was called
      "N" => "event.name"                  # Name of the logger
    }

    # For standard formatted backwards compatibility
    LegacyTable = Hash[ Table.keys.map { |k| [k, 'noop'] } ].merge(
      'm' => 'message(msg)',
      'l' => 'level(event, 1)',
      'L' => 'level(event)',
      'd' => 'date(time)',
      "p" => "$$",
      'P' => 'progname'
    )

    PatternMatcher = /([^%]*)(%\d*)?(#{Table.keys.join('|')})?(.*)/m


    attr_reader :pattern, :date_pattern


    # Initializes a new +Yell::Formatter+.
    #
    # Upon initialization it defines a format method. `format` takes
    # a {Yell::Event} instance as agument in order to apply for desired log
    # message formatting.
    #
    # @example Blank formatter
    #   Formatter.new
    #
    # @example Formatter with a message pattern
    #   Formatter.new("%d [%5L] %p : %m")
    #
    # @example Formatter with a message and date pattern
    #   Formatter.new("%d [%5L] %p : %m", "%D %H:%M:%S.%L")
    #
    # @example Formatter with a message modifier
    #   Formatter.new do |f|
    #     f.modify(Hash) { |h| "Hash: #{h.inspect}" }
    #   end
    def initialize( *args, &block )
      builder = Builder.new(*args, &block)

      @pattern = builder.pattern
      @date_pattern = builder.date_pattern
      @modifier = builder.modifier

      define_date_method!
      define_call_method!
    end

    # Get a pretty string
    def inspect
      "#<#{self.class.name} pattern: #{@pattern.inspect}, date_pattern: #{@date_pattern.inspect}>"
    end


    private

    # Message modifier class to allow different modifiers for different requirements.
    class Modifier
      def initialize
        @repository = {}
      end

      def set( key, &block )
        @repository.merge!(key => block)
      end

      def call( message )
        case
        when mod = @repository[message.class] || @repository[message.class.to_s]
          mod.call(message)
        when message.is_a?(Array)
          message.map { |m| call(m) }.join(" ")
        when message.is_a?(Hash)
          message.map { |k, v| "#{k}: #{v}" }.join(", ")<commit_msg>update formatter.rb to >1.9 hash syntax<commit_after>require 'time'

# TODO: Register custom formats
#
# @example The Yell default fomat
#   Yell::Formatter.register(:default)
#
# @example The Ruby standard logger format
#   Yell::Formatter.register(:stdlogger, "%l, [%d #%p] %5L -- : %m", "%Y-%m-%dT%H:%M:%S.%6N")
#
module Yell #:nodoc:

  # No format on the log message
  #
  # @example
  #   logger = Yell.new STDOUT, format: false
  #   logger.info "Hello World!"
  #   #=> "Hello World!"
  NoFormat = "%m"

  # Default Format
  #
  # @example
  #   logger = Yell.new STDOUT, format: Yell::DefaultFormat
  #   logger.info "Hello World!"
  #   #=> "2012-02-29T09:30:00+01:00 [ INFO] 65784 : Hello World!"
  #   #    ^                         ^       ^       ^
  #   #    ISO8601 Timestamp         Level   Pid     Message
  DefaultFormat = "%d [%5L] %p : %m"

  # Basic Format
  #
  # @example
  #   logger = Yell.new STDOUT, format: Yell::BasicFormat
  #   logger.info "Hello World!"
  #   #=> "I, 2012-02-29T09:30:00+01:00 : Hello World!"
  #   #    ^  ^                          ^
  #   #    ^  ISO8601 Timestamp          Message
  #   #    Level (short)
  BasicFormat = "%l, %d : %m"

  # Extended Format
  #
  # @example
  #   logger = Yell.new STDOUT, format: Yell::ExtendedFormat
  #   logger.info "Hello World!"
  #   #=> "2012-02-29T09:30:00+01:00 [ INFO] 65784 localhost : Hello World!"
  #   #    ^                          ^      ^     ^           ^
  #   #    ISO8601 Timestamp          Level  Pid   Hostname    Message
  ExtendedFormat  = "%d [%5L] %p %h : %m"


  # The +Formatter+ provides a handle to configure your log message style.
  class Formatter

    Table = {
      "m" => "message(event.messages)",    # Message
      "l" => "level(event.level, 1)",      # Level (short), e.g.'I', 'W'
      "L" => "level(event.level)",         # Level, e.g. 'INFO', 'WARN'
      "d" => "date(event.time)",           # ISO8601 Timestamp
      "h" => "event.hostname",             # Hostname
      "p" => "event.pid",                  # PID
      "P" => "event.progname",             # Progname
      "t" => "event.thread_id",            # Thread ID
      "F" => "event.file",                 # Path with filename where the logger was called
      "f" => "File.basename(event.file)",  # Filename where the loger was called
      "M" => "event.method",               # Method name where the logger was called
      "n" => "event.line",                 # Line where the logger was called
      "N" => "event.name"                  # Name of the logger
    }

    # For standard formatted backwards compatibility
    LegacyTable = Hash[ Table.keys.map { |k| [k, 'noop'] } ].merge(
      'm' => 'message(msg)',
      'l' => 'level(event, 1)',
      'L' => 'level(event)',
      'd' => 'date(time)',
      "p" => "$$",
      'P' => 'progname'
    )

    PatternMatcher = /([^%]*)(%\d*)?(#{Table.keys.join('|')})?(.*)/m


    attr_reader :pattern, :date_pattern


    # Initializes a new +Yell::Formatter+.
    #
    # Upon initialization it defines a format method. `format` takes
    # a {Yell::Event} instance as agument in order to apply for desired log
    # message formatting.
    #
    # @example Blank formatter
    #   Formatter.new
    #
    # @example Formatter with a message pattern
    #   Formatter.new("%d [%5L] %p : %m")
    #
    # @example Formatter with a message and date pattern
    #   Formatter.new("%d [%5L] %p : %m", "%D %H:%M:%S.%L")
    #
    # @example Formatter with a message modifier
    #   Formatter.new do |f|
    #     f.modify(Hash) { |h| "Hash: #{h.inspect}" }
    #   end
    def initialize( *args, &block )
      builder = Builder.new(*args, &block)

      @pattern = builder.pattern
      @date_pattern = builder.date_pattern
      @modifier = builder.modifier

      define_date_method!
      define_call_method!
    end

    # Get a pretty string
    def inspect
      "#<#{self.class.name} pattern: #{@pattern.inspect}, date_pattern: #{@date_pattern.inspect}>"
    end


    private

    # Message modifier class to allow different modifiers for different requirements.
    class Modifier
      def initialize
        @repository = {}
      end

      def set( key, &block )
        @repository.merge!("#{key}": block)
      end

      def call( message )
        case
        when mod = @repository[message.class] || @repository[message.class.to_s]
          mod.call(message)
        when message.is_a?(Array)
          message.map { |m| call(m) }.join(" ")
        when message.is_a?(Hash)
          message.map { |k, v| "#{k}: #{v}" }.join(", ")
{'fraction_non_alphanumeric': 0.15611722409848566, 'fraction_numerical': 0.0193920906416821, 'mean_word_length': 2.06715669896425, 'pattern_counts': {'":': 1, '<': 5, '<?xml version=': 0, '>': 57, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 10, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2319449', 'n_tokens_mistral': 3197, 'n_tokens_neox': 3022, 'n_words': 779}