如何防止Phusion Passenger覆盖Rails日志?



我正在用nginx测试Passenger,在我的log/development.log文件中,看起来每个SQL请求都是由Passenger触发的:

Decidim::Organization Load (0.3ms)  SELECT  "decidim_organizations".* FROM "decidim_organizations" WHERE "decidim_organizations"."host" = $1 LIMIT $2  [["host", "decidim.domain.tld"], ["LIMIT", 1]]
↳ /usr/lib/ruby/vendor_ruby/phusion_passenger/rack/thread_handler_extension.rb:107

虽然它看起来像预期的行为,有没有办法从应用程序中添加到堆栈跟踪文件:行?

我想要这样的:

Decidim::Organization Load (0.3ms)  SELECT  "decidim_organizations".* FROM "decidim_organizations" WHERE "decidim_organizations"."host" = $1 LIMIT $2  [["host", "decidim.domain.tld"], ["LIMIT", 1]]
↳ /home/user/ror_app/app/models/something.rb:42

添加config.log_level = :debug&重新启动服务器没有帮助。

您可以将包含'phusion_passenger'(或更好的:'passenger')的行add_silencer(忽略)到BacktraceCleaner

# config/initializers/backtrace_silencer.rb
Rails.backtrace_cleaner.add_silencer { |line| /phusion_passenger/.match?(line) }

基本上当你沉默那些passenger行,然后它应该显示你的应用程序日志,例如,我假设日志行如下

Decidim::Organization Load (0.3ms)  SELECT ...
↳ /usr/lib/ruby/vendor_ruby/phusion_passenger/rack/thread_handler_extension.rb:107
↳ another phusion_passenger
↳ /home/user/ror_app/app/models/something.rb:42
...

如果您沉默phusion_passenger行,那么日志应该显示/home/user/ror_app/app/models/something

看一下Rails上的log_subscriber代码

如果中间还有许多其他行,为了确保日志将显示应用程序的第一行代码,您可以覆盖上面的log_subscriber代码以忽略行,直到到达应用程序中的代码行。

# your_app/lib/active_record/log_subscriber.rb 
module ActiveRecord
class LogSubscriber
def extract_query_source_location(locations)
# old code
# backtrace_cleaner.clean(caller.lazy).first

# new code: to make sure that you log the line of code in your app, 
# you could replace the regex `/app/` by `/app/model.../` or
# whatever you want to reach the code you want to show in logs
locations.lazy.filter { |line| /app/.match?(line) }.first # first(3)
end
end
end 
# config/initializers/ext_core.rb
require File.join(Rails.root, "lib", "active_record", "log_subcriber.rb") 

我想active-record-query-trace可以帮到你。

最新更新