3

我正在将旧的 Rails 2.3 应用程序(实际上它甚至更旧......)升级到 Rails 3.2,并且我的 SQL 语句没有记录到 development.log。我电脑上的一个 Rails 3.1 应用程序可以很好地记录它的 SQL 语句。

我正在使用 pow 在本地为应用程序提供服务。如果我运行 rails server,会输出 SQL,但那是因为 SQL 语句正在输出到 STDERR。我检查了 ActiveRecord 正在使用什么记录器(logger.info ActiveRecord::Base.logger.inspect),我得到:

#<Logger:0x007fdb9f788688 @progname=nil, @level=0, @default_formatter=#<Logger::Formatter:0x007fdb9f788638 @datetime_format=nil>, @formatter=#<Logger::SimpleFormatter:0x007fdb9f788548 @datetime_format=nil>, @logdev=#<Logger::LogDevice:0x007fdb9f7885e8 @shift_size=nil, @shift_age=nil, @filename=nil, @dev=#<IO:<STDERR>>, @mutex=#<Logger::LogDevice::LogDeviceMutex:0x007fdb9f7885c0 @mon_owner=nil, @mon_count=0, @mon_mutex=#<Mutex:0x007fdb9f788570>>>>

如您所见,LogDevice 的@dev 指向STDERR。在我的 Rails 3.1 应用程序中,相同的查询得到:

#<ActiveSupport::BufferedLogger:0x106f28c10 @guard=#<Mutex:0x106ec21b8>, @level=0, @auto_flushing=1, @buffer={}, @log=#<File:/web/unique/dynamic_coupons/log/development.log>>

在我的配置中没有什么有趣的东西——都是非常标准的——但我把它们包括在下面是为了后代。

某处某处关闭了我的 SQL 注销,我无法理解为什么。我希望我可以在某处添加一个配置行以强制 ActiveRecord 日志记录再次转到 development.log。

谢谢您的帮助!

发展.rb

MyApp::Application.configure do
  # Settings specified here will take precedence over those in config/application.rb

  # In the development environment your application's code is reloaded on
  # every request. This slows down response time but is perfect for development
  # since you don't have to restart the web server when you make code changes.
  config.cache_classes = false

  # Log error messages when you accidentally call methods on nil.
  config.whiny_nils = true

  # Show full error reports and disable caching
  config.consider_all_requests_local       = true
  config.action_controller.perform_caching = false

  # Don't care if the mailer can't send
  config.action_mailer.raise_delivery_errors = false

  # Print deprecation notices to the Rails logger
  config.active_support.deprecation = :log

  # Only use best-standards-support built into browsers
  config.action_dispatch.best_standards_support = :builtin

  # Raise exception on mass assignment protection for ActiveRecord models
  config.active_record.mass_assignment_sanitizer = :strict

  # Log the query plan for queries taking more than this (works
  # with SQLite, MySQL, and PostgreSQL)
  config.active_record.auto_explain_threshold_in_seconds = 0.5

  # Do not compress assets
  config.assets.compress = false

  # Expands the lines which load the assets
  config.assets.debug = true

  # Mailer configuration
  config.action_mailer.delivery_method = :letter_opener
  config.action_mailer.default_url_options = { :host => 'lta.dev' }
end

应用程序.rb

module MyApp
  class Application < Rails::Application
    # Settings in config/environments/* take precedence over those specified here.
    # Application configuration should go into files in config/initializers
    # -- all .rb files in that directory are automatically loaded.

    # Custom directories with classes and modules you want to be autoloadable.
    # config.autoload_paths += %W(#{config.root}/extras)

    # Only load the plugins named here, in the order given (default is alphabetical).
    # :all can be used as a placeholder for all plugins not explicitly named.
    # config.plugins = [ :exception_notification, :ssl_requirement, :all ]

    # Activate observers that should always be running.
    # config.active_record.observers = :cacher, :garbage_collector, :forum_observer

    # Set Time.zone default to the specified zone and make Active Record auto-convert to this zone.
    # Run "rake -D time" for a list of tasks for finding time zone names. Default is UTC.
    # config.time_zone = 'Central Time (US & Canada)'

    # The default locale is :en and all translations from config/locales/*.rb,yml are auto loaded.
    # config.i18n.load_path += Dir[Rails.root.join('my', 'locales', '*.{rb,yml}').to_s]
    # config.i18n.default_locale = :de

    # Configure the default encoding used in templates for Ruby 1.9.
    config.encoding = "utf-8"

    # Configure sensitive parameters which will be filtered from the log file.
    config.filter_parameters += [:password]

    # Use SQL instead of Active Record's schema dumper when creating the database.
    # This is necessary if your schema can't be completely dumped by the schema dumper,
    # like if you have constraints or database-specific column types
    # config.active_record.schema_format = :sql

    # Enforce whitelist mode for mass assignment.
    # This will create an empty whitelist of attributes available for mass-assignment for all models
    # in your app. As such, your models will need to explicitly whitelist or blacklist accessible
    # parameters by using an attr_accessible or attr_protected declaration.
    # config.active_record.whitelist_attributes = true

    # Enable the asset pipeline
    config.assets.enabled = true

    # Version of your assets, change this if you want to expire all your assets
    config.assets.version = '1.0'

    config.generators.stylesheet_engine = :sass
  end
end
4

2 回答 2

2

感谢所有回应和思考这个问题的人。事实证明,从 2.x 时代起就有一个库劫持了记录器(足够无辜)。我不知道这个库,因为另一个程序员添加了它。我今天在搜索“Logger.new”时碰巧发现了它,我看到了发生了什么。

于 2012-08-07T15:11:29.620 回答
0

如果启动控制台(开发环境),Rails.configuration.log_level 的输出是什么?

现在将 ActiveRecord::Base.logger.level 与 Logger::DEBUG (0)、Logger::INFO (1)、Logger::WARN (2) 和 Logger::ERROR (3) 进行比较。

如果 ActiveRecord::Base.logger.level 低于 Rails.configuration.log_level,则 development.log 将不包含来自 ActiveRecord 的任何 SQL 日志记录。

于 2012-04-16T16:37:47.847 回答