This package allows you to add global context data for logging in easy way., (*1)
Installation
Add the tekord/laravel-logman package in your composer.json and update your dependencies or add it via command
line:, (*2)
$ composer require tekord/laravel-logman
If you are using Laravel < 5.5, you also need to add Tekord\Logman\ServiceProvider to your config/app.php providers array:, (*3)
'providers' => [
...
\Tekord\Logman\ServiceProvider::class,
]
and add Tekord\Logman\Facades\Logman to 'aliases' array:, (*4)
'aliases' => [
...
'Logman' => \Tekord\Logman\Facades\Logman::class,
]
Usage examples
Collect context data before request handling
Suppose we want to add incoming request information like request method, URI, user agent and referer to error log. Add the following middleware to your App\Http\Middleware namespace:, (*5)
<?php
namespace App\Http\Middleware;
class WatchWebRequest {
/**
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @param string|null $guard
*
* @return mixed
*/
public function handle($request, \Closure $next, $guard = null) {
Logman::put('request', [
'method' => $_SERVER['REQUEST_METHOD'],
'uri' => $_SERVER['REQUEST_URI'],
'user_agent' => isset($_SERVER['HTTP_USER_AGENT']) ? $_SERVER['HTTP_USER_AGENT'] : null,
'referer' => isset($_SERVER['HTTP_REFERER']) ? $_SERVER['HTTP_REFERER'] : null
]);
return $next($request);
}
}
Add it to Kernel's middleware list:, (*6)
protected $middleware = [
...
\App\Http\Middleware\WatchWebRequest::class,
];
Go to \App\Exceptions\Handler class and override the context method like this:, (*7)
protected function context() {
try {
$context = \Tekord\Logman\Facades\Logman::getContextWith([
// put additional custom context records here if you want
]);
} catch (\Throwable $e) {
$context = [];
}
return array_merge($context, parent::context());
}
Now if something goes wrong you will see request information in error log files., (*8)
Collect context data for specific controller's action
Add the following code to your controller class:, (*9)
public function callAction($method, $parameters) {
Logman::put('request', [
'method' => $_SERVER['REQUEST_METHOD'],
'uri' => $_SERVER['REQUEST_URI'],
'user_agent' => isset($_SERVER['HTTP_USER_AGENT']) ? $_SERVER['HTTP_USER_AGENT'] : null,
'referer' => isset($_SERVER['HTTP_REFERER']) ? $_SERVER['HTTP_REFERER'] : null
]);
return parent::callAction($method, $parameters);
}