Weave Code
Code Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Plugin Log Laravel Package

guzzle/plugin-log

Adds logging to Guzzle HTTP requests and responses via a plugin/listener. Capture and format request/response headers, bodies, and timing to help debug API calls, trace issues, and record activity to PSR-3 logs or custom handlers.

View on GitHub
Deep Wiki
Context7

Getting Started

Minimal Setup

  1. Installation Add the package via Composer:

    composer require guzzle/plugin-log
    

    (Note: This is a Guzzle 3 plugin—ensure compatibility with your Laravel version if using Guzzle 3.)

  2. Basic Usage Attach the logging plugin to a Guzzle client:

    use Guzzle\Plugin\Log\LogPlugin;
    use Guzzle\Plugin\Log\StreamHandler;
    
    $client = new \Guzzle\Http\Client();
    $client->getPlugin('log')->setHandler(new StreamHandler('php://output'));
    $client->getPlugin('log')->setEnabled(true);
    
  3. First Use Case Debug API requests/responses in real-time:

    $response = $client->get('https://api.example.com/data');
    // Logs request/response to stdout.
    

Implementation Patterns

Workflows

  1. Request/Response Logging Log all requests/responses to a file or stream:

    $client->getPlugin('log')->setHandler(new StreamHandler(fopen('debug.log', 'a')));
    
  2. Conditional Logging Enable logging only for specific environments:

    if (app()->environment('local')) {
        $client->getPlugin('log')->setEnabled(true);
    }
    
  3. Custom Formatting Extend the default logger to include Laravel-specific data (e.g., request IDs):

    $logger = new class implements \Psr\Log\LoggerInterface {
        public function log($level, $message, array $context = []) {
            // Prepend Laravel request ID.
            $context['request_id'] = request()->id();
            // Delegate to Guzzle's logger.
        }
    };
    
  4. Integration with Laravel Logging Route Guzzle logs to Laravel’s log channel:

    use Illuminate\Support\Facades\Log;
    
    $client->getPlugin('log')->setHandler(new class {
        public function write($message) {
            Log::debug($message);
        }
    });
    

Common Use Cases

  • Debugging API Calls: Log raw requests/responses during development.
  • Monitoring: Track API usage patterns in staging/production (with caution).
  • Audit Trails: Log sensitive operations (e.g., payment gateways) for compliance.

Gotchas and Tips

Pitfalls

  1. Guzzle 3 Compatibility

    • This package targets Guzzle 3, which is outdated. If using Laravel 5.5+, prefer:
      composer require guzzlehttp/guzzle
      
      And use GuzzleHttp\HandlerStack with middleware instead.
  2. Performance Overhead

    • Logging every request adds latency. Disable in production:
      $client->getPlugin('log')->setEnabled(env('APP_DEBUG'));
      
  3. Sensitive Data Leaks

    • Avoid logging requests with credentials or PII. Use a custom handler to redact data:
      $handler = new class {
          public function write($message) {
              $message = str_replace('password=.*&', 'password=***&', $message);
              Log::debug($message);
          }
      };
      
  4. Handler Quirks

    • StreamHandler may block if writing to a slow resource (e.g., network share).
    • Use Psr\Log\LoggerInterface handlers for better integration.

Debugging Tips

  • Verify Plugin Attachment:
    if (!$client->hasPlugin('log')) {
        throw new \RuntimeException('Log plugin not attached!');
    }
    
  • Check Handler Output: Redirect logs to stderr for Laravel Tinker:
    $client->getPlugin('log')->setHandler(new StreamHandler('php://stderr'));
    
  • Log Levels: The plugin uses Guzzle’s internal logging levels (not PSR-3). Map to Laravel levels manually if needed.

Extension Points

  1. Custom Log Formatters Override Guzzle\Plugin\Log\MessageFormatter to modify output:

    $formatter = new class extends \Guzzle\Plugin\Log\MessageFormatter {
        protected function formatRequest(\Guzzle\Http\Message\Request $request) {
            return "[CUSTOM] " . parent::formatRequest($request);
        }
    };
    $client->getPlugin('log')->setFormatter($formatter);
    
  2. Async Logging Use a queueable handler for high-throughput systems:

    $handler = new class {
        public function write($message) {
            LogRequest::dispatch($message);
        }
    };
    
Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
besmartand-pro/php-quality-config
sentix/ai-chatbot
terminal42/code-quality-tools
codifyo/ts-generator-bundle
testo/fiber
mintobit/jobqueue
a4sex/maintenance-bundle
a4sex/entity-date-update
a4sex/client-identifier
a4sex/base-utilites
a4sex/key-value-storage
a4sex/micro-status
chilldev/dependency-injection-extra
datinglibre/datinglibre-app-api
biberltd/corebundle
bricre/symfony-bundle-test
biberltd/logbundle
dominium/http-adapter-bundle
dominium/google-analytics
a4sex/auto-clean-entity