@@ -60,8 +60,7 @@ discard block |
||
| 60 | 60 | * @method bool hasInfoThatPasses($message) |
| 61 | 61 | * @method bool hasDebugThatPasses($message) |
| 62 | 62 | */ |
| 63 | -class TestLogger extends AbstractLogger |
|
| 64 | -{ |
|
| 63 | +class TestLogger extends AbstractLogger { |
|
| 65 | 64 | /** |
| 66 | 65 | * @var array |
| 67 | 66 | */ |
@@ -72,8 +71,7 @@ discard block |
||
| 72 | 71 | /** |
| 73 | 72 | * @inheritdoc |
| 74 | 73 | */ |
| 75 | - public function log($level, $message, array $context = []) |
|
| 76 | - { |
|
| 74 | + public function log($level, $message, array $context = []) { |
|
| 77 | 75 | $record = [ |
| 78 | 76 | 'level' => $level, |
| 79 | 77 | 'message' => $message, |
@@ -84,13 +82,11 @@ discard block |
||
| 84 | 82 | $this->records[] = $record; |
| 85 | 83 | } |
| 86 | 84 | |
| 87 | - public function hasRecords($level) |
|
| 88 | - { |
|
| 85 | + public function hasRecords($level) { |
|
| 89 | 86 | return isset($this->recordsByLevel[$level]); |
| 90 | 87 | } |
| 91 | 88 | |
| 92 | - public function hasRecord($record, $level) |
|
| 93 | - { |
|
| 89 | + public function hasRecord($record, $level) { |
|
| 94 | 90 | if (is_string($record)) { |
| 95 | 91 | $record = ['message' => $record]; |
| 96 | 92 | } |
@@ -105,22 +101,19 @@ discard block |
||
| 105 | 101 | }, $level); |
| 106 | 102 | } |
| 107 | 103 | |
| 108 | - public function hasRecordThatContains($message, $level) |
|
| 109 | - { |
|
| 104 | + public function hasRecordThatContains($message, $level) { |
|
| 110 | 105 | return $this->hasRecordThatPasses(function ($rec) use ($message) { |
| 111 | 106 | return strpos($rec['message'], $message) !== false; |
| 112 | 107 | }, $level); |
| 113 | 108 | } |
| 114 | 109 | |
| 115 | - public function hasRecordThatMatches($regex, $level) |
|
| 116 | - { |
|
| 110 | + public function hasRecordThatMatches($regex, $level) { |
|
| 117 | 111 | return $this->hasRecordThatPasses(function ($rec) use ($regex) { |
| 118 | 112 | return preg_match($regex, $rec['message']) > 0; |
| 119 | 113 | }, $level); |
| 120 | 114 | } |
| 121 | 115 | |
| 122 | - public function hasRecordThatPasses(callable $predicate, $level) |
|
| 123 | - { |
|
| 116 | + public function hasRecordThatPasses(callable $predicate, $level) { |
|
| 124 | 117 | if (!isset($this->recordsByLevel[$level])) { |
| 125 | 118 | return false; |
| 126 | 119 | } |
@@ -132,8 +125,7 @@ discard block |
||
| 132 | 125 | return false; |
| 133 | 126 | } |
| 134 | 127 | |
| 135 | - public function __call($method, $args) |
|
| 136 | - { |
|
| 128 | + public function __call($method, $args) { |
|
| 137 | 129 | if (preg_match('/(.*)(Debug|Info|Notice|Warning|Error|Critical|Alert|Emergency)(.*)/', $method, $matches) > 0) { |
| 138 | 130 | $genericMethod = $matches[1] . ('Records' !== $matches[3] ? 'Record' : '') . $matches[3]; |
| 139 | 131 | $level = strtolower($matches[2]); |
@@ -145,8 +137,7 @@ discard block |
||
| 145 | 137 | throw new \BadMethodCallException('Call to undefined method ' . get_class($this) . '::' . $method . '()'); |
| 146 | 138 | } |
| 147 | 139 | |
| 148 | - public function reset() |
|
| 149 | - { |
|
| 140 | + public function reset() { |
|
| 150 | 141 | $this->records = []; |
| 151 | 142 | $this->recordsByLevel = []; |
| 152 | 143 | } |
@@ -18,8 +18,7 @@ discard block |
||
| 18 | 18 | * Implementors can extend the class and implement abstract methods to run this |
| 19 | 19 | * as part of their test suite. |
| 20 | 20 | */ |
| 21 | -abstract class LoggerInterfaceTest extends TestCase |
|
| 22 | -{ |
|
| 21 | +abstract class LoggerInterfaceTest extends TestCase { |
|
| 23 | 22 | /** |
| 24 | 23 | * @return LoggerInterface |
| 25 | 24 | */ |
@@ -36,16 +35,14 @@ discard block |
||
| 36 | 35 | */ |
| 37 | 36 | abstract public function getLogs(); |
| 38 | 37 | |
| 39 | - public function testImplements() |
|
| 40 | - { |
|
| 38 | + public function testImplements() { |
|
| 41 | 39 | $this->assertInstanceOf('GravityView\Psr\Log\LoggerInterface', $this->getLogger()); |
| 42 | 40 | } |
| 43 | 41 | |
| 44 | 42 | /** |
| 45 | 43 | * @dataProvider provideLevelsAndMessages |
| 46 | 44 | */ |
| 47 | - public function testLogsAtAllLevels($level, $message) |
|
| 48 | - { |
|
| 45 | + public function testLogsAtAllLevels($level, $message) { |
|
| 49 | 46 | $logger = $this->getLogger(); |
| 50 | 47 | $logger->{$level}($message, array('user' => 'Bob')); |
| 51 | 48 | $logger->log($level, $message, array('user' => 'Bob')); |
@@ -57,8 +54,7 @@ discard block |
||
| 57 | 54 | $this->assertEquals($expected, $this->getLogs()); |
| 58 | 55 | } |
| 59 | 56 | |
| 60 | - public function provideLevelsAndMessages() |
|
| 61 | - { |
|
| 57 | + public function provideLevelsAndMessages() { |
|
| 62 | 58 | return array( |
| 63 | 59 | LogLevel::EMERGENCY => array(LogLevel::EMERGENCY, 'message of level emergency with context: {user}'), |
| 64 | 60 | LogLevel::ALERT => array(LogLevel::ALERT, 'message of level alert with context: {user}'), |
@@ -74,14 +70,12 @@ discard block |
||
| 74 | 70 | /** |
| 75 | 71 | * @expectedException \GravityView\Psr\Log\InvalidArgumentException |
| 76 | 72 | */ |
| 77 | - public function testThrowsOnInvalidLevel() |
|
| 78 | - { |
|
| 73 | + public function testThrowsOnInvalidLevel() { |
|
| 79 | 74 | $logger = $this->getLogger(); |
| 80 | 75 | $logger->log('invalid level', 'Foo'); |
| 81 | 76 | } |
| 82 | 77 | |
| 83 | - public function testContextReplacement() |
|
| 84 | - { |
|
| 78 | + public function testContextReplacement() { |
|
| 85 | 79 | $logger = $this->getLogger(); |
| 86 | 80 | $logger->info('{Message {nothing} {user} {foo.bar} a}', array('user' => 'Bob', 'foo.bar' => 'Bar')); |
| 87 | 81 | |
@@ -89,8 +83,7 @@ discard block |
||
| 89 | 83 | $this->assertEquals($expected, $this->getLogs()); |
| 90 | 84 | } |
| 91 | 85 | |
| 92 | - public function testObjectCastToString() |
|
| 93 | - { |
|
| 86 | + public function testObjectCastToString() { |
|
| 94 | 87 | if (method_exists($this, 'createPartialMock')) { |
| 95 | 88 | $dummy = $this->createPartialMock('GravityView\GravityView\Psr\Log\Test\DummyTest', array('__toString')); |
| 96 | 89 | } else { |
@@ -106,8 +99,7 @@ discard block |
||
| 106 | 99 | $this->assertEquals($expected, $this->getLogs()); |
| 107 | 100 | } |
| 108 | 101 | |
| 109 | - public function testContextCanContainAnything() |
|
| 110 | - { |
|
| 102 | + public function testContextCanContainAnything() { |
|
| 111 | 103 | $closed = fopen('php://memory', 'r'); |
| 112 | 104 | fclose($closed); |
| 113 | 105 | |
@@ -129,8 +121,7 @@ discard block |
||
| 129 | 121 | $this->assertEquals($expected, $this->getLogs()); |
| 130 | 122 | } |
| 131 | 123 | |
| 132 | - public function testContextExceptionKeyCanBeExceptionOrOtherValues() |
|
| 133 | - { |
|
| 124 | + public function testContextExceptionKeyCanBeExceptionOrOtherValues() { |
|
| 134 | 125 | $logger = $this->getLogger(); |
| 135 | 126 | $logger->warning('Random message', array('exception' => 'oops')); |
| 136 | 127 | $logger->critical('Uncaught Exception!', array('exception' => new \LogicException('Fail'))); |
@@ -15,10 +15,8 @@ |
||
| 15 | 15 | * |
| 16 | 16 | * @internal |
| 17 | 17 | */ |
| 18 | -class DummyTest |
|
| 19 | -{ |
|
| 20 | - public function __toString() |
|
| 21 | - { |
|
| 18 | +class DummyTest { |
|
| 19 | + public function __toString() { |
|
| 22 | 20 | return 'DummyTest'; |
| 23 | 21 | } |
| 24 | 22 | } |
@@ -23,8 +23,7 @@ |
||
| 23 | 23 | * See https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-3-logger-interface.md |
| 24 | 24 | * for the full interface specification. |
| 25 | 25 | */ |
| 26 | -interface LoggerInterface |
|
| 27 | -{ |
|
| 26 | +interface LoggerInterface { |
|
| 28 | 27 | /** |
| 29 | 28 | * System is unusable. |
| 30 | 29 | * |
@@ -32,8 +32,7 @@ discard block |
||
| 32 | 32 | /** |
| 33 | 33 | * Class documentation |
| 34 | 34 | */ |
| 35 | -class Logger extends AbstractLogger |
|
| 36 | -{
|
|
| 35 | +class Logger extends AbstractLogger { |
|
| 37 | 36 | /** |
| 38 | 37 | * KLogger options |
| 39 | 38 | * Anything options not considered 'core' to the logging library should be |
@@ -115,28 +114,27 @@ discard block |
||
| 115 | 114 | * @internal param string $logFilePrefix The prefix for the log file name |
| 116 | 115 | * @internal param string $logFileExt The extension for the log file |
| 117 | 116 | */ |
| 118 | - public function __construct($logDirectory, $logLevelThreshold = LogLevel::DEBUG, array $options = array()) |
|
| 119 | - {
|
|
| 117 | + public function __construct($logDirectory, $logLevelThreshold = LogLevel::DEBUG, array $options = array()) { |
|
| 120 | 118 | $this->logLevelThreshold = $logLevelThreshold; |
| 121 | 119 | $this->options = array_merge($this->options, $options); |
| 122 | 120 | |
| 123 | 121 | $logDirectory = rtrim($logDirectory, DIRECTORY_SEPARATOR); |
| 124 | - if ( ! file_exists($logDirectory)) {
|
|
| 122 | + if ( ! file_exists($logDirectory)) { |
|
| 125 | 123 | mkdir($logDirectory, $this->defaultPermissions, true); |
| 126 | 124 | } |
| 127 | 125 | |
| 128 | - if(strpos($logDirectory, 'php://') === 0) {
|
|
| 126 | + if(strpos($logDirectory, 'php://') === 0) { |
|
| 129 | 127 | $this->setLogToStdOut($logDirectory); |
| 130 | 128 | $this->setFileHandle('w+');
|
| 131 | - } else {
|
|
| 129 | + } else { |
|
| 132 | 130 | $this->setLogFilePath($logDirectory); |
| 133 | - if(file_exists($this->logFilePath) && !is_writable($this->logFilePath)) {
|
|
| 131 | + if(file_exists($this->logFilePath) && !is_writable($this->logFilePath)) { |
|
| 134 | 132 | throw new RuntimeException('The file could not be written to. Check that appropriate permissions have been set.');
|
| 135 | 133 | } |
| 136 | 134 | $this->setFileHandle('a');
|
| 137 | 135 | } |
| 138 | 136 | |
| 139 | - if ( ! $this->fileHandle) {
|
|
| 137 | + if ( ! $this->fileHandle) { |
|
| 140 | 138 | throw new RuntimeException('The file could not be opened. Check permissions.');
|
| 141 | 139 | } |
| 142 | 140 | } |
@@ -144,22 +142,21 @@ discard block |
||
| 144 | 142 | /** |
| 145 | 143 | * @param string $stdOutPath |
| 146 | 144 | */ |
| 147 | - public function setLogToStdOut($stdOutPath) {
|
|
| 145 | + public function setLogToStdOut($stdOutPath) { |
|
| 148 | 146 | $this->logFilePath = $stdOutPath; |
| 149 | 147 | } |
| 150 | 148 | |
| 151 | 149 | /** |
| 152 | 150 | * @param string $logDirectory |
| 153 | 151 | */ |
| 154 | - public function setLogFilePath($logDirectory) {
|
|
| 155 | - if ($this->options['filename']) {
|
|
| 156 | - if (strpos($this->options['filename'], '.log') !== false || strpos($this->options['filename'], '.txt') !== false) {
|
|
| 152 | + public function setLogFilePath($logDirectory) { |
|
| 153 | + if ($this->options['filename']) { |
|
| 154 | + if (strpos($this->options['filename'], '.log') !== false || strpos($this->options['filename'], '.txt') !== false) { |
|
| 157 | 155 | $this->logFilePath = $logDirectory.DIRECTORY_SEPARATOR.$this->options['filename']; |
| 158 | - } |
|
| 159 | - else {
|
|
| 156 | + } else { |
|
| 160 | 157 | $this->logFilePath = $logDirectory.DIRECTORY_SEPARATOR.$this->options['filename'].'.'.$this->options['extension']; |
| 161 | 158 | } |
| 162 | - } else {
|
|
| 159 | + } else { |
|
| 163 | 160 | $this->logFilePath = $logDirectory.DIRECTORY_SEPARATOR.$this->options['prefix'].date('Y-m-d').'.'.$this->options['extension'];
|
| 164 | 161 | } |
| 165 | 162 | } |
@@ -169,7 +166,7 @@ discard block |
||
| 169 | 166 | * |
| 170 | 167 | * @internal param resource $fileHandle |
| 171 | 168 | */ |
| 172 | - public function setFileHandle($writeMode) {
|
|
| 169 | + public function setFileHandle($writeMode) { |
|
| 173 | 170 | $this->fileHandle = fopen($this->logFilePath, $writeMode); |
| 174 | 171 | } |
| 175 | 172 | |
@@ -177,9 +174,8 @@ discard block |
||
| 177 | 174 | /** |
| 178 | 175 | * Class destructor |
| 179 | 176 | */ |
| 180 | - public function __destruct() |
|
| 181 | - {
|
|
| 182 | - if ($this->fileHandle) {
|
|
| 177 | + public function __destruct() { |
|
| 178 | + if ($this->fileHandle) { |
|
| 183 | 179 | fclose($this->fileHandle); |
| 184 | 180 | } |
| 185 | 181 | } |
@@ -189,8 +185,7 @@ discard block |
||
| 189 | 185 | * |
| 190 | 186 | * @param string $dateFormat Valid format string for date() |
| 191 | 187 | */ |
| 192 | - public function setDateFormat($dateFormat) |
|
| 193 | - {
|
|
| 188 | + public function setDateFormat($dateFormat) { |
|
| 194 | 189 | $this->options['dateFormat'] = $dateFormat; |
| 195 | 190 | } |
| 196 | 191 | |
@@ -199,8 +194,7 @@ discard block |
||
| 199 | 194 | * |
| 200 | 195 | * @param string $logLevelThreshold The log level threshold |
| 201 | 196 | */ |
| 202 | - public function setLogLevelThreshold($logLevelThreshold) |
|
| 203 | - {
|
|
| 197 | + public function setLogLevelThreshold($logLevelThreshold) { |
|
| 204 | 198 | $this->logLevelThreshold = $logLevelThreshold; |
| 205 | 199 | } |
| 206 | 200 | |
@@ -212,9 +206,8 @@ discard block |
||
| 212 | 206 | * @param array $context |
| 213 | 207 | * @return null |
| 214 | 208 | */ |
| 215 | - public function log($level, $message, array $context = array()) |
|
| 216 | - {
|
|
| 217 | - if ($this->logLevels[$this->logLevelThreshold] < $this->logLevels[$level]) {
|
|
| 209 | + public function log($level, $message, array $context = array()) { |
|
| 210 | + if ($this->logLevels[$this->logLevelThreshold] < $this->logLevels[$level]) { |
|
| 218 | 211 | return; |
| 219 | 212 | } |
| 220 | 213 | $message = $this->formatMessage($level, $message, $context); |
@@ -227,16 +220,15 @@ discard block |
||
| 227 | 220 | * @param string $message Line to write to the log |
| 228 | 221 | * @return void |
| 229 | 222 | */ |
| 230 | - public function write($message) |
|
| 231 | - {
|
|
| 232 | - if (null !== $this->fileHandle) {
|
|
| 233 | - if (fwrite($this->fileHandle, $message) === false) {
|
|
| 223 | + public function write($message) { |
|
| 224 | + if (null !== $this->fileHandle) { |
|
| 225 | + if (fwrite($this->fileHandle, $message) === false) { |
|
| 234 | 226 | throw new RuntimeException('The file could not be written to. Check that appropriate permissions have been set.');
|
| 235 | - } else {
|
|
| 227 | + } else { |
|
| 236 | 228 | $this->lastLine = trim($message); |
| 237 | 229 | $this->logLineCount++; |
| 238 | 230 | |
| 239 | - if ($this->options['flushFrequency'] && $this->logLineCount % $this->options['flushFrequency'] === 0) {
|
|
| 231 | + if ($this->options['flushFrequency'] && $this->logLineCount % $this->options['flushFrequency'] === 0) { |
|
| 240 | 232 | fflush($this->fileHandle); |
| 241 | 233 | } |
| 242 | 234 | } |
@@ -248,8 +240,7 @@ discard block |
||
| 248 | 240 | * |
| 249 | 241 | * @return string |
| 250 | 242 | */ |
| 251 | - public function getLogFilePath() |
|
| 252 | - {
|
|
| 243 | + public function getLogFilePath() { |
|
| 253 | 244 | return $this->logFilePath; |
| 254 | 245 | } |
| 255 | 246 | |
@@ -258,8 +249,7 @@ discard block |
||
| 258 | 249 | * |
| 259 | 250 | * @return string |
| 260 | 251 | */ |
| 261 | - public function getLastLogLine() |
|
| 262 | - {
|
|
| 252 | + public function getLastLogLine() { |
|
| 263 | 253 | return $this->lastLine; |
| 264 | 254 | } |
| 265 | 255 | |
@@ -271,9 +261,8 @@ discard block |
||
| 271 | 261 | * @param array $context The context |
| 272 | 262 | * @return string |
| 273 | 263 | */ |
| 274 | - protected function formatMessage($level, $message, $context) |
|
| 275 | - {
|
|
| 276 | - if ($this->options['logFormat']) {
|
|
| 264 | + protected function formatMessage($level, $message, $context) { |
|
| 265 | + if ($this->options['logFormat']) { |
|
| 277 | 266 | $parts = array( |
| 278 | 267 | 'date' => $this->getTimestamp(), |
| 279 | 268 | 'level' => strtoupper($level), |
@@ -283,15 +272,15 @@ discard block |
||
| 283 | 272 | 'context' => json_encode($context), |
| 284 | 273 | ); |
| 285 | 274 | $message = $this->options['logFormat']; |
| 286 | - foreach ($parts as $part => $value) {
|
|
| 275 | + foreach ($parts as $part => $value) { |
|
| 287 | 276 | $message = str_replace('{'.$part.'}', $value, $message);
|
| 288 | 277 | } |
| 289 | 278 | |
| 290 | - } else {
|
|
| 279 | + } else { |
|
| 291 | 280 | $message = "[{$this->getTimestamp()}] [{$level}] {$message}";
|
| 292 | 281 | } |
| 293 | 282 | |
| 294 | - if ($this->options['appendContext'] && ! empty($context)) {
|
|
| 283 | + if ($this->options['appendContext'] && ! empty($context)) { |
|
| 295 | 284 | $message .= PHP_EOL.$this->indent($this->contextToString($context)); |
| 296 | 285 | } |
| 297 | 286 | |
@@ -307,8 +296,7 @@ discard block |
||
| 307 | 296 | * |
| 308 | 297 | * @return string |
| 309 | 298 | */ |
| 310 | - private function getTimestamp() |
|
| 311 | - {
|
|
| 299 | + private function getTimestamp() { |
|
| 312 | 300 | $originalTime = microtime(true); |
| 313 | 301 | $micro = sprintf("%06d", ($originalTime - floor($originalTime)) * 1000000);
|
| 314 | 302 | $date = new DateTime(date('Y-m-d H:i:s.'.$micro, $originalTime));
|
@@ -322,10 +310,9 @@ discard block |
||
| 322 | 310 | * @param array $context The Context |
| 323 | 311 | * @return string |
| 324 | 312 | */ |
| 325 | - protected function contextToString($context) |
|
| 326 | - {
|
|
| 313 | + protected function contextToString($context) { |
|
| 327 | 314 | $export = ''; |
| 328 | - foreach ($context as $key => $value) {
|
|
| 315 | + foreach ($context as $key => $value) { |
|
| 329 | 316 | $export .= "{$key}: ";
|
| 330 | 317 | $export .= preg_replace(array( |
| 331 | 318 | '/=>\s+([a-zA-Z])/im', |
@@ -348,8 +335,7 @@ discard block |
||
| 348 | 335 | * @param string $indent What to use as the indent. |
| 349 | 336 | * @return string |
| 350 | 337 | */ |
| 351 | - protected function indent($string, $indent = ' ') |
|
| 352 | - {
|
|
| 338 | + protected function indent($string, $indent = ' ') { |
|
| 353 | 339 | return $indent.str_replace("\n", "\n".$indent, $string);
|
| 354 | 340 | } |
| 355 | 341 | } |
@@ -45,8 +45,7 @@ |
||
| 45 | 45 | * |
| 46 | 46 | * @return string |
| 47 | 47 | */ |
| 48 | - function random_bytes($bytes) |
|
| 49 | - { |
|
| 48 | + function random_bytes($bytes) { |
|
| 50 | 49 | /** @var resource $fp */ |
| 51 | 50 | static $fp = null; |
| 52 | 51 | |
@@ -38,8 +38,7 @@ |
||
| 38 | 38 | * |
| 39 | 39 | * @return int |
| 40 | 40 | */ |
| 41 | - function random_int($min, $max) |
|
| 42 | - { |
|
| 41 | + function random_int($min, $max) { |
|
| 43 | 42 | /** |
| 44 | 43 | * Type and input logic checks |
| 45 | 44 | * |
@@ -40,8 +40,7 @@ |
||
| 40 | 40 | * |
| 41 | 41 | * @return string |
| 42 | 42 | */ |
| 43 | - function random_bytes($bytes) |
|
| 44 | - { |
|
| 43 | + function random_bytes($bytes) { |
|
| 45 | 44 | try { |
| 46 | 45 | /** @var int $bytes */ |
| 47 | 46 | $bytes = RandomCompat_intval($bytes); |
@@ -45,8 +45,7 @@ |
||
| 45 | 45 | * |
| 46 | 46 | * @throws TypeError |
| 47 | 47 | */ |
| 48 | - function RandomCompat_intval($number, $fail_open = false) |
|
| 49 | - { |
|
| 48 | + function RandomCompat_intval($number, $fail_open = false) { |
|
| 50 | 49 | if (is_int($number) || is_float($number)) { |
| 51 | 50 | $number += 0; |
| 52 | 51 | } elseif (is_numeric($number)) { |