From 1e097b3707cbe3e89adcff51a6a200d180760375 Mon Sep 17 00:00:00 2001 From: Beth Skurrie Date: Fri, 19 Jun 2020 10:16:12 +1000 Subject: [PATCH] feat: use structured logs for logging errors --- lib/pact_broker/logging.rb | 14 +++++++++++--- lib/pact_broker/logging/default_formatter.rb | 4 +++- 2 files changed, 14 insertions(+), 4 deletions(-) diff --git a/lib/pact_broker/logging.rb b/lib/pact_broker/logging.rb index fca8e56ce..001ef426e 100644 --- a/lib/pact_broker/logging.rb +++ b/lib/pact_broker/logging.rb @@ -31,9 +31,17 @@ def logger=(logger) end def log_error e, description = nil - message = "#{e.class} #{e.message}\n#{e.backtrace.join("\n")}" - message = "#{description} - #{message}" if description - logger.error message + if logger.instance_of?(SemanticLogger::Logger) + if description + logger.error(description, e) + else + logger.error(e) + end + else + message = "#{e.class} #{e.message}\n#{e.backtrace.join("\n")}" + message = "#{description} - #{message}" if description + logger.error message + end if ENV['PACT_BROKER_HIDE_PACTFLOW_MESSAGES'] != 'true' logger.info "\n\n#{'*' * 80}\n\nPrefer it was someone else's job to deal with this error? Check out https://pactflow.io/oss for a hardened, fully supported SaaS version of the Pact Broker with an improved UI + more.\n\n#{'*' * 80}\n" end diff --git a/lib/pact_broker/logging/default_formatter.rb b/lib/pact_broker/logging/default_formatter.rb index 716bc3947..734024001 100644 --- a/lib/pact_broker/logging/default_formatter.rb +++ b/lib/pact_broker/logging/default_formatter.rb @@ -9,7 +9,9 @@ def initialize end def call(log, output) - @formatter.call(log.level.upcase, log.time, nil, log.message) + self.log = log + self.logger = logger + @formatter.call(log.level.upcase, log.time, nil, [tags, named_tags, duration, message, payload, exception].compact.join(" ")) end end end