<commit_before># frozen_string_literal: true

# Copyright 2017 <NAME>
#
# This software may be modified and distributed under the terms
# of the MIT license. See the LICENSE.txt file for details.

require 'rackstash/helpers'

module Rackstash
  # This class and all its data are immutable after initialization
  class Message
    include Rackstash::Helpers::UTF8

    attr_reader :message

    alias as_json message
    # Messages are implicitly conversible to Strings
    alias to_s message
    alias to_str message

    attr_reader :severity

    attr_reader :progname

    attr_reader :time

    def initialize(message, severity: UNKNOWN, time: Time.now.utc.freeze, progname: PROGNAME)
      @severity = Integer(severity)
      @severity = 0 if @severity < 0

      @time = dup_freeze(time)
      @progname = dup_freeze(progname)

      @message = cleanup(message)
    end

    # @return [String] the guman readable label for the {#severity}.
    # @see {Rackstash.severity_label}
    def severity_label
      Rackstash.severity_label(@severity)
    end

    # @return [Integer] the character length of {#message}.
    def length
      @message.length
    end
    alias size length

    def to_json
      as_json.to_json
    end

    private

    # Sanitize a single mesage to be added to the buffer, can be a single or
    # multi line string
    #
    # @param msg [String, #inspect] a message to be added to the buffer
    # @return [String] the sanitized frozen message
    def cleanup(msg)
      msg = msg.inspect unless msg.is_a?(String)
      utf8_encode(msg)
    end

    def dup_freeze(obj)
      if obj.frozen?
        obj
      else
        obj.dup.freeze rescue obj
      end
    end
  end
end
<commit_msg>Complete code documentation of Rackstash::Message<commit_after># frozen_string_literal: true

# Copyright 2017 <NAME>
#
# This software may be modified and distributed under the terms
# of the MIT license. See the LICENSE.txt file for details.

require 'rackstash/helpers'

module Rackstash
  # A Message wraps a single logged message created by the {Logger}. Here, we
  # store the formatted message itself plus some additional meta-data about the
  # message.
  #
  # In the end, only the `message` field will be included in the final log
  # event. However, the stored meta-data can be useful when filtering or
  # changing the messages of a log event using {Filters} later.
  #
  # All `Message` objects and their respective data are immutable after
  # initialization.
  class Message
    include Rackstash::Helpers::UTF8

    # @return [String] the logged message string. It usually is already
    #   formatted by the {Logger}'s formatter
    attr_reader :message
    alias as_json message
    alias to_s message
    alias to_str message

    # @return [Integer] the numeric severity of the logged message. Usually
    #   corresponds to one of the {SEVERITIES} constants
    attr_reader :severity

    # @return [String] the progname provided (or inferred) during logging of the
    #   message by the {Logger}.
    attr_reader :progname

    # @return [Time] the frozen timestamp of the logged message. While this
    #   timestamp is usually in UTC, it is not guaranteed.
    attr_reader :time

    # @param message [String, #inspect] a message string
    # @param severity [Integer] the numeric severity of the logged message
    # @param time [Time] the timestamp of the logged message
    # @param progname [String] the progname provided (or inferred) during
    #   logging of the message by the {Logger}.
    def initialize(message, severity: UNKNOWN, time: Time.now.utc.freeze, progname: PROGNAME)
      @severity = Integer(severity)
      @severity = 0 if @severity < 0

      @time = dup_freeze(time)
      @progname = dup_freeze(progname)

      @message = cleanup(message)
    end

    # @return [String] the guman readable label for the {#severity}.
    # @see Rackstash.severity_label
    def severity_label
      Rackstash.severity_label(@severity)
    end

    # @return [Integer] the character length of {#message}.
    def length
      @message.length
    end
    alias size length

    # @return [String] A JSON representation of the message string
    def to_json
      as_json.to_json
    end

    private

    # Cleanup the message.
    #
    # @param msg [String, #inspect] A message string. If anything else than
    #   a `String`, we will inspect it.
    # @return [String] the sanitized frozen message
    def cleanup(msg)
      msg = msg.inspect unless msg.is_a?(String)
      utf8_encode(msg)
    end

    def dup_freeze(obj)
      if obj.frozen?
        obj
      else
        obj.dup.freeze rescue obj
      end
    end
  end
end

{'fraction_non_alphanumeric': 0.06950959488272922, 'fraction_numerical': 0.003411513859275053, 'mean_word_length': 2.8864954432477217, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 5, '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': '5490762', 'n_tokens_mistral': 1447, 'n_tokens_neox': 1359, 'n_words': 527}