PHP code example of forecho / laravel-trace-log

1. Go to this page and download the library: Download forecho/laravel-trace-log 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/ */

    

forecho / laravel-trace-log example snippets


$app->register(Forecho\LaravelTraceLog\TraceLogServiceProvider::class);

$app->configure('tracelog');

'additional_fields' => [
    'user_id' => [App\Models\User::class, 'getId'],
    'tag' => 'test', // value can use closure、string、array
  ],

namespace App\Models;

class User 
{
    public static function getId(): ?int
    {
        return data_get(Auth::user(), 'id');
    }
}


use Forecho\LaravelTraceLog\Middleware\TraceLogMiddleware;

protected $middlewareGroups = [
    // ...

    'api' => [
        // ...        
        'request.tracelog'
    ],
];


protected $routeMiddleware = [
    // ...    
    'request.tracelog' => TraceLogMiddleware::class
];


use Forecho\LaravelTraceLog\TraceLog;

TraceLog::warning('This is a warning message.', ['foo' => 'bar']);
TraceLog::error('This is an error message.', ['foo' => 'bar']);
TraceLog::info('This is an info message.', ['foo' => 'bar']);
TraceLog::debug('This is a debug message.', ['foo' => 'bar']);

use Forecho\LaravelTraceLog\TraceLog;

TraceLog::getTraceId();

use Forecho\LaravelTraceLog\TraceLog;

$key = config('tracelog.trace_id_header_key');
$headers = [
  $key => TraceLog::getTraceId(),
]
shell
php artisan vendor:publish --provider="Forecho\LaravelTraceLog\TraceLogServiceProvider"
shell
cp vendor/forecho/laravel-trace-log/config/tracelog.php config/