From d01ac9752f324d0d839a2af94966e070d08184c8 Mon Sep 17 00:00:00 2001 From: Jay MOULIN Date: Tue, 15 Nov 2016 00:25:36 +0100 Subject: [PATCH] update documentation --- .gitignore | 2 +- README.mdown | 4 +- composer.json | 8 +++- phpunit.xml.dist | 13 +++-- src/Monolog/Handler/CsvHandler.php | 15 +++++- tests/Monolog/Handler/CsvHandlerTest.php | 12 ++--- tests/Monolog/TestCase.php | 60 ++++++++++++++++++++++++ 7 files changed, 94 insertions(+), 20 deletions(-) create mode 100644 tests/Monolog/TestCase.php diff --git a/.gitignore b/.gitignore index 22486e9..f77cac7 100644 --- a/.gitignore +++ b/.gitignore @@ -1,6 +1,6 @@ vendor +build composer.phar phpunit.xml composer.lock .DS_Store -.php_cs.cache diff --git a/README.mdown b/README.mdown index 3862921..f3edb8e 100644 --- a/README.mdown +++ b/README.mdown @@ -1,8 +1,6 @@ # CsvHandler for Monolog - Logging for PHP [![Build Status](https://img.shields.io/travis/femtopixel/monolog-csvhandler.svg)](https://travis-ci.org/femtopixel/monolog-csvhandler) -[![Total Downloads](https://img.shields.io/packagist/dt/femtopixel/monolog-csvhandler.svg)](https://packagist.org/packages/femtopixel/monolog-csvhandler) [![Latest Stable Version](https://img.shields.io/packagist/v/femtopixel/monolog-csvhandler.svg)](https://packagist.org/packages/femtopixel/monolog-csvhandler) -[![Reference Status](https://www.versioneye.com/php/monolog:monolog/reference_badge.svg)](https://www.versioneye.com/php/monolog:monolog/references) CsvHandler for Monolog sends your logs to CSV files. For more information on Monolog, see http://github.com/Seldaek/monolog @@ -24,7 +22,7 @@ use FemtoPixel\Monolog\Handler\CsvHandler; // create a log channel $log = new Logger('name'); -$log->pushHandler(new SCsvHandler('path/to/your.csv', Logger::WARNING)); +$log->pushHandler(new CsvHandler('path/to/your.csv', Logger::WARNING)); // add records to the log $log->addWarning('Foo'); diff --git a/composer.json b/composer.json index 63193d5..8d3410d 100644 --- a/composer.json +++ b/composer.json @@ -12,13 +12,17 @@ "homepage": "http://github.com/jaymoulin" } ], + "support": { + "issues": "http://github.com/femtopixel/monolog-csvhandler/issues", + "source": "https://github.com/femtopixel/monolog-csvhandler" + }, "require": { "php": ">=5.3.0", - "monolog/monolog": "^1.21.0", + "monolog/monolog": "1.x-dev", "psr/log": "~1.0" }, "require-dev": { - "phpunit/phpunit": "~4.5 || ^5.0" + "phpunit/phpunit": "~4.5" }, "_": "phpunit/phpunit-mock-objects required in 2.3.0 due to https://github.com/sebastianbergmann/phpunit-mock-objects/issues/223 - needs hhvm 3.8+ on travis", "autoload": { diff --git a/phpunit.xml.dist b/phpunit.xml.dist index 20d82b6..3d23a94 100644 --- a/phpunit.xml.dist +++ b/phpunit.xml.dist @@ -3,17 +3,24 @@ - tests/Monolog/ + tests + tests/* + + + + + + - src/Monolog/ + src - + \ No newline at end of file diff --git a/src/Monolog/Handler/CsvHandler.php b/src/Monolog/Handler/CsvHandler.php index 056f77d..a684080 100644 --- a/src/Monolog/Handler/CsvHandler.php +++ b/src/Monolog/Handler/CsvHandler.php @@ -30,9 +30,20 @@ class CsvHandler extends StreamHandler /** * @inheritdoc */ - protected function streamWrite($resource, $record) + protected function streamWrite($resource, array $record) { - return fputcsv($resource, (array)$record['formatted'], static::DELIMITER, static::ENCLOSURE, static::ESCAPE_CHAR); + if (is_array($record['formatted'])) { + foreach ($record['formatted'] as $key => $info) { + if (is_array($info)) { + $record['formatted'][$key] = json_encode($info); + } + } + } + $formated = (array) $record['formatted']; + if (version_compare(PHP_VERSION, '5.5.4', '>=')) { + return fputcsv($resource, $formated, static::DELIMITER, static::ENCLOSURE, static::ESCAPE_CHAR); + } + return fputcsv($resource, $formated, static::DELIMITER, static::ENCLOSURE); } /** diff --git a/tests/Monolog/Handler/CsvHandlerTest.php b/tests/Monolog/Handler/CsvHandlerTest.php index 3f291ad..8f7bd16 100644 --- a/tests/Monolog/Handler/CsvHandlerTest.php +++ b/tests/Monolog/Handler/CsvHandlerTest.php @@ -9,16 +9,13 @@ * file that was distributed with this source code. */ -namespace Femtopixel\Monolog\Handler; +namespace FemtoPixel\Monolog\Handler; -use Monolog\TestCase; +use FemtoPixel\Monolog\TestCase; use Monolog\Logger; class CsvHandlerTest extends TestCase { - /** - * @covers CsvHandler::write - */ public function testWrite() { $handle = fopen('php://memory', 'a+'); @@ -31,9 +28,6 @@ public function testWrite() $this->assertEquals("test\ntest2\ntest3\n", fread($handle, 100)); } - /** - * @covers CsvHandler::write - */ public function testWriteWithNormalizer() { $handle = fopen('php://memory', 'a+'); @@ -41,7 +35,7 @@ public function testWriteWithNormalizer() $handler->setFormatter($this->getNormalizeFormatter()); $handler->handle($this->getRecord(Logger::WARNING, 'doesn\'t fail')); fseek($handle, 0); - $regexp = "~\\A'doesn''t fail',Array,300,WARNING,test,'[0-9]{4}\\-[0-9]{2}+\\-[0-9]{2} [0-9]{2}:[0-9]{2}:[0-9]{2}',Array\n\\Z~"; + $regexp = "~\\A'doesn''t fail',\\[\\],300,WARNING,test,'[0-9]{4}\\-[0-9]{2}+\\-[0-9]{2} [0-9]{2}:[0-9]{2}:[0-9]{2}',\\[\\]\n\\Z~"; $this->assertSame(1, preg_match($regexp, fread($handle, 100))); } diff --git a/tests/Monolog/TestCase.php b/tests/Monolog/TestCase.php new file mode 100644 index 0000000..939ef02 --- /dev/null +++ b/tests/Monolog/TestCase.php @@ -0,0 +1,60 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace FemtoPixel\Monolog; + +use Monolog\Logger; + +class TestCase extends \PHPUnit_Framework_TestCase +{ + /** + * @return array Record + */ + protected function getRecord($level = Logger::WARNING, $message = 'test', $context = array()) + { + return array( + 'message' => $message, + 'context' => $context, + 'level' => $level, + 'level_name' => Logger::getLevelName($level), + 'channel' => 'test', + 'datetime' => \DateTime::createFromFormat('U.u', sprintf('%.6F', microtime(true))), + 'extra' => array(), + ); + } + + /** + * @return array + */ + protected function getMultipleRecords() + { + return array( + $this->getRecord(Logger::DEBUG, 'debug message 1'), + $this->getRecord(Logger::DEBUG, 'debug message 2'), + $this->getRecord(Logger::INFO, 'information'), + $this->getRecord(Logger::WARNING, 'warning'), + $this->getRecord(Logger::ERROR, 'error'), + ); + } + + /** + * @return \Monolog\Formatter\FormatterInterface + */ + protected function getIdentityFormatter() + { + $formatter = $this->getMock('Monolog\\Formatter\\FormatterInterface'); + $formatter->expects($this->any()) + ->method('format') + ->will($this->returnCallback(function ($record) { return $record['message']; })); + + return $formatter; + } +}