A platform for community discussion. Free, open, simple.
DEV: Add exception class/message to `DiscourseLogstashLogger` take 2 (#27815)
Alan Guo Xiang Tanb4b7fa17aff0
diff --git a/lib/discourse_logstash_logger.rb b/lib/discourse_logstash_logger.rbindex 8a7bcc04e4a..e25490104d3 100644--- a/lib/discourse_logstash_logger.rb+++ b/lib/discourse_logstash_logger.rb@@ -83,9 +83,9 @@ class DiscourseLogstashLogger < Logger## In theory we could get logster to include the exception class and message in opts but logster currently does not# need those options so we are parsing it from the message for now and not making a change in logster.- if progname == "web-exception" && message =~ /^(\w+) \((.+)\)\n/+ if progname == "web-exception" && message =~ /\A([^\(\)]+)\s{1}\(([\s\S]+)\)/event["exception.class"] = $1- event["exception.message"] = $2+ event["exception.message"] = $2.stripendif (env = opts&.dig(:env)).present?diff --git a/spec/lib/discourse_logstash_logger_spec.rb b/spec/lib/discourse_logstash_logger_spec.rbindex 857eca4d93c..639b16581ec 100644--- a/spec/lib/discourse_logstash_logger_spec.rb+++ b/spec/lib/discourse_logstash_logger_spec.rb@@ -51,12 +51,34 @@ RSpec.describe DiscourseLogstashLogger doit "logs a JSON string with the `exception_class` and `exception_message` fields when `progname` is `web-exception`" dologger = described_class.logger(logdev: output, type: "test")- logger.add(Logger::ERROR, "StandardError (some error message)\ntest", "web-exception")++ logger.add(+ Logger::ERROR,+ "Some::StandardError (this is a normal message)\ntest",+ "web-exception",+ )++ output.rewind+ parsed = JSON.parse(output.read.chomp)++ expect(parsed["exception.class"]).to eq("Some::StandardError")+ expect(parsed["exception.message"]).to eq("this is a normal message")+ end++ it "logs a JSON string with the `exception_class` and `exception_message` fields when `progname` is `web-exception` and the exception message contains newlines" do+ logger = described_class.logger(logdev: output, type: "test")++ logger.add(+ Logger::ERROR,+ "Some::StandardError (\n\nsome error message\n\nsomething else\n\n)\ntest",+ "web-exception",+ )+output.rewindparsed = JSON.parse(output.read.chomp)- expect(parsed["exception.class"]).to eq("StandardError")- expect(parsed["exception.message"]).to eq("some error message")+ expect(parsed["exception.class"]).to eq("Some::StandardError")+ expect(parsed["exception.message"]).to eq("some error message\n\nsomething else")endit "logs a JSON string with the right fields when `customize_event` attribute is set" do