Module: RedisLogger

Defined in:
lib/redis_middleware.rb

Overview

Note:

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.

Examples:

Enable Redis command logging

RedisLogger.logger = Logger.new(STDOUT)
RedisClient.register(RedisLogger)

See Also:

Class Attribute Summary collapse

Instance Method Summary collapse

Class Attribute Details

.loggerLogger?

Gets/sets the logger instance used by RedisLogger.

Returns:

  • (Logger, nil)

    The current logger instance or nil if not set.



27
28
29
# File 'lib/redis_middleware.rb', line 27

def logger
  @logger
end

Instance Method Details

#call(command, redis_config) ⇒ Object

Note:

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.

Parameters:

  • command (Array)

    The Redis command and its arguments.

  • redis_config (Hash)

    The configuration options for the Redis connection.

Returns:

  • (Object)

    The result of the Redis command execution.



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