Skip to content

Commit 1d928eb

Browse files
committed
add ip_address into request_data information
1 parent 6af28a0 commit 1d928eb

File tree

4 files changed

+12
-5
lines changed

4 files changed

+12
-5
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ Features
2727
- [x] Set default page (web access) or default message (console access) for error if configured 'display_errors' = 0.
2828
- [x] Set default content when request is XMLHttpRequest via 'ajax' configuration.
2929
- [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).
30-
- [x] Provide request information ( http method, raw data, body data, query data, files data, and cookie data ).
30+
- [x] Provide request information ( http method, raw data, body data, query data, files data, cookie data, and ip address).
3131
- [x] Send Mail
3232
- [x] many receivers to listed configured email
3333
- [x] with include $_FILES into attachments on upload error.

spec/Handler/Formatter/JsonSpec.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,21 +45,22 @@
4545
'raw_data' => '',
4646
'files_data' => [],
4747
'cookie_data' => [],
48+
'ip_address' => '10.1.1.1',
4849
],
4950
],
5051
];
5152

52-
$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}";
53+
$actualOld = (new Json())->format($event);
5354

54-
expect('json_encode')->toBeCalled()->times(2);
55-
expect((new Json())->format($event))->toBe($expected);
5655
// idempotent format call will use old timestamp
5756
$event['timestamp'] = DateTime::__set_state([
5857
'date' => '2016-12-30 00:42:55.558706',
5958
'timezone_type' => 3,
6059
'timezone' => 'Asia/Jakarta',
6160
]);
62-
expect((new Json())->format($event))->toBe($expected);
61+
62+
$actualNew = (new Json())->format($event);
63+
expect($actualNew)->toBe($actualOld);
6364

6465
});
6566

spec/Handler/Writer/MailSpec.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
],
3434
],
3535
'cookie_data' => [],
36+
'ip_address' => '10.1.1.1',
3637
]
3738
);
3839
});
@@ -72,6 +73,7 @@
7273
],
7374
],
7475
'cookie_data' => [],
76+
'ip_address' => '10.1.1.1',
7577
]
7678
);
7779

@@ -113,6 +115,7 @@
113115
],
114116
],
115117
'cookie_data' => [],
118+
'ip_address' => '10.1.1.1',
116119
]
117120
);
118121

src/Handler/Logging.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
use Throwable;
1212
use Zend\Console\Request as ConsoleRequest;
1313
use Zend\Http\Header\Cookie;
14+
use Zend\Http\PhpEnvironment\RemoteAddress;
1415
use Zend\Http\PhpEnvironment\Request as HttpRequest;
1516
use Zend\Log\Logger;
1617
use Zend\Log\Writer\Db;
@@ -113,6 +114,7 @@ private function getRequestData(RequestInterface $request) : array
113114
$cookie_data = $cookie instanceof Cookie
114115
? $cookie->getArrayCopy()
115116
: [];
117+
$ip_address = (new RemoteAddress())->getIpAddress();
116118

117119
return [
118120
'request_method' => $request_method,
@@ -121,6 +123,7 @@ private function getRequestData(RequestInterface $request) : array
121123
'raw_data' => $raw_data,
122124
'files_data' => $files_data,
123125
'cookie_data' => $cookie_data,
126+
'ip_address' => $ip_address,
124127
];
125128
}
126129

0 commit comments

Comments
 (0)