Class: AiClient::LoggingMiddleware

Inherits:
Object
  • Object
show all
Defined in:
lib/ai_client/logger_middleware.rb

Overview

logger = Logger.new(STDOUT)

AiClient.use(

AiClient::LoggingMiddleware.new(logger)

)

Or, if you want to use the same logger as the AiClient: AiClient.use(

AiClient::LoggingMiddleware.new(
  AiClient.configuration.logger
)

)

Instance Method Summary collapse

Constructor Details

#initialize(logger) ⇒ LoggingMiddleware

Initializes the LoggingMiddleware with a logger.

Parameters:

  • logger (Logger)

    The logger used for logging middleware actions.



24
25
26
# File 'lib/ai_client/logger_middleware.rb', line 24

def initialize(logger)
  @logger = logger
end

Instance Method Details

#call(client, next_middleware, *args) ⇒ Object

Calls the next middleware in the stack while logging the start and finish times.

Parameters:

  • client (Object)

    The client instance.

  • next_middleware (Proc)

    The next middleware to call.

  • args (Array)

    The arguments passed to the middleware call, with the first being the method name.

Returns:

  • (Object)

    The result of the next middleware call.



36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/ai_client/logger_middleware.rb', line 36

def call(client, next_middleware, *args)
  method_name = args.first.is_a?(Symbol) ? args.first : 'unknown method'
  @logger.info("Starting #{method_name} call")
  start_time = Time.now

  result = next_middleware.call(*args)

  end_time = Time.now
  duration = end_time - start_time
  @logger.info("Finished #{method_name} call (took #{duration.round(3)} seconds)")

  result
end