Class: LogParser::Message
- Inherits:
-
Object
- Object
- LogParser::Message
- Defined in:
- lib/log_parser/message.rb
Overview
A single log message.
Instance Attribute Summary collapse
-
#level ⇒ :error, ...
The severity of this message.
-
#log_lines ⇒ Hash<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.
-
#message ⇒ String
The actual message body from the log.
-
#pattern ⇒ Class
The Pattern this message was matched by.
-
#preformatted ⇒ true, false
If ‘true`, `message` should be printed as-is.
-
#source_file ⇒ String?
The name of the source file this message originated from.
-
#source_lines ⇒ Hash<Symbol, Int>?
A hash with keys ‘:from` and `:to` mapping to the first and last line index in `source_file` this message pertains to.
Instance Method Summary collapse
-
#initialize(message:, source_file: nil, source_lines: nil, log_lines: nil, preformatted: false, level: :info, pattern: nil) ⇒ Message
constructor
A new instance of Message.
-
#to_json(_options = {}) ⇒ String
Convert this message to JSON.
-
#to_s ⇒ String
Convert this message to a file-line string representation.
Constructor Details
permalink #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 = @source_file = source_file @source_lines = source_lines @log_lines = log_lines @preformatted = preformatted @level = level @pattern = pattern end |
Instance Attribute Details
permalink #level ⇒ :error, ...
The severity of this message.
26 27 28 |
# File 'lib/log_parser/message.rb', line 26 def level @level end |
permalink #log_lines ⇒ Hash<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.
26 27 28 |
# File 'lib/log_parser/message.rb', line 26 def log_lines @log_lines end |
permalink #message ⇒ String
The actual message body from the log.
26 27 28 |
# File 'lib/log_parser/message.rb', line 26 def @message end |
permalink #pattern ⇒ Class
The Pattern this message was matched by.
26 27 28 |
# File 'lib/log_parser/message.rb', line 26 def pattern @pattern end |
permalink #preformatted ⇒ true, false
If ‘true`, `message` should be printed as-is. Otherwise, whitespace can be eliminated.
26 27 28 |
# File 'lib/log_parser/message.rb', line 26 def preformatted @preformatted end |
permalink #source_file ⇒ String?
The name of the source file this message originated from. ‘nil` if it could not be determined.
26 27 28 |
# File 'lib/log_parser/message.rb', line 26 def source_file @source_file end |
permalink #source_lines ⇒ Hash<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.
26 27 28 |
# File 'lib/log_parser/message.rb', line 26 def source_lines @source_lines end |
Instance Method Details
permalink #to_json(_options = {}) ⇒ String
Convert this message to JSON.
66 67 68 69 70 71 72 73 74 75 76 77 |
# File 'lib/log_parser/message.rb', line 66 def to_json( = {}) 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 |
permalink #to_s ⇒ String
Convert this message to a file-line string representation.
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 = .split("\n").map(&:strip).join(' ') unless @preformatted += "\nLog pattern: '#{@pattern}'" if Logger.debug? <<~MSG #{@source_file}#{lines}: #{@level.to_s.upcase} #{} MSG end |