Skip to content

Commit

Permalink
[logging] initial implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
0exp committed Sep 6, 2018
1 parent 1ab81a1 commit 75c62ad
Show file tree
Hide file tree
Showing 15 changed files with 144 additions and 16 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
# Changelog
All notable changes to this project will be documented in this file.

## [Unreleased]
- logging

## [0.2.0] - 2018-09-03
- fetching operation `AnyCache#fetch(key, force:, expires_in:, &block)`
- fetches data from the cache using the given key;
Expand Down
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ require 'any_cache'
- [AnyCache with ActiveSupport::Cache::MemoryStore](#anycache-with-activesupportcachememorystore)
- [Many cache storages](#many-cache-storages)
- [Custom cache clients](#custom-cache-clients)
- [Shared configs](#shared-config)
- [Operations](#operations)
- [Fetch](#fetch)
- [Read](#read)
Expand Down Expand Up @@ -279,6 +280,8 @@ end
AnyCache.build(MyCacheClient.new)
```

#### Shared configs

## Operations

`AnyCache` provides a following operation set:
Expand Down
37 changes: 22 additions & 15 deletions lib/any_cache.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# frozen_string_literal: true

require 'logger'
require 'qonfig'
require 'securerandom'
require 'concurrent/atomic/reentrant_read_write_lock'
Expand All @@ -11,17 +12,22 @@ class AnyCache
require_relative 'any_cache/error'
require_relative 'any_cache/drivers'
require_relative 'any_cache/adapters'

# @since 0.1.0
extend Forwardable
require_relative 'any_cache/logging'
require_relative 'any_cache/delegation'

# @since 0.2.0
include Qonfig::Configurable

# @since 0.3.0
include Delegation

# @since 0.2.0
# rubocop:disable Metrics/BlockLength
configuration do
setting :driver

setting :logger, Logging::Logger.new(STDOUT)

setting :redis do
setting :options, {}
end
Expand Down Expand Up @@ -53,6 +59,7 @@ class AnyCache
setting :options, {}
end
end
# rubocop:enable Metrics/BlockLength

class << self
# @param driver [Object]
Expand All @@ -66,18 +73,18 @@ def build(driver = Drivers.build(config))
end

# @api public
# @since 0.1.0
def_delegators :adapter,
:read,
:write,
:delete,
:increment,
:decrement,
:expire,
:persist,
:clear,
:exist?,
:fetch
# @since 0.3.0
def_loggable_delegators :adapter,
:read,
:write,
:delete,
:increment,
:decrement,
:expire,
:persist,
:clear,
:exist?,
:fetch

# @return [AnyCache::Adapters::Basic]
#
Expand Down
50 changes: 50 additions & 0 deletions lib/any_cache/delegation.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
# frozen_string_literal: true

# @api private
# @since 0.3.0
module AnyCache::Delegation
class << self
# @param base_klass [Class]
# @return [void]
#
# @api private
# @since 0.2.0
def included(base_klass)
base_klass.extend(ClassMethods)
end
end

# @api private
# @since 0.3.0
module ClassMethods
# @param receiver [Symbol, String]
# @param delegators [Array<Symbol, String>]
# @return [void]
#
# @api private
# @since 0.3.0
def def_loggable_delegators(receiver, *delegators)
delegators.each { |delegat| def_loggable_delegator(receiver, delegat) }
end

# @param receiver [Symbol, String]
# @param delegator [Symbol, String]
# @return [void]
#
# @api private
# @since 0.3.0
def def_loggable_delegator(receiver, delegat)
define_method(delegat) do |*args, **opts, &block|
send(receiver).send(delegat, *args, **opts, &block).tap do
shared_config[:logger].tap do |logger|
AnyCache::Logging::Activity.log(
self, logger, activity: delegat, message: <<~MESSAGE
performed <##{delegat}> operation with params: #{args} and options: #{opts}.
MESSAGE
) if logger
end
end
end
end
end
end
40 changes: 40 additions & 0 deletions lib/any_cache/logging.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
# frozen_string_literal: true

module AnyCache::Logging
# @api private
# @since 0.3.0
class Logger < ::Logger
# @api private
# @since 0.3.0
def initialize(*, **)
super
self.level = ::Logger::INFO
end
end

# @api private
# @since 0.3.0
module Activity
# @return [String]
#
# @api private
# @since 0.3.0
ANONYMOUS_CACHER_INSTANCE_NAME = '<anonymous_cache>'

class << self
# @param cacher [AnyCache]
# @param logger [::Logger]
# @option activity [String, NilClass]
# @param message [String, NillClass]
# @return [void]
#
# @api private
# @since 0.3.0
def log(cacher, logger, activity: nil, message: nil)
cacher = cacher.class.name || ANONYMOUS_CACHER_INSTANCE_NAME
progname = "[AnyCache<#{cacher}>/Activity<#{activity}>]"
logger.add(logger.level, message, progname)
end
end
end
end
5 changes: 5 additions & 0 deletions spec/features/logging_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# frozen_string_literal: true

describe 'Logging' do
# TODO
end
1 change: 1 addition & 0 deletions spec/support/spec_support.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# frozen_string_literal: true

module SpecSupport
require_relative 'spec_support/null_logger'
require_relative 'spec_support/cache'
require_relative 'spec_support/testing'
require_relative 'spec_support/helpers'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ module SpecSupport::Cache::ActiveSupportFileStore
class CacheStore < AnyCache
configure do |conf|
conf.driver = :as_file_store

conf.logger = SpecSupport::NullLogger
conf.as_file_store.cache_path = File.expand_path(
File.join('..', '..', 'artifacts', SecureRandom.hex),
__dir__
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ module SpecSupport::Cache::ActiveSupportMemCacheStore
class CacheStore < AnyCache
configure do |conf|
conf.driver = :as_mem_cache_store
conf.logger = SpecSupport::NullLogger
conf.as_mem_cache_store.servers = '127.0.0.1:11211'
conf.as_mem_cache_store.options = { namespace: 'any_cache' }
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ module SpecSupport::Cache::ActiveSupportMemoryStore
class CacheStore < AnyCache
configure do |conf|
conf.driver = :as_memory_store
conf.logger = SpecSupport::NullLogger
end
end

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ module SpecSupport::Cache::ActiveSupportRedisCacheStore
class CacheStore < AnyCache
configure do |conf|
conf.driver = :as_redis_cache_store
conf.logger = SpecSupport::NullLogger
end
end

Expand Down
1 change: 1 addition & 0 deletions spec/support/spec_support/cache/dalli.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ module SpecSupport::Cache::Dalli
class CacheStore < AnyCache
configure do |conf|
conf.driver = :dalli
conf.logger = SpecSupport::NullLogger
conf.dalli.servers = '127.0.0.1:11211'
conf.dalli.options = { namespace: 'any_cache' }
end
Expand Down
1 change: 1 addition & 0 deletions spec/support/spec_support/cache/redis.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ module SpecSupport::Cache::Redis
class CacheStore < AnyCache
configure do |conf|
conf.driver = :redis
xconf.logger = SpecSupport::NullLogger
conf.redis.options = { host: '127.0.0.1', port: 6379 }
end
end
Expand Down
1 change: 1 addition & 0 deletions spec/support/spec_support/cache/redis_store.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ module SpecSupport::Cache::RedisStore
class CacheStore < AnyCache
configure do |conf|
conf.driver = :redis_store
conf.logger = SpecSupport::NullLogger
conf.redis_store.options = { host: '127.0.0.1', port: 6379 }
end
end
Expand Down
13 changes: 13 additions & 0 deletions spec/support/spec_support/null_logger.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# frozen_string_literal: true

module SpecSupport::NullLogger
class << self
def method_missing(method_name, *arguments, &block)
nil
end

def respond_to_missing?(method_name, *arguments, &block)
true
end
end
end

0 comments on commit 75c62ad

Please sign in to comment.