From 46e6a6fda476a1a07aec799d4a1e0f135400ae27 Mon Sep 17 00:00:00 2001 From: Thorsten Eckel Date: Mon, 15 May 2017 12:00:02 +0200 Subject: [PATCH] Fixed bug: Exception logging was broken since Logger::Formatter was used and ActiveSupport::TaggedLogging::Formatter broke it. --- .../lib/active_support/logger.rb | 29 ------------------ .../tagged_logging/formatter.rb | 30 +++++++++++++++++++ 2 files changed, 30 insertions(+), 29 deletions(-) delete mode 100644 lib/core_ext/activesupport/lib/active_support/logger.rb create mode 100644 lib/core_ext/activesupport/lib/active_support/tagged_logging/formatter.rb diff --git a/lib/core_ext/activesupport/lib/active_support/logger.rb b/lib/core_ext/activesupport/lib/active_support/logger.rb deleted file mode 100644 index 70213ee8a..000000000 --- a/lib/core_ext/activesupport/lib/active_support/logger.rb +++ /dev/null @@ -1,29 +0,0 @@ -# This customization provides the possiblity to log exception backtraces via the Rails.logger. -# -# @example: -# begin -# instance = "String :)" -# instance.invalid_method -# rescue => e -# Rails.logger.error e -# end -# #=> undefined method `invalid_method' for "String :)":String -# # ... backtrace ... -# https://github.com/rails/rails/blob/308e84e982b940983b4b3d5b41b0b3ac11fbae40/activesupport/lib/active_support/logger.rb#L101 -module ActiveSupport - class Logger < ::Logger - class SimpleFormatter < ::Logger::Formatter - # original behaviour: - # rubocop:disable Lint/UnusedMethodArgument, Style/CaseEquality - # This method is invoked when a log event occurs - def call(severity, timestamp, progname, msg) - return "#{String === msg ? msg : msg.inspect}\n" if !msg.is_a?(Exception) - # rubocop:enable Lint/UnusedMethodArgument, Style/CaseEquality - # custom -> print only the message if no backtrace is present - return "#{msg.message}\n" if !msg.backtrace - # otherwise combination of message and backtrace - "#{msg.message}\n#{msg.backtrace.join("\n")}\n" - end - end - end -end diff --git a/lib/core_ext/activesupport/lib/active_support/tagged_logging/formatter.rb b/lib/core_ext/activesupport/lib/active_support/tagged_logging/formatter.rb new file mode 100644 index 000000000..87d0a7286 --- /dev/null +++ b/lib/core_ext/activesupport/lib/active_support/tagged_logging/formatter.rb @@ -0,0 +1,30 @@ +# This customization removes the tagged logging functionality in favour of logging exception backtraces via the Rails.logger. +# Zammad uses Logger::Formatter which partly provides the functionality to log exceptions if given. +# ActiveSupport::TaggedLogging::Formatter removes this by addind the tags as a string which converts the Exception class to +# a flat string without the backtrace and other information. It's reduced to only the exception text. This is not wanted +# in our context. +# ActiveSupport::TaggedLogging::Formatter addresses: +# subdomains, request ids, and anything else to aid debugging of multi-user production applications. +# Which is not needed for us +# +# @example: +# begin +# instance = "String :)" +# instance.invalid_method +# rescue => e +# Rails.logger.error e +# end +# #=> undefined method `invalid_method' for "String :)":String +# # ... backtrace ... +# https://github.com/rails/rails/blob/89fab56597c335bb49887563b9a98386b5171574/activesupport/lib/active_support/tagged_logging.rb +module ActiveSupport + module TaggedLogging + module Formatter + # This method is invoked when a log event occurs. + def call(severity, timestamp, progname, msg) + # super(severity, timestamp, progname, "#{tags_text}#{msg}") + super(severity, timestamp, progname, msg) + end + end + end +end