Skip to content

Commit 8603678

Browse files
committed
Log functionality added
1 parent 2539d40 commit 8603678

File tree

3 files changed

+95
-43
lines changed

3 files changed

+95
-43
lines changed

src/Client/HttpClient.php

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,15 @@ class HttpClient
1818
*/
1919
private $projectSecret;
2020

21+
//Local
22+
private const POST_EXCEPTION = 'http://larvabug.local/api/v1/exception';
23+
private const VALIDATE_CREDENTIALS = 'http://larvabug.local/api/v1/validate/credentials';
24+
private const POST_FEEDBACK = 'http://larvabug.local/api/v1/feedback/submit';
25+
2126
//Development
22-
private const POST_EXCEPTION = 'http://dev.larvabug.com/api/v1/exception';
23-
private const VALIDATE_CREDENTIALS = 'http://dev.larvabug.com/api/v1/validate/credentials';
24-
private const POST_FEEDBACK = 'http://dev.larvabug.com/api/v1/feedback/submit';
27+
// private const POST_EXCEPTION = 'http://dev.larvabug.com/api/v1/exception';
28+
// private const VALIDATE_CREDENTIALS = 'http://dev.larvabug.com/api/v1/validate/credentials';
29+
// private const POST_FEEDBACK = 'http://dev.larvabug.com/api/v1/feedback/submit';
2530

2631

2732
/**

src/Handler/LarvaBugExceptionHandler.php

Lines changed: 40 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
use Illuminate\Support\Facades\App;
77
use Illuminate\Support\Facades\Cache;
8+
use Illuminate\Support\Facades\Log;
89
use Illuminate\Support\Facades\Session;
910
use Illuminate\Support\Facades\URL;
1011
use LarvaBug\Client\HttpClient;
@@ -38,21 +39,53 @@ public function __construct(
3839
$this->exceptionData = $exceptionData;
3940
}
4041

42+
/**
43+
* Report exception to larvabug
44+
*
45+
* @param \Throwable $exception
46+
* @return bool
47+
*/
4148
public function report(\Throwable $exception)
4249
{
43-
if (!$this->checkAppEnvironment()){
44-
return false;
45-
}
50+
try {
51+
if (!$this->checkAppEnvironment()) {
52+
return false;
53+
}
54+
55+
if ($this->skipError(get_class($exception))) {
56+
return false;
57+
}
58+
59+
$data = $this->exceptionData->prepareException($exception);
4660

47-
if ($this->skipError(get_class($exception))){
61+
$this->client->report($data);
62+
63+
return true;
64+
}catch (\Exception $exception){
65+
Log::info('Lavabug Exception :'.$exception->getMessage());
4866
return false;
4967
}
68+
}
5069

51-
$data = $this->exceptionData->prepare($exception);
70+
/**
71+
* Log details to larvabug
72+
*
73+
* @param $message
74+
* @param array $meta
75+
* @return bool
76+
*/
77+
public function log($message, array $meta = [])
78+
{
79+
try {
80+
$data = $this->exceptionData->prepareLogData($message, $meta);
5281

53-
$this->client->report($data);
82+
$this->client->report($data);
5483

55-
return true;
84+
return true;
85+
}catch (\Exception $exception){
86+
Log::info('Lavabug Exception :'.$exception->getMessage());
87+
return false;
88+
}
5689
}
5790

5891
/**

src/Handler/PrepareExceptionData.php

Lines changed: 47 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,53 @@ public function __construct()
2727
* @author Syed Faisal <sfkazmi0@gmail.com>
2828
* @param \Throwable $exception
2929
*/
30-
public function prepare(\Throwable $exception)
30+
public function prepareException(\Throwable $exception)
3131
{
32-
return $this->getExceptionData($exception);
32+
$data = $this->getRequestInfo($exception);
33+
34+
$data['exception'] = $exception->getMessage();
35+
$data['class'] = get_class($exception);
36+
$data['file'] = $exception->getFile();
37+
$data['line'] = $exception->getLine();
38+
$data['error'] = $exception->getTraceAsString();
39+
$data['trace_with_details'] = $this->prepareTraceData($exception->getTrace());
40+
41+
$count = config('larvabug.lines_count');
42+
43+
if (!$count || $count > 12) {
44+
$count = 10;
45+
}
46+
47+
$lines = file($data['file']);
48+
$data['executor'] = [];
49+
50+
for ($i = -1 * abs($count); $i <= abs($count); $i++) {
51+
$data['executor'][] = $this->getLineInfo($lines, $data['line'], $i);
52+
}
53+
54+
$data['executor'] = array_filter($data['executor']);
55+
56+
// to make symfony exception more readable
57+
if ($data['class'] == 'Symfony\Component\Debug\Exception\FatalErrorException') {
58+
preg_match("~^(.+)' in ~", $data['exception'], $matches);
59+
if (isset($matches[1])) {
60+
$data['exception'] = $matches[1];
61+
}
62+
}
63+
64+
return $data;
65+
}
66+
67+
public function prepareLogData(string $message,array $meta = [])
68+
{
69+
$data = $this->getRequestInfo();
70+
$data['exception'] = $message;
71+
$data['class'] = 'Log Information';
72+
$data['type'] = 'log';
73+
$data['meta_data'] = $meta;
74+
75+
return $data;
76+
3377
}
3478

3579
/**
@@ -38,13 +82,10 @@ public function prepare(\Throwable $exception)
3882
* @param \Throwable $exception
3983
* @return array
4084
*/
41-
private function getExceptionData(\Throwable $exception)
85+
private function getRequestInfo()
4286
{
4387
$data = [];
4488

45-
$data['exception'] = $exception->getMessage();
46-
$data['class'] = get_class($exception);
47-
$data['file'] = $exception->getFile();
4889
$data['php_version'] = PHP_VERSION;
4990
$data['server_ip'] = $_SERVER['SERVER_ADDR'] ?? null;
5091
$data['environment'] = App::environment();
@@ -56,7 +97,6 @@ private function getExceptionData(\Throwable $exception)
5697
$data['fullUrl'] = Request::fullUrl();
5798
$data['url'] = Request::path();
5899
$data['userIp'] = Request::ip();
59-
$data['line'] = $exception->getLine();
60100
$data['date_time'] = date("Y-m-d H:i:s");
61101
$data['session_id'] = Session::getId();
62102
$data['storage'] = [
@@ -76,34 +116,8 @@ private function getExceptionData(\Throwable $exception)
76116
'HEADERS' => $this->filterBlackList(Request::header()),
77117
];
78118
$data['auth_user'] = $this->getAuthUser();
79-
$data['error'] = $exception->getTraceAsString();
80-
$data['trace_with_details'] = $this->prepareTraceData($exception->getTrace());
81-
82119
$data['storage'] = array_filter($data['storage']);
83120

84-
$count = config('larvabug.lines_count');
85-
86-
if (!$count || $count > 12) {
87-
$count = 10;
88-
}
89-
90-
$lines = file($data['file']);
91-
$data['executor'] = [];
92-
93-
for ($i = -1 * abs($count); $i <= abs($count); $i++) {
94-
$data['executor'][] = $this->getLineInfo($lines, $data['line'], $i);
95-
}
96-
97-
$data['executor'] = array_filter($data['executor']);
98-
99-
// to make symfony exception more readable
100-
if ($data['class'] == 'Symfony\Component\Debug\Exception\FatalErrorException') {
101-
preg_match("~^(.+)' in ~", $data['exception'], $matches);
102-
if (isset($matches[1])) {
103-
$data['exception'] = $matches[1];
104-
}
105-
}
106-
107121
return $data;
108122
}
109123

0 commit comments

Comments
 (0)