Skip to content

Commit

Permalink
Adding some rudimentary logging.
Browse files Browse the repository at this point in the history
  • Loading branch information
mje113 committed Dec 28, 2012
1 parent bc8a784 commit d7218e5
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 17 deletions.
56 changes: 39 additions & 17 deletions lib/couchbase/model.rb
Expand Up @@ -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'
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -400,11 +410,13 @@ 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))
end
#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

# Find the model using +id+ attribute
Expand All @@ -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

Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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
Expand Down
45 changes: 45 additions & 0 deletions lib/couchbase/model/logger.rb
@@ -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 d7218e5

Please sign in to comment.