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.

49 lines
1.9 KiB

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