Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BREAKING] Upgrading monolog dependency to 2.0 #6

Open
wants to merge 2 commits into
base: v2.0.0
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@
"name": "pluggit/logging",
"description": "Logging provides a thin PSR-7 interface abstraction to connect any logging facility behind it.",
"require": {
"php": "^5.4|^7.0",
"php": "^7.2",
"psr/log": "^1.0"
},
"require-dev": {
"monolog/monolog": "^1.23",
"monolog/monolog": "^2.0",
"phpspec/phpspec": "^2.5",
"behat/behat": "^3.1",
"phpunit/phpunit": "4.8.*",
Expand Down
964 changes: 632 additions & 332 deletions composer.lock

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion spec/Monolog/Handler/RotatingFileHandlerBuilderSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class RotatingFileHandlerBuilderSpec extends ObjectBehavior
{
function let()
{
$this->beConstructedWith('log', 'Y-m-d', '{channel}.log', 14, '{date}_{filename}', Logger::NOTICE);
$this->beConstructedWith('log', 'Y-m-d', 14, '{channel}.log', '{date}_{filename}', Logger::NOTICE);
if( ! ini_get('date.timezone') )
{
date_default_timezone_set('UTC');
Expand Down
2 changes: 1 addition & 1 deletion spec/Monolog/Handler/SyslogUdpHandlerBuilderSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class SyslogUdpHandlerBuilderSpec extends ObjectBehavior
{
function let()
{
$this->beConstructedWith('log', 'Y-m-d', '{channel}.log', 14, '{date}_{filename}', Logger::NOTICE);
$this->beConstructedWith('syshost', 1234, Logger::NOTICE);
if( ! ini_get('date.timezone') )
{
date_default_timezone_set('UTC');
Expand Down
14 changes: 8 additions & 6 deletions spec/Monolog/LoggingFactorySpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@
use Cmp\Logging\Monolog\Handler\RotatingFileHandlerBuilder;
use Cmp\Logging\Monolog\Handler\SyslogUdpHandlerBuilder;
use Monolog\Formatter\FormatterInterface;
use Monolog\Handler\AbstractHandler;
use Monolog\Handler\HandlerInterface;
use Monolog\Handler\RotatingFileHandler;
use Monolog\Handler\SyslogUdpHandler;
use Monolog\Logger;
use PhpSpec\ObjectBehavior;
use Prophecy\Argument;
Expand All @@ -34,7 +36,7 @@ function it_should_return_logger_with_appropriate_channel()
$this->get('test_a')->getName()->shouldReturn('test_a');
}

function it_should_return_handler(HandlerBuilderInterface $handlerBuilder, HandlerInterface $handler, FormatterInterface $formatter)
function it_should_return_handler(HandlerBuilderInterface $handlerBuilder, AbstractHandler $handler, FormatterInterface $formatter)
{
$this->beConstructedWith('test', 'error', $formatter);
$channelName = 'test';
Expand All @@ -43,20 +45,20 @@ function it_should_return_handler(HandlerBuilderInterface $handlerBuilder, Handl
$this->get($channelName)->getHandlers()->shouldReturn([$handler]);
}

function it_should_return_many_handlers(RotatingFileHandlerBuilder $rotatingFileHandlerBuilder, SyslogUdpHandlerBuilder $syslogUdpHandlerBuilder, RotatingFileHandler $rotatingFileHandler, FormatterInterface $formatter)
function it_should_return_many_handlers(RotatingFileHandlerBuilder $rotatingFileHandlerBuilder, SyslogUdpHandlerBuilder $syslogUdpHandlerBuilder, RotatingFileHandler $rotatingFileHandler, SyslogUdpHandler $syslogUdpHandler, FormatterInterface $formatter)
{
$this->beConstructedWith('test', 'error', $formatter);
$channelName = 'test';
$rotatingFileHandlerBuilder->build($channelName, $formatter, [])->willReturn($rotatingFileHandler);
$syslogUdpHandlerBuilder->build($channelName, $formatter, [])->willReturn($syslogUdpHandlerBuilder);
$syslogUdpHandlerBuilder->build($channelName, $formatter, [])->willReturn($syslogUdpHandler);
$this->addHandlerBuilder($rotatingFileHandlerBuilder);
$this->addHandlerBuilder($syslogUdpHandlerBuilder);
$this->get($channelName)->getHandlers()->shouldReturn([$rotatingFileHandler, $syslogUdpHandlerBuilder]);
$this->get($channelName)->getHandlers()->shouldReturn([$rotatingFileHandler, $syslogUdpHandler]);
}

function it_should_build_rotating_file_handler()
{
$this->addRotatingFileHandlerBuilder('log', 'Y-m-d', '{channel}.log', 14, '{date}_{filename}', Logger::NOTICE);
$this->addRotatingFileHandlerBuilder('log', 'Y-m-d', 14, '{channel}.log', '{date}_{filename}', Logger::NOTICE);
$this->get('test')->getHandlers()[0]->shouldBeAnInstanceOf('Monolog\Handler\RotatingFileHandler');
}

Expand All @@ -75,7 +77,7 @@ function it_should_build_stdout_handler()
function it_should_build_all_handlers()
{
$this->addSyslogUdpHandlerBuilder('123.34.4.45', 89, Logger::NOTICE);
$this->addRotatingFileHandlerBuilder('log', 'Y-m-d', '{channel}.log', 14, '{date}_{filename}', Logger::NOTICE);
$this->addRotatingFileHandlerBuilder('log', 'Y-m-d', 14, '{channel}.log', '{date}_{filename}', Logger::NOTICE);
$this->addStdoutHandlerBuilder(Logger::NOTICE);
$this->get('test')->getHandlers()[0]->shouldBeAnInstanceOf('Monolog\Handler\SyslogUdpHandler');
$this->get('test')->getHandlers()[1]->shouldBeAnInstanceOf('Monolog\Handler\RotatingFileHandler');
Expand Down
4 changes: 2 additions & 2 deletions src/Monolog/Formatter/ElasticSearchFormatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class ElasticSearchFormatter extends JsonFormatter
*
* @return mixed The formatted record
*/
public function format(array $record)
public function format(array $record): string
{
$this->formatContextException($record);
if (!empty($record['context'])) {
Expand Down Expand Up @@ -52,7 +52,7 @@ protected function removeContextFromRecordIfEmpty(array $record)
*
* @return string
*/
protected function formatBatchJson(array $records)
protected function formatBatchJson(array $records): string
{
$cleanRecords = array_map(
function ($record) {
Expand Down
10 changes: 3 additions & 7 deletions src/Monolog/Formatter/ExceptionFormatterTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,13 @@
trait ExceptionFormatterTrait
{
/**
* @param \Exception $e
* @param \Throwable $e
* @param int $depth (for now will not be implemented as 0 will not give trace by default)
*
* @return array
*/
protected function normalizeException($e)
protected function normalizeException(\Throwable $e, int $depth = 0): array
{
// TODO 2.0 only check for Throwable
if (!$e instanceof \Exception && !$e instanceof \Throwable) {
throw new \InvalidArgumentException('Exception/Throwable expected, got '.gettype($e).' / '.get_class($e));
}

$data = array(
'class' => get_class($e),
'message' => $e->getMessage(),
Expand Down
3 changes: 2 additions & 1 deletion src/Monolog/Handler/HandlerBuilderInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

use Monolog\Formatter\FormatterInterface;
use Monolog\Handler\AbstractHandler;
use Monolog\Handler\HandlerInterface;

interface HandlerBuilderInterface
{
Expand All @@ -13,5 +14,5 @@ interface HandlerBuilderInterface
*
* @return AbstractHandler
*/
public function build($channelName, FormatterInterface $formatter, $processors = []);
public function build($channelName, FormatterInterface $formatter, $processors = []): AbstractHandler;
}
13 changes: 10 additions & 3 deletions src/Monolog/Handler/RotatingFileHandlerBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
namespace Cmp\Logging\Monolog\Handler;

use Monolog\Formatter\FormatterInterface;
use Monolog\Handler\AbstractHandler;
use Monolog\Handler\RotatingFileHandler;

class RotatingFileHandlerBuilder implements HandlerBuilderInterface
Expand Down Expand Up @@ -46,8 +47,14 @@ class RotatingFileHandlerBuilder implements HandlerBuilderInterface
* @param $fileNameFormat
* @param $level
*/
public function __construct($directoryPath, $dateFormat, $maxFiles, $fileName, $fileNameFormat, $level)
{
public function __construct(
string $directoryPath,
string $dateFormat,
int $maxFiles,
string $fileName,
string $fileNameFormat,
int $level
) {
$this->dateFormat = $dateFormat;
$this->maxFiles = $maxFiles;
$this->fileName = $fileName;
Expand All @@ -60,7 +67,7 @@ public function __construct($directoryPath, $dateFormat, $maxFiles, $fileName, $
/**
* @inheritDoc
*/
public function build($channelName, FormatterInterface $formatter, $processors = [])
public function build($channelName, FormatterInterface $formatter, $processors = []): AbstractHandler
{
$fileName = $this->directoryPath.'/'.str_replace('{channel}', $channelName, $this->fileName);
$handler = new RotatingFileHandler($fileName, $this->maxFiles);
Expand Down
2 changes: 1 addition & 1 deletion src/Monolog/Handler/StdoutHandlerBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public function __construct($level)
*
* @throws \Exception
*/
public function build($channelName, FormatterInterface $formatter, $processors = [])
public function build($channelName, FormatterInterface $formatter, $processors = []): AbstractHandler
{
if (!$this->handler) {
$this->handler = new StreamHandler("php://stdout");
Expand Down
9 changes: 5 additions & 4 deletions src/Monolog/Handler/SyslogUdpHandlerBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

use Monolog\Formatter\FormatterInterface;
use Monolog\Handler\AbstractHandler;
use Monolog\Handler\HandlerInterface;
use Monolog\Handler\SyslogUdpHandler;

class SyslogUdpHandlerBuilder implements HandlerBuilderInterface
Expand All @@ -18,7 +19,7 @@ class SyslogUdpHandlerBuilder implements HandlerBuilderInterface
private $syslogUdpHost;

/**
* @var string
* @var integer
*/
private $syslogUdpPort;

Expand All @@ -31,10 +32,10 @@ class SyslogUdpHandlerBuilder implements HandlerBuilderInterface
* SyslogUdpHandlerBuilder constructor.
*
* @param string $syslogUdpHost
* @param string $syslogUdpPort
* @param integer $syslogUdpPort
* @param integer $level
*/
public function __construct($syslogUdpHost, $syslogUdpPort, $level)
public function __construct(string $syslogUdpHost, int $syslogUdpPort, int $level)
{
$this->syslogUdpHost = $syslogUdpHost;
$this->syslogUdpPort = $syslogUdpPort;
Expand All @@ -44,7 +45,7 @@ public function __construct($syslogUdpHost, $syslogUdpPort, $level)
/**
* @inheritDoc
*/
public function build($channelName, FormatterInterface $formatter, $processors = [])
public function build($channelName, FormatterInterface $formatter, $processors = []): AbstractHandler
{
if (!$this->syslogUdpHandler) {
if (empty($this->syslogUdpHost)) {
Expand Down
3 changes: 2 additions & 1 deletion src/Monolog/Handler/TestHandlerBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
namespace Cmp\Logging\Monolog\Handler;

use Monolog\Formatter\FormatterInterface;
use Monolog\Handler\AbstractHandler;
use Monolog\Handler\TestHandler;

class TestHandlerBuilder implements HandlerBuilderInterface
Expand All @@ -14,7 +15,7 @@ class TestHandlerBuilder implements HandlerBuilderInterface
/**
* @inheritDoc
*/
public function build($channelName, FormatterInterface $formatter, $processors = [])
public function build($channelName, FormatterInterface $formatter, $processors = []): AbstractHandler
{
if (is_null($this->handler)) {
$this->handler = new TestHandler();
Expand Down
2 changes: 1 addition & 1 deletion src/Monolog/Logger/SilentLogger.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class SilentLogger extends Logger
* @param array $context The log context
* @return Boolean Whether the record has been processed
*/
public function addRecord($level, $message, array $context = array())
public function addRecord($level, $message, array $context = array()): bool
{
try{
$context = array_merge($this->defaultContext, $context);
Expand Down