Skip to content

Commit

Permalink
update documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
jaymoulin committed Nov 16, 2016
1 parent f90803c commit d01ac97
Show file tree
Hide file tree
Showing 7 changed files with 94 additions and 20 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
vendor
build
composer.phar
phpunit.xml
composer.lock
.DS_Store
.php_cs.cache
4 changes: 1 addition & 3 deletions README.mdown
Original file line number Diff line number Diff line change
@@ -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

Expand All @@ -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');
Expand Down
8 changes: 6 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down
13 changes: 10 additions & 3 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,24 @@
<phpunit bootstrap="vendor/autoload.php" colors="true">
<testsuites>
<testsuite name="Monolog Test Suite">
<directory>tests/Monolog/</directory>
<directory>tests</directory>
<directory>tests/*</directory>
</testsuite>
</testsuites>

<logging>
<log type="coverage-html" target="build/coverage/report" lowUpperBound="35" highLowerBound="70"/>
<log type="junit" target="build/logs/junit.xml" />
<log type="coverage-clover" target="build/logs/clover.xml"/>
</logging>

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

<php>
<ini name="date.timezone" value="UTC"/>
</php>
</phpunit>
</phpunit>
15 changes: 13 additions & 2 deletions src/Monolog/Handler/CsvHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

/**
Expand Down
12 changes: 3 additions & 9 deletions tests/Monolog/Handler/CsvHandlerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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+');
Expand All @@ -31,17 +28,14 @@ public function testWrite()
$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~";
$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)));
}

Expand Down
60 changes: 60 additions & 0 deletions tests/Monolog/TestCase.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?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;

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;
}
}

0 comments on commit d01ac97

Please sign in to comment.