Commit
As of this commit, dm-rails provides the same amount of log information as ActiveRecord, and it does so via the same interface. This means that logs show up just like ActiveRecord's in the console, and the log level for example can be controlled in the same way as for ActiveRecord (e.g. via config/environments) This also means that dm-rails now provides all the information rails_metrics also displays for ActiveRecord. Currently, rails_metrics only works when used from my fork at snusnu/rails_metrics (you have to use the 'datamapper-compatibility' branch). I'm pretty confident that these changes get merged upstream eventually.
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
module LogListener | ||
def log(message) | ||
ActiveSupport::Notifications.instrument('sql.data_mapper', | ||
:name => 'SQL', | ||
:sql => message.query, # TODO think about changing the key to :query | ||
:start => message.start, | ||
:duration => message.duration, | ||
:connection_id => self.object_id | ||
) | ||
super | ||
rescue Exception => e | ||
# TODO proper logging | ||
puts "[datamapper - ERROR] #{e.class.name}: #{e.message}: #{message.inspect}}" | ||
end | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
require 'active_support/core_ext/hash/except' | ||
|
||
require 'dm-rails/configuration' | ||
require 'dm-rails/railties/log_listener' | ||
require 'dm-rails/railties/benchmarking_mixin' | ||
|
||
module Rails | ||
|
@@ -14,14 +15,26 @@ def self.setup(environment) | |
initialize_foreign_keys | ||
end | ||
|
||
def self.setup_logger(logger) | ||
::DataMapper.logger = logger | ||
end | ||
|
||
def self.setup_with_instrumentation(name, options) | ||
puts "[datamapper] Setting up #{name.inspect} repository: '#{options['database']}' on #{options['adapter']}" | ||
adapter = ::DataMapper.setup(name, options) | ||
adapter.extend ::DataMapper::Adapters::Benchmarking | ||
setup_log_listener(options['adapter']) | ||
end | ||
|
||
def self.setup_logger(logger) | ||
::DataMapper.logger = logger | ||
end | ||
|
||
def self.setup_log_listener(adapter_name) | ||
if Object.const_defined?('DataObjects') | ||
DataObjects::Connection.send(:include, LogListener) | ||
# FIXME Setting DataMapper::Logger.new($stdout, :off) alone won't work because the #log | ||
# method is currently only available in DO and needs an explicit DO Logger instantiated. | ||
# We turn the logger :off because ActiveSupport::Notifications handles displaying log messages | ||
do_adapter = DataObjects.const_get(adapter_name.classify) | ||
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
ku1ik
|
||
do_adapter.logger = DataObjects::Logger.new($stdout, :off) | ||
end | ||
end | ||
|
||
def self.initialize_foreign_keys | ||
|
When adapter_name is "postgres", String#classify will return "Postgre" incorrectly.
Can adapter_name ever be "sqlite"? If so, this will cause an explosion because (I think) the namespace in DO is DataObjects::Sqlite3.
Also, I assume this code will be refactored to handle cases where multiple adapters are being used.. I presume this will explode badly in cases where you are mixing usage of MySQL and MongoDB.