You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

46 lines
1.9 KiB

10 months ago
10 months ago
10 months ago
10 months ago
10 months ago
  1. # MonkeyPatch of SemanticLogger
  2. # provides better handling for payload regardless of message
  3. module SemanticLogger
  4. class Base # rubocop:disable Style/Documentation
  5. # Log message at the specified level
  6. def log_internal(level, index, message = nil, payload = nil, exception = nil) # rubocop:disable Metrics/AbcSize,Metrics/CyclomaticComplexity,Metrics/MethodLength,Metrics/PerceivedComplexity
  7. # Handle variable number of arguments by detecting exception object and payload hash.
  8. if exception.nil? && payload.nil? && message.respond_to?(:backtrace) && message.respond_to?(:message)
  9. exception = message
  10. message = nil
  11. elsif exception.nil? && payload && payload.respond_to?(:backtrace) && payload.respond_to?(:message)
  12. exception = payload
  13. payload = nil
  14. elsif message && !message.is_a?(String)
  15. payload = message
  16. message = 'payload is'
  17. end
  18. log = Log.new(name, level, index)
  19. should_log =
  20. if exception.nil? && payload.nil? && message.is_a?(Hash)
  21. # All arguments as a hash in the message.
  22. log.assign(**log.extract_arguments(message))
  23. elsif exception.nil? && message && payload && payload.is_a?(Hash)
  24. # Message supplied along with a hash with the remaining arguments.
  25. log.assign(**log.extract_arguments(payload, message))
  26. else
  27. # All fields supplied directly.
  28. log.assign(message:, payload:, exception:)
  29. end
  30. # Add result of block to message or payload if not nil
  31. if block_given?
  32. result = yield(log)
  33. if result.is_a?(String)
  34. log.message = log.message.nil? ? result : "#{log.message} -- #{result}"
  35. elsif result.is_a?(Hash)
  36. log.assign_hash(result)
  37. end
  38. end
  39. # Log level may change during assign due to :on_exception_level
  40. self.log(log) if should_log && should_log?(log)
  41. end
  42. end
  43. end