DEV: Apply `Logster.store.ignore` to `DiscourseLogstashLogger` as well (#27819)

This commit updates `DiscourseLogstashLogger#add_with_opts` to avoid
logging messages that matches regexp patterns configured in
`Logster.store.ignore`. Those error logs are mostly triggered by clients
and do not serve any useful purpose.
This commit is contained in:
Alan Guo Xiang Tan 2024-07-10 13:51:42 +08:00 committed by GitHub
parent d4c603984f
commit c9775d5f72
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 24 additions and 3 deletions

View File

@ -83,9 +83,14 @@ class DiscourseLogstashLogger < Logger
# #
# In theory we could get logster to include the exception class and message in opts but logster currently does not # 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. # 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 =~ /\A([^\(\)]+)\s{1}\(([\s\S]+)\)/ if progname == "web-exception"
event["exception.class"] = $1 # `Logster.store.ignore` is set in the logster initializer and is an array of regex patterns.
event["exception.message"] = $2.strip return if Logster.store&.ignore&.any? { |pattern| pattern.match(message) }
if message =~ /\A([^\(\)]+)\s{1}\(([\s\S]+)\)/
event["exception.class"] = $1
event["exception.message"] = $2.strip
end
end end
if (env = opts&.dig(:env)).present? if (env = opts&.dig(:env)).present?

View File

@ -173,5 +173,21 @@ RSpec.describe DiscourseLogstashLogger do
expect(parsed).not_to have_key("request.headers.some_random_header") expect(parsed).not_to have_key("request.headers.some_random_header")
end end
it "does not log the event if message matches a pattern configured by `Logster.store.ignore`" do
original_logster_store_ignore = Logster.store.ignore
Logster.store.ignore = [/^Some::StandardError/]
logger.add(
Logger::ERROR,
"Some::StandardError (this is a normal message)\ntest",
"web-exception",
)
output.rewind
expect(output.read.chomp).to be_empty
ensure
Logster.store.ignore = original_logster_store_ignore
end
end end
end end