2019-05-02 18:17:27 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2024-07-08 22:00:44 -04:00
|
|
|
if ENV["ENABLE_LOGSTASH_LOGGER"] == "1"
|
2024-07-08 21:25:46 -04:00
|
|
|
require "lograge"
|
2017-10-27 10:54:50 -04:00
|
|
|
|
2024-07-08 21:25:46 -04:00
|
|
|
Rails.application.config.after_initialize do
|
|
|
|
def unsubscribe(component_name, subscriber)
|
|
|
|
subscriber
|
|
|
|
.public_methods(false)
|
|
|
|
.reject { |method| method.to_s == "call" }
|
|
|
|
.each do |event|
|
|
|
|
ActiveSupport::Notifications
|
|
|
|
.notifier
|
|
|
|
.all_listeners_for("#{event}.#{component_name}")
|
|
|
|
.each do |listener|
|
|
|
|
if listener
|
|
|
|
.instance_variable_get("@delegate")
|
|
|
|
.class
|
|
|
|
.to_s
|
|
|
|
.start_with?("#{component_name.to_s.classify}::LogSubscriber")
|
|
|
|
ActiveSupport::Notifications.unsubscribe listener
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# This is doing what the `lograge` gem is doing but has stopped working after we upgraded to Rails 7.1 and the `lograge`
|
|
|
|
# gem does not seem to be maintained anymore so we're shipping our own fix. In the near term, we are considering
|
|
|
|
# dropping the lograge gem and just port the relevant code to our codebase.
|
|
|
|
#
|
|
|
|
# Basically, this code silences log events coming from `ActionView::Logsubscriber` and `ActionController::LogSubscriber`
|
|
|
|
# since those are just noise.
|
|
|
|
ActiveSupport::LogSubscriber.log_subscribers.each do |subscriber|
|
|
|
|
case subscriber
|
|
|
|
when ActionView::LogSubscriber
|
|
|
|
unsubscribe(:action_view, subscriber)
|
|
|
|
when ActionController::LogSubscriber
|
|
|
|
unsubscribe(:action_controller, subscriber)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
Rails.application.config.to_prepare do
|
2022-03-21 10:28:52 -04:00
|
|
|
if Rails.configuration.multisite
|
2024-06-20 04:33:01 -04:00
|
|
|
Rails.logger.formatter =
|
|
|
|
ActiveSupport::Logger::SimpleFormatter.new.extend(ActiveSupport::TaggedLogging::Formatter)
|
2022-03-21 10:28:52 -04:00
|
|
|
end
|
2017-11-14 20:02:34 -05:00
|
|
|
|
2022-03-21 10:28:52 -04:00
|
|
|
Rails.application.configure do
|
|
|
|
config.lograge.enabled = true
|
|
|
|
|
2024-07-07 20:42:58 -04:00
|
|
|
# Monkey patch Rails::Rack::Logger#logger to silence its logs.
|
|
|
|
# The `lograge` gem is supposed to do this but it broke after we upgraded to Rails 7.1.
|
|
|
|
# This patch is a temporary workaround until we find a proper fix.
|
|
|
|
Rails::Rack::Logger.prepend(Module.new { def logger = (@logger ||= Logger.new(IO::NULL)) })
|
|
|
|
|
2022-03-21 10:28:52 -04:00
|
|
|
Lograge.ignore(
|
|
|
|
lambda do |event|
|
|
|
|
# this is our hijack magic status,
|
|
|
|
# no point logging this cause we log again
|
|
|
|
# direct from hijack
|
|
|
|
event.payload[:status] == 418
|
|
|
|
end,
|
|
|
|
)
|
|
|
|
|
|
|
|
config.lograge.custom_payload do |controller|
|
|
|
|
begin
|
|
|
|
username =
|
|
|
|
begin
|
|
|
|
controller.current_user&.username if controller.respond_to?(:current_user)
|
|
|
|
rescue Discourse::InvalidAccess, Discourse::ReadOnly, ActiveRecord::ReadOnlyError
|
|
|
|
nil
|
2018-08-19 22:58:56 -04:00
|
|
|
end
|
2017-12-12 04:22:38 -05:00
|
|
|
|
2022-03-21 10:28:52 -04:00
|
|
|
ip =
|
|
|
|
begin
|
|
|
|
controller.request.remote_ip
|
|
|
|
rescue ActionDispatch::RemoteIp::IpSpoofAttackError
|
|
|
|
nil
|
|
|
|
end
|
2017-12-12 04:22:38 -05:00
|
|
|
|
2022-03-21 10:28:52 -04:00
|
|
|
{ ip: ip, username: username }
|
|
|
|
rescue => e
|
|
|
|
Rails.logger.warn(
|
|
|
|
"Failed to append custom payload: #{e.message}\n#{e.backtrace.join("\n")}",
|
|
|
|
)
|
|
|
|
{}
|
|
|
|
end
|
2017-12-07 19:30:48 -05:00
|
|
|
end
|
2017-12-04 22:51:03 -05:00
|
|
|
|
2022-03-21 10:28:52 -04:00
|
|
|
config.lograge.custom_options =
|
|
|
|
lambda do |event|
|
|
|
|
begin
|
|
|
|
exceptions = %w[controller action format id]
|
2017-10-27 10:54:50 -04:00
|
|
|
|
2022-03-21 10:28:52 -04:00
|
|
|
params = event.payload[:params].except(*exceptions)
|
2018-07-18 20:23:59 -04:00
|
|
|
|
2022-03-21 10:28:52 -04:00
|
|
|
if (file = params[:file]) && file.respond_to?(:headers)
|
|
|
|
params[:file] = file.headers
|
|
|
|
end
|
2018-07-18 20:23:59 -04:00
|
|
|
|
2022-03-21 10:28:52 -04:00
|
|
|
if (files = params[:files]) && files.respond_to?(:map)
|
|
|
|
params[:files] = files.map { |f| f.respond_to?(:headers) ? f.headers : f }
|
|
|
|
end
|
2017-10-31 20:37:11 -04:00
|
|
|
|
2022-03-21 10:28:52 -04:00
|
|
|
output = {
|
|
|
|
params: params.to_query,
|
|
|
|
database: RailsMultisite::ConnectionManagement.current_db,
|
|
|
|
}
|
2017-11-02 02:40:18 -04:00
|
|
|
|
2022-03-21 10:28:52 -04:00
|
|
|
if data = (Thread.current[:_method_profiler] || event.payload[:timings])
|
|
|
|
sql = data[:sql]
|
2017-11-28 01:00:13 -05:00
|
|
|
|
2022-03-21 10:28:52 -04:00
|
|
|
if sql
|
|
|
|
output[:db] = sql[:duration] * 1000
|
|
|
|
output[:db_calls] = sql[:calls]
|
|
|
|
end
|
2017-11-28 01:00:13 -05:00
|
|
|
|
2022-03-21 10:28:52 -04:00
|
|
|
redis = data[:redis]
|
2017-11-28 01:00:13 -05:00
|
|
|
|
2022-03-21 10:28:52 -04:00
|
|
|
if redis
|
|
|
|
output[:redis] = redis[:duration] * 1000
|
|
|
|
output[:redis_calls] = redis[:calls]
|
|
|
|
end
|
2018-02-20 23:19:59 -05:00
|
|
|
|
2022-03-21 10:28:52 -04:00
|
|
|
net = data[:net]
|
2018-02-20 23:19:59 -05:00
|
|
|
|
2022-03-21 10:28:52 -04:00
|
|
|
if net
|
|
|
|
output[:net] = net[:duration] * 1000
|
|
|
|
output[:net_calls] = net[:calls]
|
2023-01-07 06:59:28 -05:00
|
|
|
end
|
2022-03-21 10:28:52 -04:00
|
|
|
end
|
2017-11-24 19:10:49 -05:00
|
|
|
|
2022-03-21 10:28:52 -04:00
|
|
|
output
|
|
|
|
rescue RateLimiter::LimitExceeded
|
|
|
|
# no idea who this is, but they are limited
|
|
|
|
{}
|
|
|
|
rescue => e
|
|
|
|
Rails.logger.warn(
|
|
|
|
"Failed to append custom options: #{e.message}\n#{e.backtrace.join("\n")}",
|
|
|
|
)
|
|
|
|
{}
|
2023-01-07 06:59:28 -05:00
|
|
|
end
|
2022-03-21 10:28:52 -04:00
|
|
|
end
|
2017-10-31 20:37:11 -04:00
|
|
|
|
2024-07-08 22:00:44 -04:00
|
|
|
config.lograge.formatter = Lograge::Formatters::Logstash.new
|
2017-11-13 23:50:26 -05:00
|
|
|
|
2024-07-08 22:00:44 -04:00
|
|
|
require "discourse_logstash_logger"
|
2017-11-13 23:50:26 -05:00
|
|
|
|
2024-07-08 22:00:44 -04:00
|
|
|
config.lograge.logger =
|
|
|
|
DiscourseLogstashLogger.logger(
|
|
|
|
logdev: Rails.root.join("log", "#{Rails.env}.log"),
|
|
|
|
type: :rails,
|
|
|
|
customize_event:
|
|
|
|
lambda { |event| event["database"] = RailsMultisite::ConnectionManagement.current_db },
|
2024-07-08 02:03:11 -04:00
|
|
|
)
|
2018-04-13 00:08:27 -04:00
|
|
|
|
2024-07-08 22:00:44 -04:00
|
|
|
# Stop broadcasting to Rails' default logger
|
|
|
|
Rails.logger.stop_broadcasting_to(
|
|
|
|
Rails.logger.broadcasts.find { |logger| logger.is_a?(ActiveSupport::Logger) },
|
|
|
|
)
|
|
|
|
|
|
|
|
Logster.logger.subscribe do |severity, message, progname, opts, &block|
|
|
|
|
config.lograge.logger.add_with_opts(severity, message, progname, opts, &block)
|
2022-03-21 10:28:52 -04:00
|
|
|
end
|
2017-10-31 20:37:11 -04:00
|
|
|
end
|
2017-10-27 05:54:45 -04:00
|
|
|
end
|
|
|
|
end
|