formatter.rb 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. # This customization adds the id of the current Thread to all log lines.
  3. # The #msg2str method will be extended, so that the "Rails.bracktrace_cleaner" can be used to clean the exceptions.
  4. # It was introduced to make it more easy to follow the execution of tasks in the log in threaded processes.
  5. #
  6. # before:
  7. # D, [2018-11-20T16:35:03.483547 #72102] DEBUG -- : (0.5ms) SELECT COUNT(*) FROM "delayed_jobs"
  8. #
  9. # after:
  10. # D, [2018-11-20T16:35:03.483547 #72102-23423534] DEBUG -- : (0.5ms) SELECT COUNT(*) FROM "delayed_jobs"
  11. class Logger
  12. class Formatter
  13. # original: Format = "%s, [%s#%d] %5s -- %s: %s\n".freeze
  14. FORMAT_WITH_THREAD_ID = "%s, [%s#%d-%d] %5s -- %s: %s\n".freeze
  15. def call(severity, time, progname, msg)
  16. format(FORMAT_WITH_THREAD_ID, severity[0..0], format_datetime(time), Process.pid, Thread.current.object_id, severity, progname, msg2str(msg))
  17. end
  18. private
  19. def msg2str(msg)
  20. case msg
  21. when ::String
  22. msg
  23. when ::Exception
  24. # "#{ msg.message } (#{ msg.class })\n#{ msg.backtrace.join("\n") if msg.backtrace }"
  25. "#{msg.message} (#{msg.class})\n#{Rails.backtrace_cleaner.clean(msg.backtrace).join("\n") if msg.backtrace}"
  26. else
  27. msg.inspect
  28. end
  29. end
  30. end
  31. end