Module: JsonLogging::MessageParser

Defined in:
lib/json_logging/message_parser.rb

Class Method Summary collapse

Class Method Details

.json_string?(str) ⇒ Boolean

Returns:

  • (Boolean)


27
28
29
30
# File 'lib/json_logging/message_parser.rb', line 27

def json_string?(str)
  (str.start_with?("{") && str.end_with?("}")) ||
    (str.start_with?("[") && str.end_with?("]"))
end

.parse_message(msg) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/json_logging/message_parser.rb', line 5

def parse_message(msg)
  if msg.is_a?(Hash) || (msg.respond_to?(:to_hash) && !msg.is_a?(String))
    # Sanitize hash messages
    Sanitizer.sanitize_hash(msg.to_hash)
  elsif msg.is_a?(String) && json_string?(msg)
    begin
      parsed = JSON.parse(msg)
      # Sanitize parsed JSON structure
      Sanitizer.sanitize_value(parsed)
    rescue JSON::ParserError
      # If JSON parsing fails, sanitize the raw string
      Sanitizer.sanitize_string(msg)
    end
  elsif msg.is_a?(Exception)
    # Handle exceptions specially with sanitization
    Sanitizer.sanitize_exception(msg)
  else
    # Sanitize other types
    Sanitizer.sanitize_value(msg)
  end
end