Skip to content

Commit

Permalink
Updates to Monolog 2.0 (and PHP 7.2)
Browse files Browse the repository at this point in the history
  • Loading branch information
jaymoulin committed Jan 24, 2020
1 parent b6d5335 commit ae6d1d5
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 67 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ composer.phar
phpunit.xml
composer.lock
.DS_Store
.phpunit.result.cache
13 changes: 7 additions & 6 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,22 @@
"authors": [
{
"name": "Jay MOULIN",
"email": "jaymoulin@gmail.com",
"homepage": "http://github.com/jaymoulin"
"email": "jay@femtopixel.com",
"homepage": "http://github.com/femtopixel"
}
],
"support": {
"issues": "http://github.com/femtopixel/monolog-csvhandler/issues",
"source": "https://github.com/femtopixel/monolog-csvhandler"
},
"require": {
"php": ">=5.3.0",
"monolog/monolog": "^1.22.0",
"psr/log": "~1.0"
"php": ">=7.2.0",
"monolog/monolog": "^2.0",
"psr/log": "~1.0",
"ext-json": "*"
},
"require-dev": {
"phpunit/phpunit": "~4.5"
"phpunit/phpunit": "^8.0"
},
"autoload": {
"psr-4": {"FemtoPixel\\Monolog\\": "src/Monolog"}
Expand Down
28 changes: 12 additions & 16 deletions src/Monolog/Handler/CsvHandler.php
Original file line number Diff line number Diff line change
@@ -1,16 +1,9 @@
<?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\FormatterInterface;
use Monolog\Formatter\LineFormatter;
use Monolog\Formatter\NormalizerFormatter;
use Monolog\Handler\StreamHandler;

Expand All @@ -19,7 +12,7 @@
*
* 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>
* @author Jay MOULIN <jay@femtopixel.com>
*/
class CsvHandler extends StreamHandler
{
Expand All @@ -30,7 +23,7 @@ class CsvHandler extends StreamHandler
/**
* @inheritdoc
*/
protected function streamWrite($resource, array $record)
protected function streamWrite($stream, array $record): void
{
if (is_array($record['formatted'])) {
foreach ($record['formatted'] as $key => $info) {
Expand All @@ -39,17 +32,20 @@ protected function streamWrite($resource, array $record)
}
}
}
$formated = (array)$record['formatted'];
$formatted = (array)$record['formatted'];
if (version_compare(PHP_VERSION, '5.5.4', '>=') && !defined('HHVM_VERSION')) {
return fputcsv($resource, $formated, static::DELIMITER, static::ENCLOSURE, static::ESCAPE_CHAR);
fputcsv($stream, $formatted, static::DELIMITER, static::ENCLOSURE, static::ESCAPE_CHAR);
return;
}
return fputcsv($resource, $formated, static::DELIMITER, static::ENCLOSURE);
fputcsv($stream, $formatted, static::DELIMITER, static::ENCLOSURE);
}

/**
* @inheritdoc
* Gets the default formatter.
*
* Overwrite this if the LineFormatter is not a good default for your handler.
*/
protected function getDefaultFormatter()
protected function getDefaultFormatter(): FormatterInterface
{
return new NormalizerFormatter();
}
Expand Down
22 changes: 3 additions & 19 deletions tests/Monolog/Handler/CsvHandlerTest.php
Original file line number Diff line number Diff line change
@@ -1,18 +1,10 @@
<?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 FemtoPixel\Monolog\TestCase;
use Monolog\Logger;
use \Monolog\Formatter\NormalizerFormatter;

class CsvHandlerTest extends TestCase
{
Expand All @@ -32,18 +24,10 @@ public function testWriteWithNormalizer()
{
$handle = fopen('php://memory', 'a+');
$handler = new CsvHandler($handle);
$handler->setFormatter($this->getNormalizeFormatter());
$handler->setFormatter(new NormalizerFormatter);
$handler->handle($this->getRecord(Logger::WARNING, 'doesn\'t fail'));
fseek($handle, 0);
$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~";
$regexp = "~\\A'doesn''t fail',\\[\\],300,WARNING,test,[0-9]{4}\\-[0-9]{2}\\-[0-9]{2}T[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)));
}

/**
* @return \Monolog\Formatter\NormalizerFormatter
*/
protected function getNormalizeFormatter()
{
return $this->getMock('Monolog\\Formatter\\NormalizerFormatter', null);
}
}
33 changes: 7 additions & 26 deletions tests/Monolog/TestCase.php
Original file line number Diff line number Diff line change
@@ -1,21 +1,16 @@
<?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;
use Monolog\Formatter\FormatterInterface;

class TestCase extends \PHPUnit_Framework_TestCase
class TestCase extends \PHPUnit\Framework\TestCase
{
/**
* @param int $level
* @param string $message
* @param array $context
* @return array Record
*/
protected function getRecord($level = Logger::WARNING, $message = 'test', $context = array())
Expand All @@ -32,25 +27,11 @@ protected function getRecord($level = Logger::WARNING, $message = 'test', $conte
}

/**
* @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
* @return FormatterInterface
*/
protected function getIdentityFormatter()
{
$formatter = $this->getMock('Monolog\\Formatter\\FormatterInterface');
$formatter = $this->createMock(FormatterInterface::class);
$formatter->expects($this->any())
->method('format')
->will($this->returnCallback(function ($record) { return $record['message']; }));
Expand Down

0 comments on commit ae6d1d5

Please sign in to comment.