Skip to content

Commit

Permalink
add ip_address into request_data information
Browse files Browse the repository at this point in the history
  • Loading branch information
samsonasik committed Sep 24, 2018
1 parent 6af28a0 commit 1d928eb
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 5 deletions.
2 changes: 1 addition & 1 deletion README.md
Expand Up @@ -27,7 +27,7 @@ Features
- [x] Set default page (web access) or default message (console access) for error if configured 'display_errors' = 0.
- [x] Set default content when request is XMLHttpRequest via 'ajax' configuration.
- [x] Set default content when there is [no template service](https://github.com/zendframework/zend-expressive-template/blob/9b6c2e06f8c1d7e43750f72b64cc749552f2bdbe/src/TemplateRendererInterface.php) via 'no_template' configuration (ZF Expressive 3).
- [x] Provide request information ( http method, raw data, body data, query data, files data, and cookie data ).
- [x] Provide request information ( http method, raw data, body data, query data, files data, cookie data, and ip address).
- [x] Send Mail
- [x] many receivers to listed configured email
- [x] with include $_FILES into attachments on upload error.
Expand Down
9 changes: 5 additions & 4 deletions spec/Handler/Formatter/JsonSpec.php
Expand Up @@ -45,21 +45,22 @@
'raw_data' => '',
'files_data' => [],
'cookie_data' => [],
'ip_address' => '10.1.1.1',
],
],
];

$expected = "{\n \"timestamp\": \"2016-12-30T00:42:49+07:00\",\n \"priority\": 3,\n \"priorityName\": \"ERR\",\n \"message\": \"1: a sample exception preview\",\n \"extra\": {\n \"url\": \"http://localhost/error-preview?foo=bar&page=1\",\n \"file\": \"/path/to/app/vendor/samsonasik/error-hero-module/src/Controller/ErrorPreviewController.php\",\n \"line\": 11,\n \"error_type\": \"Exception\",\n \"trace\": \"#0 /path/to/app/vendor/zendframework/zend-mvc/src/Controller/AbstractActionController.php(78): ErrorHeroModule\\\\Controller\\\\ErrorPreviewController->exceptionAction()\n #1 /path/to/app/vendor/zendframework/zend-eventmanager/src/EventManager.php(322): Zend\\\\Mvc\\\\Controller\\\\AbstractActionController->onDispatch(Object(Zend\\\\Mvc\\\\MvcEvent))\n #2 /path/to/app/vendor/zendframework/zend-eventmanager/src/EventManager.php(179): Zend\\\\EventManager\\\\EventManager->triggerListeners(Object(Zend\\\\Mvc\\\\MvcEvent), Object(Closure))\n #3 /path/to/app/vendor/zendframework/zend-mvc/src/Controller/AbstractController.php(105): Zend\\\\EventManager\\\\EventManager->triggerEventUntil(Object(Closure), Object(Zend\\\\Mvc\\\\MvcEvent))\n #4 /path/to/app/vendor/zendframework/zend-mvc/src/DispatchListener.php(119): Zend\\\\Mvc\\\\Controller\\\\AbstractController->dispatch(Object(Zend\\\\Http\\\\PhpEnvironment\\\\Request), Object(Zend\\\\Http\\\\PhpEnvironment\\\\Response))\n #5 /path/to/app/vendor/zendframework/zend-eventmanager/src/EventManager.php(322): Zend\\\\Mvc\\\\DispatchListener->onDispatch(Object(Zend\\\\Mvc\\\\MvcEvent))\n #6 /path/to/app/vendor/zendframework/zend-eventmanager/src/EventManager.php(179): Zend\\\\EventManager\\\\EventManager->triggerListeners(Object(Zend\\\\Mvc\\\\MvcEvent), Object(Closure))\n #7 /path/to/app/vendor/zendframework/zend-mvc/src/Application.php(332): Zend\\\\EventManager\\\\EventManager->triggerEventUntil(Object(Closure), Object(Zend\\\\Mvc\\\\MvcEvent))\n #8 /path/to/app/public/index.php(40): Zend\\\\Mvc\\\\Application->run()\n #9 {main}\",\n \"request_data\": {\n \"request_method\": \"GET\",\n \"query_data\": {\n \"foo\": \"bar\",\n \"page\": \"1\"\n },\n \"body_data\": [],\n \"raw_data\": \"\",\n \"files_data\": [],\n \"cookie_data\": []\n }\n }\n}";
$actualOld = (new Json())->format($event);

expect('json_encode')->toBeCalled()->times(2);
expect((new Json())->format($event))->toBe($expected);
// idempotent format call will use old timestamp
$event['timestamp'] = DateTime::__set_state([
'date' => '2016-12-30 00:42:55.558706',
'timezone_type' => 3,
'timezone' => 'Asia/Jakarta',
]);
expect((new Json())->format($event))->toBe($expected);

$actualNew = (new Json())->format($event);
expect($actualNew)->toBe($actualOld);

});

Expand Down
3 changes: 3 additions & 0 deletions spec/Handler/Writer/MailSpec.php
Expand Up @@ -33,6 +33,7 @@
],
],
'cookie_data' => [],
'ip_address' => '10.1.1.1',
]
);
});
Expand Down Expand Up @@ -72,6 +73,7 @@
],
],
'cookie_data' => [],
'ip_address' => '10.1.1.1',
]
);

Expand Down Expand Up @@ -113,6 +115,7 @@
],
],
'cookie_data' => [],
'ip_address' => '10.1.1.1',
]
);

Expand Down
3 changes: 3 additions & 0 deletions src/Handler/Logging.php
Expand Up @@ -11,6 +11,7 @@
use Throwable;
use Zend\Console\Request as ConsoleRequest;
use Zend\Http\Header\Cookie;
use Zend\Http\PhpEnvironment\RemoteAddress;
use Zend\Http\PhpEnvironment\Request as HttpRequest;
use Zend\Log\Logger;
use Zend\Log\Writer\Db;
Expand Down Expand Up @@ -113,6 +114,7 @@ private function getRequestData(RequestInterface $request) : array
$cookie_data = $cookie instanceof Cookie
? $cookie->getArrayCopy()
: [];
$ip_address = (new RemoteAddress())->getIpAddress();

return [
'request_method' => $request_method,
Expand All @@ -121,6 +123,7 @@ private function getRequestData(RequestInterface $request) : array
'raw_data' => $raw_data,
'files_data' => $files_data,
'cookie_data' => $cookie_data,
'ip_address' => $ip_address,
];
}

Expand Down

0 comments on commit 1d928eb

Please sign in to comment.