Skip to content

Commit

Permalink
Add CsvHandler
Browse files Browse the repository at this point in the history
  • Loading branch information
jaymoulin committed Nov 14, 2016
0 parents commit f90803c
Show file tree
Hide file tree
Showing 9 changed files with 247 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.gitattributes export-ignore
.gitignore export-ignore
.travis.yml export-ignore
6 changes: 6 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
vendor
composer.phar
phpunit.xml
composer.lock
.DS_Store
.php_cs.cache
16 changes: 16 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -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
19 changes: 19 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -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.
51 changes: 51 additions & 0 deletions README.mdown
Original file line number Diff line number Diff line change
@@ -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
<?php

use Monolog\Logger;
use FemtoPixel\Monolog\Handler\CsvHandler;

// create a log channel
$log = new Logger('name');
$log->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 <jaymoulin@gmail.com>
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
33 changes: 33 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -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"
}
}
19 changes: 19 additions & 0 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?xml version="1.0" encoding="UTF-8"?>

<phpunit bootstrap="vendor/autoload.php" colors="true">
<testsuites>
<testsuite name="Monolog Test Suite">
<directory>tests/Monolog/</directory>
</testsuite>
</testsuites>

<filter>
<whitelist>
<directory suffix=".php">src/Monolog/</directory>
</whitelist>
</filter>

<php>
<ini name="date.timezone" value="UTC"/>
</php>
</phpunit>
45 changes: 45 additions & 0 deletions src/Monolog/Handler/CsvHandler.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php

/*
* This file is part of the Monolog package.
*
* (c) Jordi Boggiano <j.boggiano@seld.be>
*
* 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 <jaymoulin@gmail.com>
*/
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();
}
}
55 changes: 55 additions & 0 deletions tests/Monolog/Handler/CsvHandlerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?php

/*
* This file is part of the Monolog package.
*
* (c) Jordi Boggiano <j.boggiano@seld.be>
*
* 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);
}
}

0 comments on commit f90803c

Please sign in to comment.