-
Notifications
You must be signed in to change notification settings - Fork 10
/
ExceptionHandler.php
72 lines (57 loc) · 1.82 KB
/
ExceptionHandler.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
<?php
namespace Illuminated\Console\Exceptions;
use Exception;
use Illuminate\Foundation\Exceptions\Handler;
use Psr\Log\LoggerInterface;
class ExceptionHandler extends Handler
{
private $logger;
private $timeStarted;
private $timeFinished;
protected $reservedMemory;
public function setLogger(LoggerInterface $logger)
{
$this->logger = $logger;
}
public function initialize(LoggerInterface $logger)
{
$this->setLogger($logger);
$this->registerShutdownFunction();
}
public function report(Exception $e)
{
$context = [
'code' => $e->getCode(),
'message' => $e->getMessage(),
'file' => $e->getFile(),
'line' => $e->getLine(),
];
if ($e instanceof RuntimeException) {
$eContext = $e->getContext();
if (!empty($eContext)) {
$context['context'] = $eContext;
}
}
$this->logger->error($e->getMessage(), $context);
}
private function registerShutdownFunction()
{
$this->timeStarted = microtime(true);
$this->reservedMemory = str_repeat(' ', 20 * 1024);
register_shutdown_function([$this, 'onShutdown']);
}
public function onShutdown()
{
$this->reservedMemory = null;
$this->timeFinished = microtime(true);
$executionTime = round($this->timeFinished - $this->timeStarted, 3);
$this->logger->info("Execution time: {$executionTime} sec.");
$memoryPeak = format_bytes(memory_get_peak_usage(true));
$this->logger->info("Memory peak usage: {$memoryPeak}.");
$this->logger->info('%separator%');
$handlers = (array) $this->logger->getHandlers();
foreach ($handlers as $handler) {
$handler->close();
}
}
}