class LogParser::Message

A single log message.

@attr [String] message

The actual message body from the log.

@attr [String,nil] source_file

The name of the source file this message originated from.
`nil` if it could not be determined.

@attr [Hash<Symbol, Int>,nil] source_lines

A hash with keys `:from` and `:to` mapping to the first and last line index in `source_file` this message pertains to.
`nil` if they could not be determined.

@attr [Hash<Symbol, Int>,nil] log_lines

A hash with keys `:from` and `:to` mapping to the first and last line index in the log file that this message covers.
`nil` if they could not be determined.

@attr [true,false] preformatted

If `true`, `message` should be printed as-is.
Otherwise, whitespace can be eliminated.

@attr [:error,:warning,:info,:debug] level

The severity of this message.

@attr [Class] pattern

The {LogParser::Pattern} this message was matched by.

Attributes

level[RW]
log_lines[RW]
message[RW]
preformatted[RW]
source_file[RW]
source_lines[RW]

Public Class Methods

new(message:, source_file: nil, source_lines: nil, log_lines: nil, preformatted: false, level: :info, pattern: nil) click to toggle source
# File lib/log_parser/message.rb, line 27
def initialize(message:, source_file: nil, source_lines: nil, log_lines: nil,
               preformatted: false, level: :info, pattern: nil)
  @message = message
  @source_file = source_file
  @source_lines = source_lines
  @log_lines = log_lines
  @preformatted = preformatted
  @level = level
  @pattern = pattern
end

Public Instance Methods

to_json(_options = {}) click to toggle source

Convert this message to JSON.

@return [String]

The JSON string representing this message.
# File lib/log_parser/message.rb, line 66
def to_json(_options = {})
  hash = {
    level: @level,
    source_file: @source_file,
    source_lines: @source_lines,
    message: @message,
    log_lines: @log_lines,
    preformatted: @preformatted
  }
  hash[:pattern] = @pattern if Logger.debug?
  JSON.pretty_generate hash
end
to_s() click to toggle source

Convert this message to a file-line string representation.

@return [String]

# File lib/log_parser/message.rb, line 44
    def to_s
      lines = if @source_lines.nil?
                ''
              else
                # @type [Hash<Symbol, Int>] @source_lines
                ":#{@source_lines.values.uniq.join('-')}"
              end

      message = @message
      message = message.split("\n").map(&:strip).join(' ') unless @preformatted
      message += "\nLog pattern: '#{@pattern}'" if Logger.debug?

      <<~MSG
        #{@source_file}#{lines}: #{@level.to_s.upcase}
        #{message}
      MSG
    end