Skip to content

Commit 1284adc

Browse files
committed
Added logger fix issue sending events
1 parent 4157c82 commit 1284adc

11 files changed

Lines changed: 90 additions & 87 deletions

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@
2525
"require": {
2626
"php": ">=7.2.0",
2727
"guzzlehttp/guzzle": "^6.0",
28-
"antecedent/patchwork": "~2.0"
28+
"antecedent/patchwork": "~2.0",
29+
"monolog/monolog": "2.0.2"
2930
},
3031
"require-dev": {
3132
"phpunit/phpunit": "^8",

src/Agent.php

Lines changed: 0 additions & 32 deletions
This file was deleted.

src/EventManager.php

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
namespace SecureNative\sdk;
44

5+
use DateTime;
6+
use DateTimeZone;
57
use GuzzleHttp\Psr7\Request;
68
use GuzzleHttp\Exception\RequestException;
79
use phpDocumentor\Reflection\Types\Array_;
@@ -39,11 +41,16 @@ public function buildEvent(EventOptions $opts)
3941
$method = $opts->context->method ? $opts->context->method : '';
4042
$url = $opts->context->url ? $opts->context->url : '';
4143
$headers =$opts->context->headers ? $opts->context->headers : null;
42-
$request = new Request($cid, $vid, $fp, $ip, $remoteIp, $method, $url, $headers);
44+
45+
$reqCtx = new RequestContext($cid, $vid, $fp, $ip, $remoteIp, $method, $url, $headers);
46+
4347
$properties = $opts->properties;
44-
$timestamp = $opts->timestamp ? $opts->timestamp : (new DateTime("now", new DateTimeZone("UTC")))->format(DateTime::ISO8601);
48+
$timestamp = null;
49+
try {
50+
$timestamp = $opts->timestamp ? $opts->timestamp : (new DateTime("now", new DateTimeZone("UTC")))->format(DateTime::ISO8601);
51+
} catch (\Exception $e) {}
4552

46-
$event = new SecurenativeEvent($rid, $eventType, $userId, $userTraits, $request, $properties, $timestamp);
53+
$event = new SecurenativeEvent($rid, $eventType, $userId, $userTraits, $reqCtx, $properties, $timestamp);
4754

4855
Logger::debug('Created event', $event);
4956

@@ -59,7 +66,7 @@ public function sendSync(SecurenativeEvent $event, $requestUrl)
5966
Logger::debug('Successfully sent event', $event);
6067
return json_decode($body);
6168
} catch (RequestException $e) {
62-
Logger::debug('Failed to send event', $e->getMessage());
69+
Logger::error('Failed to send event', $e->getMessage());
6370
return null;
6471
}
6572
}
@@ -78,7 +85,7 @@ public function sendAsync(SecurenativeEvent $event, $requestUrl)
7885
try {
7986
$this::sendEvents();
8087
} catch (Exception $e) {
81-
Logger::debug("Failed to send queue events", $e->getMessage());
88+
Logger::error("Failed to send queue events", $e->getMessage());
8289
return;
8390
}
8491
}
@@ -93,10 +100,10 @@ private function sendEvents()
93100
if (($key = array_search($request, $this->eventsQueue)) !== false) {
94101
unset($this->eventsQueue[$key]);
95102
}
96-
}, function (RequestException $e) {
97-
Logger::debug("Failed to send event request", $e->getMessage());
103+
}, function (Exception $e) {
104+
Logger::error("Failed to send event request", $e->getMessage());
98105
});
99-
$promise->wait();
106+
$promise->wait(false);
100107
}
101108
}
102109

src/Logger.php

Lines changed: 28 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,29 +2,44 @@
22

33
namespace SecureNative\sdk;
44

5+
use Monolog\Logger as MonoLogger;
6+
use Monolog\Handler\StreamHandler;
57

68
class Logger
79
{
8-
private static $debugEnabled = false;
10+
private static $logger;
11+
private static $logLevels = [
12+
'debug' => MonoLogger::DEBUG,
13+
'info' => MonoLogger::INFO,
14+
'warning' => MonoLogger::WARNING,
15+
'error' => MonoLogger::ERROR,
16+
'fatal' => MonoLogger::CRITICAL,
17+
];
918

1019
public static function init(SecureNativeOptions $options) {
11-
if ($options->getDebugMode()) {
12-
self::$debugEnabled = true;
13-
}
14-
}
15-
16-
private static function baseMessage($message, ...$args) {
17-
// TODO: Add log library
18-
echo "\n" . $message . " " . json_encode($args);
20+
Logger::$logger = new MonoLogger('securenative-sdk');
21+
//$logLevel = Logger::$logLevels[$options->getLogLevel()];
22+
$logLevel = Logger::$logLevels['debug'];
23+
Logger::$logger->pushHandler(new StreamHandler('php://stdout', $level = $logLevel, $bubble = true));
1924
}
2025

2126
public static function debug($message, ...$args) {
22-
if (self::$debugEnabled) {
23-
self::baseMessage($message, $args);
24-
}
27+
Logger::$logger->debug($message, $args);
2528
}
2629

2730
public static function info($message, ...$args) {
28-
self::baseMessage($message, $args);
31+
Logger::$logger->info($message, $args);
32+
}
33+
34+
public static function error($message, ...$args) {
35+
Logger::$logger->error($message, $args);
36+
}
37+
38+
public static function warning($message, ...$args) {
39+
Logger::$logger->warning($message, $args);
40+
}
41+
42+
public static function fatal($message, ...$args) {
43+
Logger::$logger->critical($message, $args);
2944
}
3045
}

src/Models/Context.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,16 @@ class Context
88
public $ip;
99
public $remoteIp;
1010
public $headers;
11+
public $url;
12+
public $method;
1113

12-
public function __construct($clientToken = null, $ip = null, $remoteIp = null, $headers = null)
14+
public function __construct($clientToken = null, $ip = null, $remoteIp = null, $headers = null, $url= null, $method = null)
1315
{
1416
$this->clientToken = $clientToken;
1517
$this->ip = $ip;
1618
$this->remoteIp = $remoteIp;
1719
$this->headers = $headers;
20+
$this->url = $url;
21+
$this->method = $method;
1822
}
1923
}

src/Models/EventOptions.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ class EventOptions
1717
const EVENT_CONTEXT_IP = 'ip';
1818
const EVENT_CONTEXT_REMOTE_IP = 'remoteIp';
1919
const EVENT_CONTEXT_HEADERS = 'headers';
20+
const EVENT_CONTEXT_URL = 'url';
21+
const EVENT_CONTEXT_METHOD = 'url';
2022
const EVENT_PROPERTIES = 'properties';
2123
const EVENT_TIMESTAMP = 'timestamp';
2224

@@ -51,11 +53,14 @@ private function set($data){
5153
isset($context[self::EVENT_CONTEXT_CLIENT_TOKEN]) ? $context[self::EVENT_CONTEXT_CLIENT_TOKEN] : '',
5254
isset($context[self::EVENT_CONTEXT_IP]) ? $context[self::EVENT_CONTEXT_IP] : '',
5355
isset($context[self::EVENT_CONTEXT_REMOTE_IP]) ? $context[self::EVENT_CONTEXT_REMOTE_IP] : '',
54-
isset($context[self::EVENT_CONTEXT_HEADERS]) ? $context[self::EVENT_CONTEXT_HEADERS] : ''
56+
isset($context[self::EVENT_CONTEXT_HEADERS]) ? $context[self::EVENT_CONTEXT_HEADERS] : '',
57+
isset($context[self::EVENT_CONTEXT_URL]) ? $context[self::EVENT_CONTEXT_URL] : '',
58+
isset($context[self::EVENT_CONTEXT_METHOD]) ? $context[self::EVENT_CONTEXT_METHOD] : ''
5559
);
5660
}
5761

58-
if (sizeof($data[self::EVENT_PROPERTIES] > self::MAX_EVENT_PROPERTIES_COUNT)) {
62+
63+
if (count($data[self::EVENT_PROPERTIES]) > self::MAX_EVENT_PROPERTIES_COUNT) {
5964
throw new Exception('You can only set up to '+ self::MAX_EVENT_PROPERTIES_COUNT +' custom properties');
6065
}
6166

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
namespace SecureNative\sdk;
44

5-
class Request
5+
class RequestContext
66
{
77
public $cid;
88
public $vid;
@@ -46,7 +46,7 @@ public function getCid()
4646

4747
/**
4848
* @param mixed $cid
49-
* @return Request
49+
* @return RequestContext
5050
*/
5151
public function setCid($cid)
5252
{
@@ -64,7 +64,7 @@ public function getVid()
6464

6565
/**
6666
* @param mixed $vid
67-
* @return Request
67+
* @return RequestContext
6868
*/
6969
public function setVid($vid)
7070
{
@@ -82,7 +82,7 @@ public function getFp()
8282

8383
/**
8484
* @param mixed $fp
85-
* @return Request
85+
* @return RequestContext
8686
*/
8787
public function setFp($fp)
8888
{
@@ -100,7 +100,7 @@ public function getIp()
100100

101101
/**
102102
* @param mixed $ip
103-
* @return Request
103+
* @return RequestContext
104104
*/
105105
public function setIp($ip)
106106
{
@@ -118,7 +118,7 @@ public function getRemoteIp()
118118

119119
/**
120120
* @param mixed $remoteIp
121-
* @return Request
121+
* @return RequestContext
122122
*/
123123
public function setRemoteIp($remoteIp)
124124
{
@@ -136,7 +136,7 @@ public function getMethod()
136136

137137
/**
138138
* @param mixed $method
139-
* @return Request
139+
* @return RequestContext
140140
*/
141141
public function setMethod($method)
142142
{
@@ -154,7 +154,7 @@ public function getUrl()
154154

155155
/**
156156
* @param mixed $url
157-
* @return Request
157+
* @return RequestContext
158158
*/
159159
public function setUrl($url)
160160
{
@@ -172,7 +172,7 @@ public function getHeaders()
172172

173173
/**
174174
* @param mixed $headers
175-
* @return Request
175+
* @return RequestContext
176176
*/
177177
public function setHeaders($headers)
178178
{

src/SecureNative.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ public static function init($apiKey, SecureNativeOptions $secureNativeOptions)
1717
if ($apiKey == '') {
1818
throw new Exception('You must pass your SecureNative api key');
1919
}
20-
2120
Logger::init($secureNativeOptions);
2221

2322
self::$apiKey = $apiKey;

src/SecureNativeOptions.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ class SecureNativeOptions
88
private $apiUrl = 'https://api.securenative.com/collector/api/v1';
99
private $interval = 1000;
1010
private $maxEvents = 1000;
11-
private $timeout = 3000;
11+
private $timeout = 30000;
1212
private $autoSend = true;
1313
private $disable = false;
1414
private $logLevel = 'fatal';

src/Utils.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,11 @@ public static function headersFromRequest()
4747

4848
public static function urlFromRequest()
4949
{
50-
return (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on' ? "https" : "http") . "://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
50+
if (array_key_exists('HTTPS', $_SERVER)) {
51+
return 'http' . (isset($_SERVER['HTTPS']) ? 's' : '') . '://' . "{$_SERVER['HTTP_HOST']}/{$_SERVER['REQUEST_URI']}";
52+
}
53+
54+
return '';
5155
}
5256

5357
public static function methodFromRequest()

0 commit comments

Comments
 (0)