2019-05-03 06:17:27 +08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2022-03-21 22:28:52 +08:00
|
|
|
Rails.application.config.to_prepare do
|
|
|
|
if (Rails.env.production? && SiteSetting.logging_provider == "lograge") ||
|
|
|
|
(ENV["ENABLE_LOGRAGE"] == "1")
|
|
|
|
require "lograge"
|
2017-10-27 22:54:50 +08:00
|
|
|
|
2022-03-21 22:28:52 +08:00
|
|
|
if Rails.configuration.multisite
|
2024-06-20 16:33:01 +08:00
|
|
|
Rails.logger.formatter =
|
|
|
|
ActiveSupport::Logger::SimpleFormatter.new.extend(ActiveSupport::TaggedLogging::Formatter)
|
2022-03-21 22:28:52 +08:00
|
|
|
end
|
2017-11-15 09:02:34 +08:00
|
|
|
|
2022-03-21 22:28:52 +08:00
|
|
|
Rails.application.configure do
|
|
|
|
config.lograge.enabled = true
|
|
|
|
|
|
|
|
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-20 10:58:56 +08:00
|
|
|
end
|
2017-12-12 17:22:38 +08:00
|
|
|
|
2022-03-21 22:28:52 +08:00
|
|
|
ip =
|
|
|
|
begin
|
|
|
|
controller.request.remote_ip
|
|
|
|
rescue ActionDispatch::RemoteIp::IpSpoofAttackError
|
|
|
|
nil
|
|
|
|
end
|
2017-12-12 17:22:38 +08:00
|
|
|
|
2022-03-21 22:28:52 +08: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-08 08:30:48 +08:00
|
|
|
end
|
2017-12-05 11:51:03 +08:00
|
|
|
|
2022-03-21 22:28:52 +08:00
|
|
|
config.lograge.custom_options =
|
|
|
|
lambda do |event|
|
|
|
|
begin
|
|
|
|
exceptions = %w[controller action format id]
|
2017-10-27 22:54:50 +08:00
|
|
|
|
2022-03-21 22:28:52 +08:00
|
|
|
params = event.payload[:params].except(*exceptions)
|
2018-07-19 08:23:59 +08:00
|
|
|
|
2022-03-21 22:28:52 +08:00
|
|
|
if (file = params[:file]) && file.respond_to?(:headers)
|
|
|
|
params[:file] = file.headers
|
|
|
|
end
|
2018-07-19 08:23:59 +08:00
|
|
|
|
2022-03-21 22:28:52 +08:00
|
|
|
if (files = params[:files]) && files.respond_to?(:map)
|
|
|
|
params[:files] = files.map { |f| f.respond_to?(:headers) ? f.headers : f }
|
|
|
|
end
|
2017-11-01 08:37:11 +08:00
|
|
|
|
2022-03-21 22:28:52 +08:00
|
|
|
output = {
|
|
|
|
params: params.to_query,
|
|
|
|
database: RailsMultisite::ConnectionManagement.current_db,
|
|
|
|
}
|
2017-11-02 14:40:18 +08:00
|
|
|
|
2022-03-21 22:28:52 +08:00
|
|
|
if data = (Thread.current[:_method_profiler] || event.payload[:timings])
|
|
|
|
sql = data[:sql]
|
2017-11-28 14:00:13 +08:00
|
|
|
|
2022-03-21 22:28:52 +08:00
|
|
|
if sql
|
|
|
|
output[:db] = sql[:duration] * 1000
|
|
|
|
output[:db_calls] = sql[:calls]
|
|
|
|
end
|
2017-11-28 14:00:13 +08:00
|
|
|
|
2022-03-21 22:28:52 +08:00
|
|
|
redis = data[:redis]
|
2017-11-28 14:00:13 +08:00
|
|
|
|
2022-03-21 22:28:52 +08:00
|
|
|
if redis
|
|
|
|
output[:redis] = redis[:duration] * 1000
|
|
|
|
output[:redis_calls] = redis[:calls]
|
|
|
|
end
|
2018-02-21 12:19:59 +08:00
|
|
|
|
2022-03-21 22:28:52 +08:00
|
|
|
net = data[:net]
|
2018-02-21 12:19:59 +08:00
|
|
|
|
2022-03-21 22:28:52 +08:00
|
|
|
if net
|
|
|
|
output[:net] = net[:duration] * 1000
|
|
|
|
output[:net_calls] = net[:calls]
|
2023-01-07 19:59:28 +08:00
|
|
|
end
|
2022-03-21 22:28:52 +08:00
|
|
|
end
|
2017-11-25 08:10:49 +08:00
|
|
|
|
2022-03-21 22:28:52 +08: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 19:59:28 +08:00
|
|
|
end
|
2022-03-21 22:28:52 +08:00
|
|
|
end
|
2017-11-01 08:37:11 +08:00
|
|
|
|
2024-07-05 09:41:52 +08:00
|
|
|
if ENV["ENABLE_LOGSTASH_LOGGER"] == "1"
|
2022-03-21 22:28:52 +08:00
|
|
|
config.lograge.formatter = Lograge::Formatters::Logstash.new
|
2017-11-14 12:50:26 +08:00
|
|
|
|
2022-03-21 22:28:52 +08:00
|
|
|
require "discourse_logstash_logger"
|
2017-11-14 12:50:26 +08:00
|
|
|
|
2022-03-21 22:28:52 +08:00
|
|
|
config.lograge.logger =
|
2024-07-05 09:41:52 +08:00
|
|
|
DiscourseLogstashLogger.logger(
|
|
|
|
logdev: Rails.root.join("log", "#{Rails.env}.log"),
|
|
|
|
type: :rails,
|
|
|
|
customize_event:
|
|
|
|
lambda do |event|
|
|
|
|
event["database"] = RailsMultisite::ConnectionManagement.current_db
|
|
|
|
end,
|
|
|
|
)
|
|
|
|
|
|
|
|
# Stop broadcasting to Rails' default logger
|
|
|
|
Rails.logger.stop_broadcasting_to(
|
|
|
|
Rails.logger.broadcasts.find { |logger| logger.is_a?(ActiveSupport::Logger) },
|
|
|
|
)
|
2018-04-13 12:08:27 +08:00
|
|
|
|
2024-07-05 09:41:52 +08:00
|
|
|
Logster.logger.subscribe do |severity, message, progname, opts, &block|
|
|
|
|
config.lograge.logger.add_with_opts(severity, message, progname, opts, &block)
|
|
|
|
end
|
2022-03-21 22:28:52 +08:00
|
|
|
end
|
2017-11-01 08:37:11 +08:00
|
|
|
end
|
2017-10-27 17:54:45 +08:00
|
|
|
end
|
|
|
|
end
|