Permalink
Browse files

Logging cleanup.

User should set the logger if they want to log DF operations.
DF will try to inherit AR:Base's logger if possible.
Should solve the issue of nil pointer errors if no logger is set.
  • Loading branch information...
1 parent 39dabfb commit 01a675c5e5dcf9991ce9c51468cd9e2ea8cdd9e9 @mperham mperham committed Nov 22, 2008
Showing with 16 additions and 27 deletions.
  1. +16 −27 lib/data_fabric.rb
View
@@ -37,27 +37,17 @@
# end
# end
module DataFabric
-
- def self.logger
- ActiveRecord::Base.logger
- end
+
+ # Set this logger to log DataFabric operations.
+ # The logger should quack like a standard Ruby Logger.
+ mattr_accessor :logger
def self.init
- logger.info "Loading data_fabric #{DataFabric::Version::STRING} with ActiveRecord #{ActiveRecord::VERSION::STRING}"
+ logger = ActiveRecord::Base.logger unless logger
+ log { "Loading data_fabric #{DataFabric::Version::STRING} with ActiveRecord #{ActiveRecord::VERSION::STRING}" }
ActiveRecord::Base.send(:include, self)
end
- mattr_writer :debugging
- @@debugging = false
-
- def self.debugging?
- if @@debugging.nil? && logger
- logger.debug?
- else
- !!@@debugging
- end
- end
-
def self.activate_shard(shards, &block)
ensure_setup
@@ -93,7 +83,7 @@ def self.active_shard(group)
raise ArgumentError, "No active shard for #{group}" unless shard
end
end
-
+
def self.included(model)
# Wire up ActiveRecord::Base
model.extend ClassMethods
@@ -102,15 +92,19 @@ def self.included(model)
def self.ensure_setup
Thread.current[:shards] = {} unless Thread.current[:shards]
end
-
+
+ def self.log(level=Logger::INFO, &block)
+ logger && logger.add(level, &block)
+ end
+
# Class methods injected into ActiveRecord::Base
module ClassMethods
def data_fabric(options)
proxy = DataFabric::ConnectionProxy.new(self, options)
ActiveRecord::Base.active_connections[name] = proxy
raise ArgumentError, "data_fabric does not support ActiveRecord's allow_concurrency = true" if allow_concurrency
- DataFabric.logger.info "Creating data_fabric proxy for class #{name}"
+ DataFabric.log { "Creating data_fabric proxy for class #{name}" }
end
alias :connection_topology :data_fabric # legacy
end
@@ -148,7 +142,7 @@ def transaction(start_db_transaction = true, &block)
end
def method_missing(method, *args, &block)
- logger.debug("Calling #{method} on #{connection}") if DataFabric.debugging?
+ DataFabric.log(Logger::DEBUG) { "Calling #{method} on #{connection}" }
connection.send(method, *args, &block)
end
@@ -199,9 +193,8 @@ def connection
if not connected?
config = ActiveRecord::Base.configurations[name]
raise ArgumentError, "Unknown database config: #{name}, have #{ActiveRecord::Base.configurations.inspect}" unless config
- logger.debug("Connecting to #{name}") if DataFabric.debugging?
+ DataFabric.log { "Connecting to #{name}" }
@model_class.establish_connection(config)
- @model_class.connection.verify!(0)
cached_connections[name] = @model_class.connection
@model_class.active_connections[@model_class.name] = self
end
@@ -210,7 +203,7 @@ def connection
end
def connected?
- not cached_connections[connection_name].nil?
+ cached_connections[connection_name]
end
def set_role(role)
@@ -220,10 +213,6 @@ def set_role(role)
def master
with_master { return connection }
end
-
- def logger
- DataFabric.logger
- end
end
module ActiveRecordConnectionMethods

0 comments on commit 01a675c

Please sign in to comment.