diff --git a/Gemfile.lock b/Gemfile.lock index 521090e7948..11f3f74bbd3 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -184,7 +184,7 @@ GEM logstash-event (1.2.02) logstash-logger (0.26.1) logstash-event (~> 1.2) - logster (1.2.9) + logster (1.2.11) loofah (2.2.2) crass (~> 1.0.2) nokogiri (>= 1.5.9) diff --git a/lib/discourse.rb b/lib/discourse.rb index 79be1a74644..de70d4834d1 100644 --- a/lib/discourse.rb +++ b/lib/discourse.rb @@ -489,6 +489,41 @@ module Discourse nil end + # you can use Discourse.warn when you want to report custom environment + # with the error, this helps with grouping + def self.warn(message, env = nil) + append = env ? (+" ") << env.map { |k, v|"#{k}: #{v}" }.join(" ") : "" + + if !(Logster::Logger === Rails.logger) + Rails.logger.warn("#{message}#{append}") + return + end + + loggers = [Rails.logger] + if Rails.logger.chained + loggers.concat(Rails.logger.chained) + end + + if old_env = Thread.current[Logster::Logger::LOGSTER_ENV] + env = env.merge(old_env) + end + + loggers.each do |logger| + + if !(Logster::Logger === logger) + logger.warn("#{message} #{append}") + next + end + + logger.store.report( + ::Logger::Severity::WARN, + "discourse", + message, + env: env + ) + end + end + # report a warning maintaining backtrack for logster def self.warn_exception(e, message: "", env: nil) if Rails.logger.respond_to? :add_with_opts diff --git a/lib/middleware/request_tracker.rb b/lib/middleware/request_tracker.rb index 0c766104642..26680bf9c62 100644 --- a/lib/middleware/request_tracker.rb +++ b/lib/middleware/request_tracker.rb @@ -258,7 +258,7 @@ class Middleware::RequestTracker if !limiter_assets10.can_perform? if warn - Rails.logger.warn("Global asset IP rate limit exceeded for #{ip}: 10 second rate limit, uri: #{request.env["REQUEST_URI"]}") + Discourse.warn("Global asset IP rate limit exceeded for #{ip}: 10 second rate limit", uri: request.env["REQUEST_URI"]) end return !(GlobalSetting.max_reqs_per_ip_mode == "warn") @@ -272,7 +272,7 @@ class Middleware::RequestTracker false rescue RateLimiter::LimitExceeded if warn - Rails.logger.warn("Global IP rate limit exceeded for #{ip}: #{type} second rate limit, uri: #{request.env["REQUEST_URI"]}") + Discourse.warn("Global IP rate limit exceeded for #{ip}: #{type} second rate limit", uri: request.env["REQUEST_URI"]) !(GlobalSetting.max_reqs_per_ip_mode == "warn") else true