Permalink
Browse files

Adding some rudimentary logging.

  • Loading branch information...
1 parent bc8a784 commit e3029076a00b120832e553563b93d92b8124100b @mje113 committed Dec 28, 2012
Showing with 83 additions and 16 deletions.
  1. +38 −16 lib/couchbase/model.rb
  2. +45 −0 lib/couchbase/model/logger.rb
@@ -21,6 +21,7 @@
require 'couchbase/model/version'
require 'couchbase/model/uuid'
require 'couchbase/model/configuration'
+require 'couchbase/model/logger'
unless Object.respond_to?(:singleton_class)
require 'couchbase/model/ext/singleton_class'
@@ -129,6 +130,15 @@ class Model
# @private Container for all view names of all subclasses
@@views = {}
+ # Establish logger for logging operations
+ def self.logger
+ @@logger ||= Couchbase::Logger.new
+ end
+
+ def self.log(operation, key, options = {}, &block)
+ logger.log(operation, key, options) { yield }
+ end
+
# Use custom connection options
#
# @since 0.0.1
@@ -400,10 +410,12 @@ def self.belongs_to(name, options = {})
# @example Find model using +id+
# post = Post.find('the-id')
def self.find(id)
- if id && (res = bucket.get(id, :quiet => false, :extended => true))
- obj, flags, cas = res
- obj = {:raw => obj} unless obj.is_a?(Hash)
- new({:id => id, :meta => {'flags' => flags, 'cas' => cas}}.merge(obj))
+ log('find', id) do
+ if id && (res = bucket.get(id, :quiet => false, :extended => true))
+ obj, flags, cas = res
+ obj = {:raw => obj} unless obj.is_a?(Hash)
+ new({:id => id, :meta => {'flags' => flags, 'cas' => cas}}.merge(obj))
+ end
end
end
@@ -418,10 +430,12 @@ def self.find(id)
# @example Find model using +id+
# post = Post.find_by_id('the-id')
def self.find_by_id(id)
- if id && (res = bucket.get(id, :quiet => true, :extended => true))
- obj, flags, cas = res
- obj = {:raw => obj} unless obj.is_a?(Hash)
- new({:id => id, :meta => {'flags' => flags, 'cas' => cas}}.merge(obj))
+ log('find', id) do
+ if id && (res = bucket.get(id, :quiet => true, :extended => true))
+ obj, flags, cas = res
+ obj = {:raw => obj} unless obj.is_a?(Hash)
+ new({:id => id, :meta => {'flags' => flags, 'cas' => cas}}.merge(obj))
+ end
end
end
@@ -495,15 +509,19 @@ def create(options = {})
if respond_to?(:valid?) && !valid?
return false
end
+
options = model.defaults.merge(options)
value = (options[:format] == :plain) ? @raw : attributes_with_values
- unless @meta
- @meta = {}
- if @meta.respond_to?(:with_indifferent_access)
- @meta = @meta.with_indifferent_access
+
+ model.log('create', @id, options) do
+ unless @meta
+ @meta = {}
+ if @meta.respond_to?(:with_indifferent_access)
+ @meta = @meta.with_indifferent_access
+ end
end
+ @meta['cas'] = model.bucket.add(@id, value, options)
end
- @meta['cas'] = model.bucket.add(@id, value, options)
self
end
@@ -543,8 +561,10 @@ def save(options = {})
return false
end
options = model.defaults.merge(options)
- value = (options[:format] == :plain) ? @raw : attributes_with_values
- @meta['cas'] = model.bucket.replace(@id, value, options)
+ model.log('save', @id, options) do
+ value = (options[:format] == :plain) ? @raw : attributes_with_values
+ @meta['cas'] = model.bucket.replace(@id, value, options)
+ end
self
end
@@ -587,7 +607,9 @@ def update(attrs, options = {})
# p.delete
def delete(options = {})
raise Couchbase::Error::MissingId, "missing id attribute" unless @id
- model.bucket.delete(@id, options)
+ model.log('delete', @id) do
+ model.bucket.delete(@id, options)
+ end
@id = nil
@meta = nil
self
@@ -0,0 +1,45 @@
+# Author:: Couchbase <info@couchbase.com>
+# Copyright:: 2012 Couchbase, Inc.
+# License:: Apache License, Version 2.0
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+#
+
+module Couchbase
+
+ class Logger
+
+ def initialize
+ @_logger = if defined?(::Rails)
+ Rails.logger
+ elsif defined?(::Logger)
+ ::Logger.new($stderr)
+ else
+ nil
+ end
+ end
+
+ def log(operation, key, options = {}, &block)
+ return yield unless @_logger
+
+ start = Time.now
+ result = yield
+ elapsed = Time.now - start
+ @_logger.info("Couchbase [#{operation}] (#{elapsed}ms) key=\"#{key}\" (#{options})")
+ result
+ rescue Exception => e
+ @_logger.error "Couchbase error [#{e.class.name}] [#{e.message}]"
+ raise e
+ end
+ end
+end

0 comments on commit e302907

Please sign in to comment.