Module: RedisLogger
- Defined in:
- lib/redis_middleware.rb
Overview
While there were concerns about the performance impact of logging in the redis-rb gem, this middleware is designed to be optional and can be easily enabled or disabled as needed. The performance impact is minimal when logging is disabled, and the benefits during development and debugging often outweigh the slight performance cost when enabled.
RedisLogger is RedisClient middleware.
This middleware addresses the need for detailed Redis command logging, which was removed from the redis-rb gem due to performance concerns. However, in many development and debugging scenarios, the ability to log Redis commands can be invaluable.
Class Attribute Summary collapse
-
.logger ⇒ Logger?
Gets/sets the logger instance used by RedisLogger.
Instance Method Summary collapse
-
#call(command, redis_config) ⇒ Object
Logs the Redis command and its execution time.
Class Attribute Details
.logger ⇒ Logger?
Gets/sets the logger instance used by RedisLogger.
27 28 29 |
# File 'lib/redis_middleware.rb', line 27 def logger @logger end |
Instance Method Details
#call(command, redis_config) ⇒ Object
The performance impact of this logging is negligible when no logger is set, as it quickly returns control to the Redis client. When a logger is set, the minimal overhead is often offset by the valuable insights gained during development and debugging.
Logs the Redis command and its execution time.
This method is called for each Redis command when the middleware is active. It logs the command and its execution time only if a logger is set.
44 45 46 47 48 49 50 51 52 |
# File 'lib/redis_middleware.rb', line 44 def call(command, redis_config) return yield unless RedisLogger.logger start = Process.clock_gettime(Process::CLOCK_MONOTONIC, :microsecond) result = yield duration = Process.clock_gettime(Process::CLOCK_MONOTONIC, :microsecond) - start RedisLogger.logger.debug("Redis: #{command.inspect} (#{duration}µs)") result end |