1. Go to this page and download the library: Download hejunjie/error-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/ */
hejunjie / error-log example snippets
use Hejunjie\ErrorLog\Logger;
use Hejunjie\ErrorLog\Handlers;
$log = new Logger([
new Handlers\ConsoleHandler(), // Print to console
new Handlers\FileHandler('path'), // Save to file
new Handlers\RemoteApiHandler('url') // Send to a specific address
]);
$log->info('title','content',['Context']); // INFO Level
$log->warning('title','content',['Context']); // WARNING Level
$log->error('title','content',['Context']); // ERROR Level
$log->log('level','title','content',['Context']);
namespace app\exception;
use Carbon\Carbon;
use Throwable;
use Webman\Exception\ExceptionHandler;
use Webman\Http\Request;
use Webman\Http\Response;
use support\exception\BusinessException;
use Hejunjie\ErrorLog\Logger;
use Hejunjie\ErrorLog\Handlers;
/**
* Class Handler
* @package support\exception
*/
class Handler extends ExceptionHandler
{
public $dontReport = [
BusinessException::class,
];
public function report(Throwable $exception)
{
parent::report($exception);
if ($this->shouldntReport($exception)) {
return;
}
$request = request();
$date = Carbon::now()->timezone(config('app')['default_timezone'])->format('Y-m-d');
(new Logger([
new Handlers\FileHandler(runtime_path("logs/{$date}/critical")),
new Handlers\RemoteApiHandler(config('app')['log_report_url'])
]))->error(get_class($exception), $exception->getMessage(), [
'project' => config('app')['app_name'],
'ip' => $request->getRealIp(),
'method' => $request->method(),
'full_url' => $request->fullUrl(),
'trace' => $this->getDebugData($exception)
]);
}
public function render(Request $request, Throwable $exception): Response
{
$isDebug = config('app')['debug'] == 1;
$statusCode = $this->getHttpStatusCode($exception);
$response = [
'code' => $this->getErrorCode($exception),
'message' => $isDebug ? $exception->getMessage() : 'Server Error',
'data' => $isDebug ? $this->getDebugData($exception) : new \stdClass()
];
if ($requestId = $request->header('X-Request-ID')) {
$response['request_id'] = $requestId;
}
return new Response(
$statusCode,
['Content-Type' => 'application/json'],
json_encode($response, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES)
);
}
protected function getHttpStatusCode(Throwable $exception): int
{
$code = $exception->getCode();
return ($code >= 100 && $code < 600) ? $code : 500;
}
protected function getErrorCode(Throwable $exception): int
{
return $exception->getCode() ?: 500;
}
protected function getDebugData(Throwable $exception): array
{
$trace = $exception->getTrace();
$simplifiedTrace = array_map(function ($frame) {
return [
'file' => $frame['file'] ?? '[internal function]',
'line' => $frame['line'] ?? 0,
'function' => $frame['function'] ?? null,
'class' => $frame['class'] ?? null,
'type' => $frame['type'] ?? null
];
}, $trace);
return [
'class' => get_class($exception),
'message' => $exception->getMessage(),
'code' => $exception->getCode(),
'file' => $exception->getFile(),
'line' => $exception->getLine(),
'trace' => config('app')['debug'] == 1 ? $simplifiedTrace : array_slice($simplifiedTrace, 0, 5),
'previous' => $exception->getPrevious() ? [
'class' => get_class($exception->getPrevious()),
'message' => $exception->getPrevious()->getMessage()
] : null
];
}
}
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.