1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Copyright 2016 - 2018, Cake Development Corporation (http://cakedc.com) |
4
|
|
|
* |
5
|
|
|
* Licensed under The MIT License |
6
|
|
|
* Redistributions of files must retain the above copyright notice. |
7
|
|
|
* |
8
|
|
|
* @copyright Copyright 2016 - 2018, Cake Development Corporation (http://cakedc.com) |
9
|
|
|
* @license MIT License (http://www.opensource.org/licenses/mit-license.php) |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace CakeDC\Api\Service\Extension; |
13
|
|
|
|
14
|
|
|
use CakeDC\Api\Service\Action\Action; |
15
|
|
|
use CakeDC\Api\Service\Service; |
16
|
|
|
use Cake\Event\Event; |
17
|
|
|
use Cake\Event\EventListenerInterface; |
18
|
|
|
use Cake\Log\LogTrait; |
19
|
|
|
use Psr\Log\LogLevel; |
20
|
|
|
|
21
|
|
|
class LogExtension extends Extension implements EventListenerInterface |
22
|
|
|
{ |
23
|
|
|
use LogTrait; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @var Service |
27
|
|
|
*/ |
28
|
|
|
protected $_service; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @var Action |
32
|
|
|
*/ |
33
|
|
|
protected $_action; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @var int |
37
|
|
|
*/ |
38
|
|
|
protected $_timer; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Returns a list of events this object is implementing. When the class is registered |
42
|
|
|
* in an event manager, each individual method will be associated with the respective event. |
43
|
|
|
* |
44
|
|
|
* @return array |
45
|
|
|
*/ |
46
|
|
|
public function implementedEvents() |
47
|
|
|
{ |
48
|
|
|
return [ |
49
|
|
|
'Service.beforeDispatch' => 'beforeProcess', |
50
|
|
|
'Service.afterDispatch' => 'afterProcess', |
51
|
|
|
]; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* before process |
56
|
|
|
* |
57
|
|
|
* @param Event $event An Event instance. |
58
|
|
|
* @return void |
59
|
|
|
*/ |
60
|
|
|
public function beforeProcess(Event $event) |
61
|
|
|
{ |
62
|
|
|
$this->_service = $event->getData('service'); |
63
|
|
|
$this->_timer = microtime(true); |
|
|
|
|
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* after process |
68
|
|
|
* |
69
|
|
|
* @param Event $event An Event instance. |
70
|
|
|
* @return void |
71
|
|
|
*/ |
72
|
|
|
public function afterProcess(Event $event) |
|
|
|
|
73
|
|
|
{ |
74
|
|
|
$duration = round((microtime(true) - $this->_timer) * 1000, 0); |
75
|
|
|
$url = $this->_service->getBaseUrl(); |
76
|
|
|
$data = $this->_service->getParser()->getParams(); |
77
|
|
|
$result = $this->_service->getResult()->toArray(); |
78
|
|
|
$log = [ |
79
|
|
|
'url' => $url, |
80
|
|
|
'method' => env('REQUEST_METHOD'), |
81
|
|
|
'duration' => $duration . 'ms', |
82
|
|
|
'input' => $data, |
83
|
|
|
'result' => json_encode($result, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE), |
84
|
|
|
]; |
85
|
|
|
$this->log($log, LogLevel::INFO, ['api']); |
86
|
|
|
} |
87
|
|
|
} |
88
|
|
|
|
This check looks for assignments to scalar types that may be of the wrong type.
To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.