-
Notifications
You must be signed in to change notification settings - Fork 0
/
Logger.php
91 lines (77 loc) · 1.88 KB
/
Logger.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
<?php
namespace SomeCompanyNamespace\Services\Phishing;
use DatabaseServer;
use DateTime;
use Exception;
use SGException;
use UsageMonitor;
class Logger
{
/**
* logToAll method
*
* @param array $data
* @return int
*/
public function logToAll(array $data): int
{
$this->log($data);
$last_log_id = $this->saveToDB($data);
return $last_log_id;
}
/**
* log method
*
* @param array $data
* @return void
*/
public function log(array $data): void
{
$vendor_response = $data['vendor_response'];
UsageMonitor::Debug(
[
'key' => 'phishingServiceLog',
'cid' => $data['customer_id'] ?? '',
'url' => $data['url'] ?? '',
'responseCode' => $vendor_response->errorNo ?? '',
'responseBody' => $vendor_response->urlData->threatData->verdict ?? ''
],
"Log API call phishing_scan"
);
}
/**
* saveToDB method
*
* @param array $data
* @return int|SGException
*/
public function saveToDB(array $data): int
{
$now = new DateTime();
$data = [
'iTimestamp' => $now->format(\DateTime::ISO8601),
'iCustomerID' => $data['customer_id'] ?? '',
'iSurveyID' => $data['survey_id'] ?? '',
'sIdentifier' => $data['url'] ?? '',
'sResponse' => json_encode($data['vendor_response']) ?? ''
];
$adapter = DatabaseServer::getPrimaryAdapter();
$result = $adapter->insert('phishing_logs', $data);
if ($result === false) {
throw new SGException("Unable to insert row in " . get_class($this) . '::' . __FUNCTION__ . ' (triggered in ' . __METHOD__ . ')');
}
return $adapter->lastInsertId();
}
/**
* @param Exception $e
*/
public function logException(Exception $e): void
{
APM::captureThrowable($e);
UsageMonitor::Debug([
'key' => 'phishingServiceLogException',
'errorMsg' => $e->getMessage(),
'observed_location' => $e->getFile() . ':' . $e->getLine(),
], 'Exception: ' . get_class($e) . ' - ' . $e->getMessage());
}
}