Class: Aspire::Caching::CacheLogger
- Inherits:
-
Object
- Object
- Aspire::Caching::CacheLogger
- Defined in:
- lib/aspire/caching/cache_logger.rb
Overview
A wrapper class for Logger adding utility methods
Instance Attribute Summary collapse
-
#logger ⇒ Logger
The logger.
Instance Method Summary collapse
-
#initialize(logger = nil) ⇒ CacheLogger
constructor
Initialises a new CacheLogger instance.
-
#log_exception(message, exception = nil, level: nil) ⇒ Object
Logs and raises an exception.
-
#log_return(result, *args, **kwargs) ⇒ Object
Logs an event and returns its first argument - allows for compact code such as ‘return log_return(result, msg,…)’.
-
#method_missing(method, *args, &block) ⇒ Object
Delegates missing methods to the logger.
-
#respond_to_missing?(method) ⇒ Boolean
Delegates missing method respond_to? to the wrapped logger.
Constructor Details
#initialize(logger = nil) ⇒ CacheLogger
Initialises a new CacheLogger instance
38 39 40 |
# File 'lib/aspire/caching/cache_logger.rb', line 38 def initialize(logger = nil) self.logger = logger end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method, *args, &block) ⇒ Object
Delegates missing methods to the logger
19 20 21 22 23 24 25 26 |
# File 'lib/aspire/caching/cache_logger.rb', line 19 def method_missing(method, *args, &block) # Do not fail if logger is undefined return nil unless logger # Fail if logger does not respond to this method super unless logger.respond_to?(method) # Delegate to the logger method logger.public_send(method, *args, &block) end |
Instance Attribute Details
#logger ⇒ Logger
Returns the logger.
12 13 14 |
# File 'lib/aspire/caching/cache_logger.rb', line 12 def logger @logger end |
Instance Method Details
#log_exception(message, exception = nil, level: nil) ⇒ Object
Logs and raises an exception
47 48 49 50 |
# File 'lib/aspire/caching/cache_logger.rb', line 47 def log_exception(, exception = nil, level: nil) log(level || Logger::ERROR, ) raise exception || Aspire::Exceptions::Error, end |
#log_return(result, *args, **kwargs) ⇒ Object
Logs an event and returns its first argument
-
allows for compact code such as ‘return log_return(result, msg,…)’
57 58 59 60 |
# File 'lib/aspire/caching/cache_logger.rb', line 57 def log_return(result, *args, **kwargs) log(*args, **kwargs) result end |
#respond_to_missing?(method) ⇒ Boolean
Delegates missing method respond_to? to the wrapped logger
31 32 33 34 |
# File 'lib/aspire/caching/cache_logger.rb', line 31 def respond_to_missing?(method) # If logger is undefined, all missing methods are accepted logger ? logger.respond_to?(method) : true end |