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

Api Logger Bundle Laravel Package

adityasetiono/api-logger-bundle

Symfony2 bundle that logs REST API requests/responses (default /api/), tracks duration, and flags slow calls. Configurable enable/disable and slow-time threshold, with optional separate Monolog channels/files for API and slow logs.

View on GitHub
Deep Wiki
Context7

Getting Started

Minimal Steps

  1. Installation:

    composer require gamma/api-logger-bundle:dev-master
    

    Register the bundle in config/bundles.php (Laravel 5.5+) or AppKernel.php (Symfony):

    Gamma\ApiLoggerBundle\GammaApiLoggerBundle::class,
    
  2. Enable Logging: The bundle is enabled by default. Logs will be written to var/logs/api.log (Symfony) or Laravel’s default log channel (if configured).

  3. First Use Case: Log an API request in a controller:

    use Gamma\ApiLoggerBundle\Logger\ApiLogger;
    
    public function index(ApiLogger $apiLogger)
    {
        $apiLogger->logRequest(); // Logs incoming request details
        // Your logic here
        $apiLogger->logResponse($response); // Logs response details
    }
    

Implementation Patterns

Core Workflows

  1. Request/Response Logging:

    • Inject ApiLogger into controllers/services:
      public function store(Request $request, ApiLogger $apiLogger)
      {
          $apiLogger->logRequest($request); // Auto-captures method, URI, headers, body
          $response = $this->processRequest($request);
          $apiLogger->logResponse($response); // Captures status, headers, body
      }
      
    • Use middleware for global logging (Symfony):
      // src/Kernel.php
      protected function build(Request $request)
      {
          $this->apiLogger->logRequest($request);
          return parent::build($request);
      }
      
  2. Profiling Slow Requests:

    • Configure threshold in config/packages/gamma_api_logger.yaml (Symfony) or config/gamma_api_logger.php (Laravel):
      gamma_api_logger:
          slow_request_threshold: 500 # ms
      
    • Log slow requests automatically (e.g., to slow_api.log).
  3. Custom Log Channels:

    • Route logs to Monolog handlers (Symfony) or Laravel’s logging channels:
      # config/packages/monolog.yaml
      handlers:
          api_log:
              type: stream
              path: "%kernel.logs_dir%/api.log"
              level: debug
      
  4. Laravel Integration:

    • Use the Log facade with the api channel (if configured):
      Log::channel('api')->info('Custom API event', ['metadata' => $data]);
      

Gotchas and Tips

Pitfalls

  1. Performance Overhead:

    • Disable logging in production for high-traffic APIs:
      gamma_api_logger:
          enabled: false
      
    • Use logRequest()/logResponse() selectively in performance-critical paths.
  2. Sensitive Data Leakage:

    • Never log raw request/response bodies containing PII (passwords, tokens). Use:
      $apiLogger->logRequest($request, ['exclude_body' => true]);
      
  3. Deprecated Symfony2:

    • The bundle targets Symfony 2.x. For Laravel, wrap it in a Symfony bridge or use alternatives like spatie/laravel-logging.
  4. Log Rotation:

    • Configure log rotation manually (Symfony’s monolog or Laravel’s single channel) to avoid disk bloat.

Debugging

  • Missing Logs:

    • Verify the bundle is registered in bundles.php/AppKernel.php.
    • Check Monolog/Laravel log channel configuration for gamma_api_logger.
  • Slow Request Threshold:

    • Debug with:
      $apiLogger->setSlowRequestThreshold(1000); // Temporarily override
      

Extension Points

  1. Custom Log Format:

    • Extend the logger class to modify output:
      class CustomApiLogger extends ApiLogger
      {
          protected function formatRequest(Request $request)
          {
              return parent::formatRequest($request) . "\nCustom: " . $request->ip();
          }
      }
      
    • Bind the custom class in Symfony’s services or Laravel’s container.
  2. Database Logging:

    • Store logs in a DB by extending the logger to use a Doctrine connection:
      $apiLogger->logToDatabase($request, $response);
      
  3. Laravel-Specific:

    • Use Laravel’s terminating middleware to log responses:
      public function handle($request, Closure $next)
      {
          $response = $next($request);
          app('apiLogger')->logResponse($response);
          return $response;
      }
      
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.
emuniq/filament-browser-notifications
syriable/filament-translator
hungnm28/livewire-form
wenprise/eloquent
crudly/encrypted
fadion/bouncy
cuci/prototurk-sdk
gos/pubsub-router-bundle
cuci/prototurk-sdk-symfony
clementtalleu/easyadmin-markdown-bundle
codeflextech/permission-manager
karnoweb/livewire-datepicker
sayedenam/sayed-dashboard
milito/query-filter
apiboxsym/user-bundle
apiboxsym/health-check-bundle
jayeshmepani/jpl-moshier-ephemeris-php
elnasnato/laraliveui
labrodev/rest-sdk
sampaui/sampaui