Class: LogParser::Message

Inherits:
Object
  • Object
show all
Defined in:
lib/log_parser/message.rb

Overview

A single log message.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(message:, source_file: nil, source_lines: nil, log_lines: nil, preformatted: false, level: :info, pattern: nil) ⇒ Message

Returns a new instance of Message.



27
28
29
30
31
32
33
34
35
36
# 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

Instance Attribute Details

#level:error, ...

The severity of this message.

Returns:

  • (:error, :warning, :info, :debug)

    the current value of level



26
27
28
# File 'lib/log_parser/message.rb', line 26

def level
  @level
end

#log_linesHash<Symbol, Int>?

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.

Returns:

  • (Hash<Symbol, Int>, nil)

    the current value of log_lines



26
27
28
# File 'lib/log_parser/message.rb', line 26

def log_lines
  @log_lines
end

#messageString

The actual message body from the log.

Returns:

  • (String)

    the current value of message



26
27
28
# File 'lib/log_parser/message.rb', line 26

def message
  @message
end

#patternClass

The Pattern this message was matched by.

Returns:

  • (Class)

    the current value of pattern



26
27
28
# File 'lib/log_parser/message.rb', line 26

def pattern
  @pattern
end

#preformattedtrue, false

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

Returns:

  • (true, false)

    the current value of preformatted



26
27
28
# File 'lib/log_parser/message.rb', line 26

def preformatted
  @preformatted
end

#source_fileString?

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

Returns:

  • (String, nil)

    the current value of source_file



26
27
28
# File 'lib/log_parser/message.rb', line 26

def source_file
  @source_file
end

#source_linesHash<Symbol, Int>?

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.

Returns:

  • (Hash<Symbol, Int>, nil)

    the current value of source_lines



26
27
28
# File 'lib/log_parser/message.rb', line 26

def source_lines
  @source_lines
end

Instance Method Details

#to_json(_options = {}) ⇒ String

Convert this message to JSON.

Returns:

  • (String)

    The JSON string representing this message.



66
67
68
69
70
71
72
73
74
75
76
77
# 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_sString

Convert this message to a file-line string representation.

Returns:

  • (String)


44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# 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