From f90803cc08a50ce5232fc90d79a1c673b3b90329 Mon Sep 17 00:00:00 2001 From: Jay MOULIN Date: Tue, 15 Nov 2016 00:23:40 +0100 Subject: [PATCH] Add CsvHandler --- .gitattributes | 3 ++ .gitignore | 6 +++ .travis.yml | 16 +++++++ LICENSE | 19 ++++++++ README.mdown | 51 ++++++++++++++++++++++ composer.json | 33 ++++++++++++++ phpunit.xml.dist | 19 ++++++++ src/Monolog/Handler/CsvHandler.php | 45 +++++++++++++++++++ tests/Monolog/Handler/CsvHandlerTest.php | 55 ++++++++++++++++++++++++ 9 files changed, 247 insertions(+) create mode 100644 .gitattributes create mode 100644 .gitignore create mode 100644 .travis.yml create mode 100644 LICENSE create mode 100644 README.mdown create mode 100644 composer.json create mode 100644 phpunit.xml.dist create mode 100644 src/Monolog/Handler/CsvHandler.php create mode 100644 tests/Monolog/Handler/CsvHandlerTest.php diff --git a/.gitattributes b/.gitattributes new file mode 100644 index 0000000..4afe792 --- /dev/null +++ b/.gitattributes @@ -0,0 +1,3 @@ +.gitattributes export-ignore +.gitignore export-ignore +.travis.yml export-ignore diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..22486e9 --- /dev/null +++ b/.gitignore @@ -0,0 +1,6 @@ +vendor +composer.phar +phpunit.xml +composer.lock +.DS_Store +.php_cs.cache diff --git a/.travis.yml b/.travis.yml new file mode 100644 index 0000000..1262f25 --- /dev/null +++ b/.travis.yml @@ -0,0 +1,16 @@ +language: php + +sudo: false + +matrix: + include: + - php: 5.3 + - php: 5.4 + - php: 5.5 + - php: 5.6 + - php: 7 + - php: hhvm + fast_finish: true + +before_script: composer install +script: composer phpunit diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..756d196 --- /dev/null +++ b/LICENSE @@ -0,0 +1,19 @@ +Copyright (c) 2011-2016 Jay MOULIN + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is furnished +to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +THE SOFTWARE. diff --git a/README.mdown b/README.mdown new file mode 100644 index 0000000..3862921 --- /dev/null +++ b/README.mdown @@ -0,0 +1,51 @@ +# 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 + +## Installation + +Install the latest version with + +```bash +$ composer require femtopixel/monolog-csvhandler +``` + +## Basic Usage + +```php +pushHandler(new SCsvHandler('path/to/your.csv', Logger::WARNING)); + +// add records to the log +$log->addWarning('Foo'); +$log->addError('Bar'); +``` + +## About + +### Requirements + +- Monolog works with PHP 5.3 or above, and is also tested to work with HHVM. + +### Submitting bugs and feature requests + +Bugs and feature request are tracked on [GitHub](https://github.com/fmetopixel/monolog-csvhandler/issues) + +### Author + +Jay MOULIN +See also the list of [contributors](https://github.com/femtopixel/monolog-csvhandler/contributors) which participated in this handler. + +### License + +Monolog is licensed under the MIT License and so for this CsvHandler - see the `LICENSE` file for details diff --git a/composer.json b/composer.json new file mode 100644 index 0000000..63193d5 --- /dev/null +++ b/composer.json @@ -0,0 +1,33 @@ +{ + "name": "femtopixel/monolog-csvhandler", + "description": "Sends your logs to csv files", + "keywords": ["log", "csv", "logging", "psr-3"], + "homepage": "http://github.com/femtopixel/monolog-csvhandler", + "type": "library", + "license": "MIT", + "authors": [ + { + "name": "Jay MOULIN", + "email": "jaymoulin@gmail.com", + "homepage": "http://github.com/jaymoulin" + } + ], + "require": { + "php": ">=5.3.0", + "monolog/monolog": "^1.21.0", + "psr/log": "~1.0" + }, + "require-dev": { + "phpunit/phpunit": "~4.5 || ^5.0" + }, + "_": "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": { + "psr-4": {"FemtoPixel\\Monolog\\": "src/Monolog"} + }, + "autoload-dev": { + "psr-4": {"FemtoPixel\\Monolog\\": "tests/Monolog"} + }, + "scripts": { + "phpunit": "phpunit" + } +} diff --git a/phpunit.xml.dist b/phpunit.xml.dist new file mode 100644 index 0000000..20d82b6 --- /dev/null +++ b/phpunit.xml.dist @@ -0,0 +1,19 @@ + + + + + + tests/Monolog/ + + + + + + src/Monolog/ + + + + + + + diff --git a/src/Monolog/Handler/CsvHandler.php b/src/Monolog/Handler/CsvHandler.php new file mode 100644 index 0000000..056f77d --- /dev/null +++ b/src/Monolog/Handler/CsvHandler.php @@ -0,0 +1,45 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace FemtoPixel\Monolog\Handler; + +use Monolog\Formatter\NormalizerFormatter; +use Monolog\Handler\StreamHandler; + +/** + * Stores to a csv file + * + * Can be used to store big loads to physical files and import them later into another system that can handle CSV + * + * @author Jay MOULIN + */ +class CsvHandler extends StreamHandler +{ + const DELIMITER = ','; + const ENCLOSURE = '\''; + const ESCAPE_CHAR = '\\'; + + /** + * @inheritdoc + */ + protected function streamWrite($resource, $record) + { + return fputcsv($resource, (array)$record['formatted'], static::DELIMITER, static::ENCLOSURE, static::ESCAPE_CHAR); + } + + /** + * @inheritdoc + */ + protected function getDefaultFormatter() + { + return new NormalizerFormatter(); + } +} diff --git a/tests/Monolog/Handler/CsvHandlerTest.php b/tests/Monolog/Handler/CsvHandlerTest.php new file mode 100644 index 0000000..3f291ad --- /dev/null +++ b/tests/Monolog/Handler/CsvHandlerTest.php @@ -0,0 +1,55 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Femtopixel\Monolog\Handler; + +use Monolog\TestCase; +use Monolog\Logger; + +class CsvHandlerTest extends TestCase +{ + /** + * @covers CsvHandler::write + */ + public function testWrite() + { + $handle = fopen('php://memory', 'a+'); + $handler = new CsvHandler($handle); + $handler->setFormatter($this->getIdentityFormatter()); + $handler->handle($this->getRecord(Logger::WARNING, 'test')); + $handler->handle($this->getRecord(Logger::WARNING, 'test2')); + $handler->handle($this->getRecord(Logger::WARNING, 'test3')); + fseek($handle, 0); + $this->assertEquals("test\ntest2\ntest3\n", fread($handle, 100)); + } + + /** + * @covers CsvHandler::write + */ + public function testWriteWithNormalizer() + { + $handle = fopen('php://memory', 'a+'); + $handler = new CsvHandler($handle); + $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~"; + $this->assertSame(1, preg_match($regexp, fread($handle, 100))); + } + + /** + * @return \Monolog\Formatter\NormalizerFormatter + */ + protected function getNormalizeFormatter() + { + return $this->getMock('Monolog\\Formatter\\NormalizerFormatter', null); + } +}