From d71fbb5b5144561abfda1f87d3c3f4cbf2d31698 Mon Sep 17 00:00:00 2001 From: James Harris Date: Wed, 19 Nov 2014 09:18:43 +1000 Subject: [PATCH 1/2] Improved readability of output. * Dates are now at the beginning of the line * The log level is output as a fixed-width (4 character) code --- .php_cs | 15 ++++++++++++ src/Logger.php | 23 ++++++++++++++----- src/PackageInfo.php | 2 +- test/suite/LoggerTest.php | 34 +++++++++++++++++++++------- test/suite/ParentLoggerTraitTest.php | 2 +- test/suite/SubLoggerTest.php | 4 ++-- 6 files changed, 62 insertions(+), 18 deletions(-) create mode 100644 .php_cs diff --git a/.php_cs b/.php_cs new file mode 100644 index 0000000..27b65a5 --- /dev/null +++ b/.php_cs @@ -0,0 +1,15 @@ +in(__DIR__); + +return Symfony\CS\Config\Config::create() + ->fixers(array( + '-concat_without_spaces', + '-new_with_braces', + 'align_double_arrow', + 'align_equals', + 'ordered_use', + 'short_array_syntax', + )) + ->finder($finder); diff --git a/src/Logger.php b/src/Logger.php index 9fe062d..8507dd6 100644 --- a/src/Logger.php +++ b/src/Logger.php @@ -2,9 +2,9 @@ namespace Icecave\Stump; use Icecave\Isolator\IsolatorTrait; +use Psr\Log\LogLevel; use Psr\Log\LoggerInterface; use Psr\Log\LoggerTrait; -use Psr\Log\LogLevel; /** * A very simple PSR-3 logger implementation that writes to STDOUT. @@ -28,8 +28,8 @@ public function __construct( $dateFormat = 'Y-m-d H:i:s' ) { $this->minimumLogLevel = self::$levels[$minimumLogLevel]; - $this->dateFormat = $dateFormat; - $this->fileName = $fileName; + $this->dateFormat = $dateFormat; + $this->fileName = $fileName; } /** @@ -60,9 +60,9 @@ public function log($level, $message, array $context = []) ->fwrite( $this->stream, sprintf( - '%s %s: %s' . PHP_EOL, - strtoupper($level), + '%s %s %s' . PHP_EOL, $dateTime, + self::$levelText[$level], $this->substitutePlaceholders( $message, $context @@ -79,7 +79,7 @@ public function log($level, $message, array $context = []) * * @return string The message template with placeholder values substituted. */ - public function substitutePlaceholders($message, array $context) + private function substitutePlaceholders($message, array $context) { if (false === strpos($message, '{')) { return $message; @@ -104,6 +104,17 @@ public function substitutePlaceholders($message, array $context) LogLevel::DEBUG => 0, ]; + private static $levelText = [ + LogLevel::EMERGENCY => 'EMER', + LogLevel::ALERT => 'ALRT', + LogLevel::CRITICAL => 'CRIT', + LogLevel::ERROR => 'ERRO', + LogLevel::WARNING => 'WARN', + LogLevel::NOTICE => 'NOTC', + LogLevel::INFO => 'INFO', + LogLevel::DEBUG => 'DEBG', + ]; + private $minimumLogLevel; private $dateFormat; private $fileName; diff --git a/src/PackageInfo.php b/src/PackageInfo.php index 1d2f8b3..0969ded 100644 --- a/src/PackageInfo.php +++ b/src/PackageInfo.php @@ -3,6 +3,6 @@ class PackageInfo { - const NAME = 'Stump'; + const NAME = 'Stump'; const VERSION = '0.2.0'; } diff --git a/test/suite/LoggerTest.php b/test/suite/LoggerTest.php index d667201..8f1739c 100644 --- a/test/suite/LoggerTest.php +++ b/test/suite/LoggerTest.php @@ -2,8 +2,8 @@ namespace Icecave\Stump; use Icecave\Isolator\Isolator; -use Phake; use PHPUnit_Framework_TestCase; +use Phake; use Psr\Log\LogLevel; class LoggerTest extends PHPUnit_Framework_TestCase @@ -11,7 +11,6 @@ class LoggerTest extends PHPUnit_Framework_TestCase public function setUp() { $this->isolator = Phake::mock('Icecave\Isolator\Isolator'); - $this->minimumLogLevel = LogLevel::INFO; Phake::when($this->isolator) ->fopen(Phake::anyParameters()) @@ -21,16 +20,19 @@ public function setUp() ->date(Phake::anyParameters()) ->thenReturn(''); - $this->logger = new Logger($this->minimumLogLevel); + $this->logger = new Logger; $this->logger->setIsolator($this->isolator); } - public function testLog() + /** + * @dataProvider logTestVectors + */ + public function testLog($logLevel, $logLevelText) { $this->logger->log( - LogLevel::WARNING, - 'Warning message.' + $logLevel, + 'Test message.' ); Phake::verify($this->isolator)->fopen( @@ -40,10 +42,24 @@ public function testLog() Phake::verify($this->isolator)->fwrite( '', - 'WARNING : Warning message.' . PHP_EOL + ' ' . $logLevelText . ' Test message.' . PHP_EOL ); } + public function logTestVectors() + { + return [ + [LogLevel::EMERGENCY, 'EMER'], + [LogLevel::ALERT, 'ALRT'], + [LogLevel::CRITICAL, 'CRIT'], + [LogLevel::ERROR, 'ERRO'], + [LogLevel::WARNING, 'WARN'], + [LogLevel::NOTICE, 'NOTC'], + [LogLevel::INFO, 'INFO'], + [LogLevel::DEBUG, 'DEBG'], + ]; + } + public function testLogWithPlaceholderValues() { $this->logger->log( @@ -57,12 +73,14 @@ public function testLogWithPlaceholderValues() Phake::verify($this->isolator)->fwrite( '', - 'INFO : Foo: FOO, F: F, Missing: {missing}' . PHP_EOL + ' INFO Foo: FOO, F: F, Missing: {missing}' . PHP_EOL ); } public function testLogIgnoresLowLogLevel() { + $this->logger = new Logger(LogLevel::INFO); + $this->logger->debug('This should not be logged.'); Phake::verifyNoInteraction($this->isolator); diff --git a/test/suite/ParentLoggerTraitTest.php b/test/suite/ParentLoggerTraitTest.php index edc52ef..68813c4 100644 --- a/test/suite/ParentLoggerTraitTest.php +++ b/test/suite/ParentLoggerTraitTest.php @@ -1,8 +1,8 @@ Date: Wed, 19 Nov 2014 09:29:26 +1000 Subject: [PATCH 2/2] Bumped version and updated changelog. --- CHANGELOG.md | 7 ++++++- README.md | 2 +- composer.json | 2 +- composer.lock | 12 ++++++------ src/PackageInfo.php | 2 +- 5 files changed, 15 insertions(+), 10 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 0765166..4ace74c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,8 +1,13 @@ # Stump Changelog +### 0.3.0 (2014-11-19) + +* **[IMPROVED]** Improved readability of log output by moving the date to the start of the line and using a fixed-width log level string +* **[BC, FIXED]** `Logger::substitutePlaceholders()` is now (correctly) marked as `private` + ### 0.2.0 (2014-10-25) -* **[BC]** Replaced `PrefixableLoggerInterface` with `ParentLoggerInterface` which allows for chaning of "sub-loggers" +* **[BC]** Replaced `PrefixableLoggerInterface` with `ParentLoggerInterface` which allows for chaining of "sub-loggers" ### 0.1.1 (2014-10-24) diff --git a/README.md b/README.md index 80b38fc..f73d029 100644 --- a/README.md +++ b/README.md @@ -35,4 +35,4 @@ INFO 2014-10-24 16:26:13: It's better than bad... it's good! [Build Status]: http://img.shields.io/travis/IcecaveStudios/stump/develop.svg?style=flat-square [Test Coverage]: http://img.shields.io/coveralls/IcecaveStudios/stump/develop.svg?style=flat-square -[SemVer]: http://img.shields.io/:semver-0.2.0-yellow.svg?style=flat-square +[SemVer]: http://img.shields.io/:semver-0.3.0-yellow.svg?style=flat-square diff --git a/composer.json b/composer.json index 06d339b..bccbe96 100644 --- a/composer.json +++ b/composer.json @@ -33,7 +33,7 @@ }, "extra": { "branch-alias": { - "dev-develop": "0.2.x-dev" + "dev-develop": "0.3.x-dev" } } } diff --git a/composer.lock b/composer.lock index 563cfdf..7039f52 100644 --- a/composer.lock +++ b/composer.lock @@ -4,7 +4,7 @@ "Read more about it at http://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file", "This file is @generated automatically" ], - "hash": "93be09ce81d97c6bddfd89ecd7f48e6b", + "hash": "0a05fd19f9eb57aa19ea04cf0d7462fb", "packages": [ { "name": "icecave/isolator", @@ -257,16 +257,16 @@ }, { "name": "icecave/archer", - "version": "1.3.0", + "version": "1.3.1", "source": { "type": "git", "url": "https://github.com/IcecaveStudios/archer.git", - "reference": "5b1b4748d0fd6a8f977486d15d10e52e68349379" + "reference": "a17abd703a794aefcc56816e4d0a169b29500c66" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/IcecaveStudios/archer/zipball/5b1b4748d0fd6a8f977486d15d10e52e68349379", - "reference": "5b1b4748d0fd6a8f977486d15d10e52e68349379", + "url": "https://api.github.com/repos/IcecaveStudios/archer/zipball/a17abd703a794aefcc56816e4d0a169b29500c66", + "reference": "a17abd703a794aefcc56816e4d0a169b29500c66", "shasum": "" }, "require": { @@ -331,7 +331,7 @@ "testing", "unit" ], - "time": "2014-09-18 03:05:28" + "time": "2014-11-10 21:59:53" }, { "name": "nikic/php-parser", diff --git a/src/PackageInfo.php b/src/PackageInfo.php index 0969ded..20714f2 100644 --- a/src/PackageInfo.php +++ b/src/PackageInfo.php @@ -4,5 +4,5 @@ class PackageInfo { const NAME = 'Stump'; - const VERSION = '0.2.0'; + const VERSION = '0.3.0'; }