Module: DatabaseLogger
- Defined in:
- lib/middleware/database_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.
DatabaseLogger is Valkey/RedisClient middleware.
This middleware addresses the need for detailed Database 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 Database commands can be invaluable.
Class Attribute Summary collapse
-
.commands ⇒ Array
readonly
Gets the captured commands for testing purposes.
-
.logger ⇒ Logger?
Gets/sets the logger instance used by DatabaseLogger.
Class Method Summary collapse
-
.capture_commands { ... } ⇒ Array
Captures commands in a block and returns them.
-
.clear_commands ⇒ Array
Clears the captured commands array.
Instance Method Summary collapse
-
#call(command, _config) ⇒ Object
Logs the Database command and its execution time.
Class Attribute Details
.commands ⇒ Array (readonly)
Gets the captured commands for testing purposes.
41 42 43 |
# File 'lib/middleware/database_middleware.rb', line 41 def commands @commands end |
.logger ⇒ Logger?
Gets/sets the logger instance used by DatabaseLogger.
37 38 39 |
# File 'lib/middleware/database_middleware.rb', line 37 def logger @logger end |
Class Method Details
.capture_commands { ... } ⇒ Array
Captures commands in a block and returns them. This is useful for testing to see what commands were executed.
62 63 64 65 66 |
# File 'lib/middleware/database_middleware.rb', line 62 def capture_commands clear_commands yield @commands.dup end |
.clear_commands ⇒ Array
Clears the captured commands array.
45 46 47 |
# File 'lib/middleware/database_middleware.rb', line 45 def clear_commands @commands = [] end |
Instance Method Details
#call(command, _config) ⇒ Object
Commands are always captured with minimal overhead for testing purposes. Logging only occurs when DatabaseLogger.logger is set.
Logs the Database command and its execution time.
This method is called for each Database command when the middleware is active. It always captures commands for testing and logs them if a logger is set.
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 |
# File 'lib/middleware/database_middleware.rb', line 81 def call(command, _config) start = Process.clock_gettime(Process::CLOCK_MONOTONIC, :microsecond) result = yield duration = Process.clock_gettime(Process::CLOCK_MONOTONIC, :microsecond) - start # Always capture commands for testing purposes DatabaseLogger.instance_variable_get(:@commands) << { command: command.dup, duration: duration, timestamp: Time.now, } # Log if logger is set DatabaseLogger.logger&.debug("Redis: #{command.inspect} (#{duration}µs)") result end |