### Install Faraday::DetailedLogger Gem Source: https://github.com/envylabs/faraday-detailed_logger/blob/main/README.md Instructions for adding the faraday-detailed_logger gem to your project's Gemfile and installing it via Bundler or directly using the gem command. ```ruby gem 'faraday-detailed_logger' ``` ```bash $ bundle $ gem install faraday-detailed_logger ``` -------------------------------- ### Example cURL Command and Expected Log Output Source: https://github.com/envylabs/faraday-detailed_logger/blob/main/README.md Provides a sample cURL command with verbose output and the corresponding log output generated by Faraday::DetailedLogger at DEBUG level, demonstrating how request and response details are formatted. ```bash $ curl -v -d "requestbody=content" http://sushi.com/temaki > GET /temaki HTTP/1.1 > User-Agent: Faraday::DetailedLogger > Host: sushi.com > Content-Type: application/x-www-form-urlencoded > > requestbody=content > < HTTP/1.1 200 OK < Content-Type: application/json < < {"order_id":"1"} ``` ```plain POST http://sushi.com/nigirizushi "User-Agent: Faraday::DetailedLogger\nContent-Type: application/x-www-form-urlencoded\n\nrequestbody=content" HTTP 200 "Content-Type: application/json\n\n{\"order_id\":\"1\"}" ``` -------------------------------- ### Register Faraday DetailedLogger Middleware Source: https://context7.com/envylabs/faraday-detailed_logger/llms.txt This snippet demonstrates the basic registration of the DetailedLogger middleware into a Faraday connection. It uses default STDOUT logging and standard Faraday setup. ```ruby require 'faraday' require 'faraday/detailed_logger' connection = Faraday.new(url: 'http://api.example.com') do |faraday| faraday.request :url_encoded faraday.response :detailed_logger faraday.adapter Faraday.default_adapter end response = connection.get('/users/123') # INFO level output: GET http://api.example.com/users/123 # INFO level output: HTTP 200 ``` -------------------------------- ### Use Faraday Detailed Logger in Test Environments Source: https://context7.com/envylabs/faraday-detailed_logger/llms.txt This example shows how to integrate the Faraday Detailed Logger middleware within a test environment using a test adapter. It captures logs in memory using `StringIO` for later assertions. The snippet requires 'faraday', 'faraday/detailed_logger', 'logger', and 'stringio'. It demonstrates logging a POST request and then verifying the log output for the request and response details. ```ruby require 'faraday' require 'faraday/detailed_logger' require 'logger' require 'stringio' # Capture logs in memory for test assertions log_output = StringIO.new test_logger = Logger.new(log_output) test_logger.level = Logger::DEBUG connection = Faraday.new(url: 'http://test.example.com') do |faraday| faraday.request :url_encoded faraday.response :detailed_logger, test_logger faraday.adapter :test do |stub| stub.post('/api/users') do |env| [201, { 'Content-Type' => 'application/json' }, '{"id":1,"name":"Alice"}'] end end end connection.post('/api/users', { name: 'Alice', email: 'alice@example.com' }) # Verify logging output log_output.rewind logs = log_output.read raise 'Missing request log' unless logs.include?('POST http://test.example.com/api/users') raise 'Missing response log' unless logs.include?('HTTP 201') ``` -------------------------------- ### Configure Custom Logger with Faraday DetailedLogger Source: https://context7.com/envylabs/faraday-detailed_logger/llms.txt This example shows how to configure the DetailedLogger middleware to use a custom Logger instance, directing output to a specific file ('logs/api_requests.log') and setting the log level to INFO. ```ruby require 'faraday' require 'faraday/detailed_logger' require 'logger' file_logger = Logger.new('logs/api_requests.log') file_logger.level = Logger::INFO connection = Faraday.new(url: 'https://api.github.com') do |faraday| faraday.request :url_encoded faraday.response :detailed_logger, file_logger faraday.adapter Faraday.default_adapter end response = connection.get('/users/octocat') # Logs written to logs/api_requests.log: # I, [timestamp] INFO -- : GET https://api.github.com/users/octocat # I, [timestamp] INFO -- : HTTP 200 ``` -------------------------------- ### Custom Logger and Log Level Configuration Source: https://github.com/envylabs/faraday-detailed_logger/blob/main/README.md Shows how to configure Faraday::DetailedLogger to use a custom Logger object and set its log level. This example uses a file-based logger and sets the level to INFO. ```ruby require 'faraday' require 'faraday/detailed_logger' require 'logger' my_logger = Logger.new('logfile.log') my_logger.level = Logger::INFO connection = Faraday.new(url: 'http://sushi.com') do |faraday| faraday.request :url_encoded faraday.response :detailed_logger, my_logger # <-- sets a custom logger. faraday.adapter Faraday.default_adapter end ``` -------------------------------- ### Enable Debug Level Logging with Bodies in Faraday DetailedLogger Source: https://context7.com/envylabs/faraday-detailed_logger/llms.txt This example shows how to configure the DetailedLogger middleware for DEBUG level logging. This captures full request and response headers and bodies, which is useful for in-depth debugging but should be used cautiously due to sensitive data exposure. ```ruby require 'faraday' require 'faraday/detailed_logger' require 'logger' debug_logger = Logger.new($stdout) debug_logger.level = Logger::DEBUG connection = Faraday.new(url: 'https://api.example.com') do |faraday| faraday.request :url_encoded faraday.response :detailed_logger, debug_logger faraday.adapter Faraday.default_adapter end connection.post('/api/orders', { product_id: 42, quantity: 3 }) # INFO output: POST https://api.example.com/api/orders # DEBUG output: "Content-Type: application/x-www-form-urlencoded\nUser-Agent: Faraday v2.0.0\n\nproduct_id=42&quantity=3" # INFO output: HTTP 201 # DEBUG output: "Content-Type: application/json\nContent-Length: 156\n\n{\"order_id\":\"xyz789\",\"status\":\"created\",\"total\":99.99}" ``` -------------------------------- ### Map HTTP Status Codes to Log Levels with Faraday Detailed Logger Source: https://context7.com/envylabs/faraday-detailed_logger/llms.txt This example illustrates how the Faraday Detailed Logger middleware automatically adjusts log levels based on HTTP response status codes. It uses a test adapter to simulate different status codes (200, 301, 404, 500) and shows the corresponding log level output (INFO for success/redirect, WARN for client/server errors). The snippet requires 'faraday', 'faraday/detailed_logger', and 'logger'. ```ruby require 'faraday' require 'faraday/detailed_logger' require 'logger' logger = Logger.new($stdout) connection = Faraday.new(url: 'https://api.example.com') do |faraday| faraday.response :detailed_logger, logger faraday.adapter :test do |stub| stub.get('/success') { [200, {}, 'OK'] } stub.get('/redirect') { [301, { 'Location' => '/new-path' }, ''] } stub.get('/not-found') { [404, {}, 'Not Found'] } stub.get('/server-error') { [500, {}, 'Internal Server Error'] } end end connection.get('/success') # INFO: HTTP 200 connection.get('/redirect') # INFO: HTTP 301 connection.get('/not-found') # WARN: HTTP 404 connection.get('/server-error') # WARN: HTTP 500 ``` -------------------------------- ### Configure Multiple Faraday Connections with Different Loggers Source: https://context7.com/envylabs/faraday-detailed_logger/llms.txt This code demonstrates how to configure multiple Faraday connections, each with its own logger and log level, for different services or environments. It sets up a production client with INFO level logging to a file and a development client with DEBUG level logging to standard output. The snippet requires 'faraday', 'faraday/detailed_logger', and 'logger'. It also shows how to prefix log messages with a service name. ```ruby require 'faraday' require 'faraday/detailed_logger' require 'logger' # Production API with INFO level logging production_logger = Logger.new('logs/production_api.log') production_logger.level = Logger::INFO production_client = Faraday.new(url: 'https://api.production.com') do |faraday| faraday.response :detailed_logger, production_logger, 'Production' faraday.adapter Faraday.default_adapter end # Development API with DEBUG level logging dev_logger = Logger.new($stdout) dev_logger.level = Logger::DEBUG dev_client = Faraday.new(url: 'https://api.staging.com') do |faraday| faraday.response :detailed_logger, dev_logger, 'Staging' faraday.adapter Faraday.default_adapter end production_client.get('/health') # Logs only: [Production] GET https://api.production.com/health dev_client.get('/health') # Logs: [Staging] GET https://api.staging.com/health # Plus DEBUG details with headers and body ``` -------------------------------- ### Using Rails Logger with Tagging Source: https://github.com/envylabs/faraday-detailed_logger/blob/main/README.md Illustrates how to integrate Faraday::DetailedLogger with the Rails logger and how to add a custom tag to the log output for easier identification. ```ruby faraday.response :detailed_logger, Rails.logger faraday.response :detailed_logger, Rails.logger, 'Sushi Request' ``` -------------------------------- ### Request Logging Format Source: https://github.com/envylabs/faraday-detailed_logger/blob/main/README.md Details the logging format for the request portion of an HTTP interaction. The POST/method line is logged at INFO level, while headers and body are logged at DEBUG level. ```plain POST http://sushi.com/temaki "User-Agent: Faraday v0.9.0\nAccept: application/json\nContent-Type: application/json\n\n{\"name\":\"Polar Bear\",\"ingredients\":[\"Tuna\",\"Salmon\",\"Cream Cheese\",\"Tempura Flakes\"],\"garnish\":\"Eel Sauce\"}" ``` -------------------------------- ### Response Logging Format Source: https://github.com/envylabs/faraday-detailed_logger/blob/main/README.md Explains the logging format for the response portion of an HTTP interaction. The HTTP status line is logged at INFO level, and headers/body are logged at DEBUG level. ```plain HTTP 202 "server: nginx\ndate: Tue, 01 Jul 2014 21:56:52 GMT\ncontent-type: application/json\ncontent-length: 17\nconnection: close\nstatus: 202 Accepted\n\n{\"order_id\":\"1\"}" ``` -------------------------------- ### Basic Usage: Add Detailed Logger to Faraday Connection Source: https://github.com/envylabs/faraday-detailed_logger/blob/main/README.md Demonstrates how to include the Faraday::DetailedLogger middleware in a Faraday connection. By default, it logs to STDOUT. ```ruby require 'faraday' require 'faraday/detailed_logger' connection = Faraday.new(url: 'http://sushi.com') do |faraday| faraday.request :url_encoded faraday.response :detailed_logger # <-- Inserts the logger into the connection. faraday.adapter Faraday.default_adapter end ``` -------------------------------- ### Log Exceptions During HTTP Requests with Faraday Detailed Logger Source: https://context7.com/envylabs/faraday-detailed_logger/llms.txt This snippet demonstrates how the Faraday Detailed Logger middleware automatically captures and logs exceptions that occur during HTTP requests. It also re-raises these exceptions for further application handling. It requires the 'faraday' and 'faraday/detailed_logger' gems, along with the standard 'logger' library. The output logs the exception type and message, including file and line number information. ```ruby require 'faraday' require 'faraday/detailed_logger' require 'logger' logger = Logger.new($stdout) connection = Faraday.new(url: 'https://api.example.com', request: { timeout: 5 }) do |faraday| faraday.request :url_encoded faraday.response :detailed_logger, logger faraday.adapter Faraday.default_adapter end begin connection.get('/slow-endpoint') rescue Faraday::TimeoutError => e # ERROR log output: Faraday::TimeoutError - execution expired (/path/to/faraday.rb:123) puts "Request timed out: #{e.message}" end ``` -------------------------------- ### Integrate Faraday DetailedLogger with Rails Logger Source: https://context7.com/envylabs/faraday-detailed_logger/llms.txt This code demonstrates how to integrate the DetailedLogger middleware with the Rails application logger, ensuring HTTP request logs are consolidated with other application logs in Rails. ```ruby # config/initializers/faraday.rb require 'faraday/detailed_logger' class ApiClient def self.connection @connection ||= Faraday.new(url: ENV['API_BASE_URL']) do |faraday| faraday.request :url_encoded faraday.request :json faraday.response :detailed_logger, Rails.logger faraday.response :json faraday.adapter Faraday.default_adapter end end end # Usage in controllers or services ApiClient.connection.get('/api/v1/products') # Logs appear in Rails log with appropriate log level formatting ``` -------------------------------- ### Add Tags to Faraday DetailedLogger Output Source: https://context7.com/envylabs/faraday-detailed_logger/llms.txt This snippet illustrates how to add custom tags (e.g., 'StripeAPI', 'PaymentService') to the log output when registering the DetailedLogger middleware. This is useful for differentiating logs in complex applications. ```ruby require 'faraday' require 'faraday/detailed_logger' connection = Faraday.new(url: 'https://api.stripe.com') do |faraday| faraday.request :url_encoded faraday.response :detailed_logger, Logger.new($stdout), 'StripeAPI', 'PaymentService' faraday.adapter Faraday.default_adapter end connection.post('/v1/charges', { amount: 2000, currency: 'usd' }) # INFO level output: [StripeAPI] [PaymentService] POST https://api.stripe.com/v1/charges # INFO level output: [StripeAPI] [PaymentService] HTTP 200 ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.