Skip to content

Commit 419a1d1

Browse files
authored
Merge pull request #7 from ElementaryFramework/feature/better-errors-logging
Implement errors loggers
2 parents 1389c3a + e095efe commit 419a1d1

File tree

5 files changed

+253
-17
lines changed

5 files changed

+253
-17
lines changed
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
<?php
2+
3+
/**
4+
* WaterPipe - URL routing framework for PHP
5+
*
6+
* Permission is hereby granted, free of charge, to any person obtaining a copy
7+
* of this software and associated documentation files (the "Software"), to deal
8+
* in the Software without restriction, including without limitation the rights
9+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
* copies of the Software, and to permit persons to whom the Software is
11+
* furnished to do so, subject to the following conditions:
12+
*
13+
* The above copyright notice and this permission notice shall be included in all
14+
* copies or substantial portions of the Software.
15+
*
16+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22+
* SOFTWARE.
23+
*
24+
* @category Library
25+
* @package WaterPipe
26+
* @author Axel Nana <[email protected]>
27+
* @copyright 2018 Aliens Group, Inc.
28+
* @license MIT <https://github.com/ElementaryFramework/WaterPipe/blob/master/LICENSE>
29+
* @version 1.3.0
30+
* @link http://waterpipe.na2axl.tk
31+
*/
32+
33+
namespace ElementaryFramework\WaterPipe\Configuration;
34+
35+
/**
36+
* Error logger based on the `error_log` PHP function.
37+
*/
38+
class DefaultErrorLogger implements IErrorLogger
39+
{
40+
/**
41+
* The message type.
42+
* The field only take as values the ones accepted by the
43+
* second parameter of the `error_log` PHP function.
44+
*
45+
* @var integer
46+
*/
47+
private $_messageType;
48+
49+
/**
50+
* The message destination.
51+
* The field only take as values the ones accepted by the
52+
* third parameter of the `error_log` PHP function.
53+
*
54+
* @var string
55+
*/
56+
private $_destination;
57+
58+
/**
59+
* The message extra headers.
60+
* The field is only used when message type equals 1, and
61+
* only take as values the ones accepted by the
62+
* fourth parameter of the `error_log` PHP function.
63+
*
64+
* @var string
65+
*/
66+
private $_extraHeaders;
67+
68+
/**
69+
* Creates a new instance of the default error logger.
70+
*
71+
* @param integer $messageType The message type, serving as the second parameter of the `error_log` PHP function
72+
* @param string $destination The message destination, serving as the third parameter of the `error_log` PHP function
73+
* @param string $extraHeaders The message extra headers, serving as the fourth parameter of the `error_log` PHP function
74+
*/
75+
public function __construct($messageType = null, $destination = null, $extraHeaders = null)
76+
{
77+
$this->_messageType = $messageType;
78+
$this->_destination = $destination;
79+
$this->_extraHeaders = $extraHeaders;
80+
}
81+
82+
/**
83+
* @inheritDoc
84+
*/
85+
public function log(string $message)
86+
{
87+
error_log($message, $this->_messageType, $this->_destination, $this->_extraHeaders);
88+
}
89+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
<?php
2+
3+
/**
4+
* WaterPipe - URL routing framework for PHP
5+
*
6+
* Permission is hereby granted, free of charge, to any person obtaining a copy
7+
* of this software and associated documentation files (the "Software"), to deal
8+
* in the Software without restriction, including without limitation the rights
9+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
* copies of the Software, and to permit persons to whom the Software is
11+
* furnished to do so, subject to the following conditions:
12+
*
13+
* The above copyright notice and this permission notice shall be included in all
14+
* copies or substantial portions of the Software.
15+
*
16+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22+
* SOFTWARE.
23+
*
24+
* @category Library
25+
* @package WaterPipe
26+
* @author Axel Nana <[email protected]>
27+
* @copyright 2018 Aliens Group, Inc.
28+
* @license MIT <https://github.com/ElementaryFramework/WaterPipe/blob/master/LICENSE>
29+
* @version 1.3.0
30+
* @link http://waterpipe.na2axl.tk
31+
*/
32+
33+
namespace ElementaryFramework\WaterPipe\Configuration;
34+
35+
/**
36+
* Interface used to recognize an error logging configuration
37+
* entry.
38+
*/
39+
interface IErrorLogger
40+
{
41+
/**
42+
* Logs the given message to the output wrapped by this IErrorLoggingConfiguration
43+
* implementation
44+
*
45+
* @param string $message The message to log.
46+
*
47+
* @return void
48+
*/
49+
public function log(string $message);
50+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
<?php
2+
3+
/**
4+
* WaterPipe - URL routing framework for PHP
5+
*
6+
* Permission is hereby granted, free of charge, to any person obtaining a copy
7+
* of this software and associated documentation files (the "Software"), to deal
8+
* in the Software without restriction, including without limitation the rights
9+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
* copies of the Software, and to permit persons to whom the Software is
11+
* furnished to do so, subject to the following conditions:
12+
*
13+
* The above copyright notice and this permission notice shall be included in all
14+
* copies or substantial portions of the Software.
15+
*
16+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22+
* SOFTWARE.
23+
*
24+
* @category Library
25+
* @package WaterPipe
26+
* @author Axel Nana <[email protected]>
27+
* @copyright 2018 Aliens Group, Inc.
28+
* @license MIT <https://github.com/ElementaryFramework/WaterPipe/blob/master/LICENSE>
29+
* @version 1.3.0
30+
* @link http://waterpipe.na2axl.tk
31+
*/
32+
33+
namespace ElementaryFramework\WaterPipe\Configuration;
34+
35+
/**
36+
* Error logger which redirect message to the STDERR output stream.
37+
*/
38+
class StdErrorLogger implements IErrorLogger
39+
{
40+
/**
41+
* @inheritDoc
42+
*/
43+
public function log(string $message)
44+
{
45+
$stream = fopen("php://stderr", 'w');
46+
fwrite($stream, $message, strlen($message));
47+
fclose($stream);
48+
}
49+
}

src/WaterPipe/WaterPipe.php

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -570,11 +570,10 @@ private function _executeRequest()
570570
$this->_executeAction($this->_errorsRegistry[ResponseStatus::InternalServerErrorCode]);
571571
} else {
572572
$config = WaterPipeConfig::get();
573-
if ($config->useStderr()) {
574-
$stream = fopen("php://stderr", 'w');
573+
$logger = $config->errorLogger();
574+
if ($logger !== null) {
575575
$string = $e->__toString();
576-
fwrite($stream, $string, strlen($string));
577-
fclose($stream);
576+
$logger->log($string);
578577
} else throw $e;
579578
}
580579
}

src/WaterPipe/WaterPipeConfig.php

Lines changed: 62 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,10 @@
3232

3333
namespace ElementaryFramework\WaterPipe;
3434

35+
use ElementaryFramework\WaterPipe\Configuration\DefaultErrorLogger;
36+
use ElementaryFramework\WaterPipe\Configuration\IErrorLogger;
37+
use ElementaryFramework\WaterPipe\Configuration\StdErrorLogger;
38+
3539
class WaterPipeConfig
3640
{
3741
/**
@@ -71,18 +75,19 @@ public static function get(): WaterPipeConfig
7175
private $_defaultCharset = "utf-8";
7276

7377
/**
74-
* Defines if WaterPipe uses the stderr output channel
75-
* to print errors and uncaught exceptions.
78+
* Defines the error logger to use in WaterPipe.
7679
*
77-
* @var bool
80+
* @var IErrorLogger
7881
*/
79-
private $_useStderr = true;
82+
private $_errorLogger = null;
8083

8184
/**
8285
* WaterPipeConfig constructor.
8386
*/
8487
private function __construct()
85-
{ }
88+
{
89+
$this->_errorLogger = new DefaultErrorLogger();
90+
}
8691

8792
/**
8893
* Checks if query strings are enabled.
@@ -97,14 +102,20 @@ public function isQueryStringEnabled(): bool
97102
/**
98103
* Set the enabled state of query strings.
99104
*
100-
* @param bool $enable
105+
* @param bool $enable true to enable query strings, false otherwise.
106+
*
107+
* @return self
101108
*/
102-
public function setQueryStringEnabled(bool $enable): void
109+
public function setQueryStringEnabled(bool $enable): WaterPipeConfig
103110
{
104111
$this->_queryStringEnabled = $enable;
112+
113+
return $this;
105114
}
106115

107116
/**
117+
* Returns the defined default charset.
118+
*
108119
* @return string
109120
*/
110121
public function getDefaultCharset(): string
@@ -113,33 +124,71 @@ public function getDefaultCharset(): string
113124
}
114125

115126
/**
127+
* Sets the default charset to use when sending responses.
128+
*
116129
* @param string $defaultCharset
130+
*
131+
* @return self
117132
*/
118-
public function setDefaultCharset(string $defaultCharset): void
133+
public function setDefaultCharset(string $defaultCharset): WaterPipeConfig
119134
{
120135
$this->_defaultCharset = $defaultCharset;
136+
137+
return $this;
121138
}
122139

123140
/**
124141
* Checks if WaterPipe print errors and uncaught exceptions in the stderr.
142+
* This method is deprecated and it is not recommended to use it in new projects.
125143
*
144+
* @deprecated 1.3.0
145+
*
126146
* @return bool
127147
*/
128-
public function useStderr()
148+
public function useStderr(): bool
129149
{
130-
return $this->_useStderr;
150+
return $this->_errorLogger instanceof StdErrorLogger;
131151
}
132152

133153
/**
134154
* Set if WaterPipe have to print errors and uncaught exceptions in the stderr.
155+
* This method is deprecated and it is not recommended to use it in new projects.
135156
*
157+
* @deprecated 1.3.0
158+
*
136159
* @param bool $useStderr True to enable, false to disable.
137160
*
138-
* @return self
161+
* @return self
162+
*/
163+
public function setUseStderr(bool $useStderr): WaterPipeConfig
164+
{
165+
$this->_errorLogger = $useStderr
166+
? new StdErrorLogger()
167+
: new DefaultErrorLogger();
168+
169+
return $this;
170+
}
171+
172+
/**
173+
* Gets the error logger currently used by WaterPipe.
174+
*
175+
* @return IErrorLogger
176+
*/
177+
public function errorLogger(): IErrorLogger
178+
{
179+
return $this->_errorLogger;
180+
}
181+
182+
/**
183+
* Defines the error logger to use in WaterPipe.
184+
*
185+
* @param IErrorLogger $errorLogger The error logger.
186+
*
187+
* @return self
139188
*/
140-
public function setUseStderr(bool $useStderr)
189+
public function setErrorLogger(IErrorLogger $errorLogger): WaterPipeConfig
141190
{
142-
$this->_useStderr = $useStderr;
191+
$this->_errorLogger = $errorLogger;
143192

144193
return $this;
145194
}

0 commit comments

Comments
 (0)