PHP code example of restrole / laravel-logwithtraceid

1. Go to this page and download the library: Download restrole/laravel-logwithtraceid library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

restrole / laravel-logwithtraceid example snippets




return [
    'middleware_groups' => ['api'],
    'header_name' => 'X-Trace-ID',
    'log_context_key' => 'trace_id',
];

return [
    'middleware_group' => null, // Set to null to disable auto-registration
    // ... other config options
];

protected $middlewareGroups = [
    'web' => [
        // ... other middleware
        \Restrole\LaravelLogWithTraceId\Middleware\AddTraceIdMiddleware::class,
    ],

    'api' => [
        // ... other middleware
        \Restrole\LaravelLogWithTraceId\Middleware\AddTraceIdMiddleware::class,
    ],
];

$traceId = app('trace_id');

Log::info('User logged in');
// Output: [2025-01-01 12:00:00] local.INFO: User logged in {"trace_id":"abc123"}
BASH
php artisan vendor:publish --tag=laravel-logwithtraceid