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 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
-
.logger ⇒ Logger?
Gets/sets the logger instance used by DatabaseLogger.
Instance Method Summary collapse
-
#call(command, _config) ⇒ Object
Logs the Database command and its execution time.
Class Attribute Details
.logger ⇒ Logger?
Gets/sets the logger instance used by DatabaseLogger.
29 30 31 |
# File 'lib/middleware/database_middleware.rb', line 29 def logger @logger end |
Instance Method Details
#call(command, _config) ⇒ Object
The performance impact of this logging is negligible when no logger is set, as it quickly returns control to the Database client. When a logger is set, the minimal overhead is often offset by the valuable insights gained during development and debugging.
Logs the Database command and its execution time.
This method is called for each Database command when the middleware is active. It logs the command and its execution time only if a logger is set.
46 47 48 49 50 51 52 53 54 |
# File 'lib/middleware/database_middleware.rb', line 46 def call(command, _config) return yield unless DatabaseLogger.logger start = Process.clock_gettime(Process::CLOCK_MONOTONIC, :microsecond) result = yield duration = Process.clock_gettime(Process::CLOCK_MONOTONIC, :microsecond) - start DatabaseLogger.logger.debug("Redis: #{command.inspect} (#{duration}µs)") result end |