Laravel Log::withContext() doesn't work
iamwildtuna opened this issue · 4 comments
iamwildtuna commented
Hello! Thanks for you work!
In Laravel 8+ Log::withContext() doesn't work.
hedii commented
Hi, please share your configuration and code that shows something that is not working
iamwildtuna commented
Middleware:
<?php
namespace App\Http\Middleware;
use Closure;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Str;
class AssignRequestId
{
/**
* Обработчик входящего запроса
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @return mixed
*/
public function handle(Request $request, Closure $next)
{
$requestId = (string) Str::uuid();
Log::withContext([
'request-id' => $requestId
]);
$request->headers->set('Request-Id', $requestId);
return $next($request)->header('Request-Id', $requestId);
}
}
Used code:
<?php
namespace App\Http\Controllers;
use App\Http\ApiResponseInjectController;
use Firebase\JWT\JWT;
use Firebase\JWT\Key;
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
use Illuminate\Foundation\Bus\DispatchesJobs;
use Illuminate\Foundation\Validation\ValidatesRequests;
use Illuminate\Http\Request;
use Illuminate\Routing\Controller as BaseController;
use Illuminate\Support\Facades\Log;
class TokenController extends BaseController
{
use AuthorizesRequests, DispatchesJobs, ValidatesRequests, ApiResponseInjectController;
/**
* Создание JWT
*
* @return \Illuminate\Http\JsonResponse
*/
public function generate(Request $request)
{
$payload = [
'iss' => 'localhost',
'aud' => 'localhost',
'iat' => time(),
'nbf' => time(),
'exp' => time() + 64000,
'user_id' => rand(100000, 999999),
'user_name' => 'test',
'rights' => []
];
$jwt = JWT::encode($payload, config('app.jwt_private'), 'RS256');
Log::channel('gelf')->debug('Выпущен токен JWT', ['jwt' => $jwt]);
return $this->apiResponse->getSuccess(['token' => $jwt]);
}
}
hedii commented
Log::channel('gelf')->withContext([...
iamwildtuna commented