-
Notifications
You must be signed in to change notification settings - Fork 17
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
Update to monolog v2 #66
Changes from all commits
1e9f6b0
bfd2d49
9e77b77
41ad7ef
cd4cbf5
61fa788
ab44357
9886db5
a2c244e
d5fd2a9
12ace1b
a502af8
0e50768
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,7 @@ | ||
build: | ||
environment: | ||
php: | ||
version: 7.1.0 | ||
version: 7.4.0 | ||
tests: | ||
override: | ||
- | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,6 +11,7 @@ | |
use Interop\Container\Exception\ContainerException; | ||
use Interop\Container\Exception\NotFoundException; | ||
use InvalidArgumentException; | ||
use Monolog\Handler\FormattableHandlerInterface; | ||
use Monolog\Handler\HandlerInterface; | ||
use Monolog\Logger; | ||
use Monolog\Formatter\FormatterInterface; | ||
|
@@ -93,7 +94,6 @@ public function createHandler($container, MonologOptions $options, $handler) | |
return $container->get($handler); | ||
} | ||
|
||
|
||
if (!isset($handler['name'])) { | ||
throw new RuntimeException('Cannot create logger handler'); | ||
} | ||
|
@@ -130,8 +130,18 @@ public function createHandler($container, MonologOptions $options, $handler) | |
} | ||
|
||
if (isset($handler['formatter'])) { | ||
$formatter = $this->createFormatter($container, $handler['formatter']); | ||
$instance->setFormatter($formatter); | ||
if ($instance instanceof FormattableHandlerInterface | ||
|| !defined('Monolog\Logger::API') | ||
|| \Monolog\Logger::API === 1) { | ||
$formatter = $this->createFormatter($container, $handler['formatter']); | ||
$instance->setFormatter($formatter); | ||
} else { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. it seems we're missing coverage over this branch. will you please add a test? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. apologies, i now see that |
||
throw new RuntimeException(sprintf( | ||
'Handler(%s) doesn\'t implements %s to attach formatter.', | ||
$handlerClassName, | ||
FormattableHandlerInterface::class | ||
)); | ||
} | ||
} | ||
|
||
return $instance; | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,46 +1,92 @@ | ||
<?php | ||
|
||
// @codingStandardsIgnoreFile | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. what are these about? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. To keep compatibility I needed to create conditional implementations of mock. After that PHPCS throw this error https://travis-ci.org/enlitepro/enlite-monolog/jobs/641132116#L321 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. i see. i suppose it will be short-lived anyway. we can drop |
||
|
||
namespace EnliteMonologTest\Service; | ||
|
||
use Monolog\Formatter\FormatterInterface; | ||
|
||
class FormatterMock implements FormatterInterface | ||
{ | ||
/** @var callable */ | ||
private $encoder; | ||
|
||
/** | ||
* FormatterMock constructor. | ||
* @param callable $encoder | ||
*/ | ||
public function __construct($encoder) | ||
if (interface_exists(FormatterInterface::class)) { | ||
class FormatterMock implements FormatterInterface | ||
{ | ||
if (!is_callable($encoder)) { | ||
throw new \RuntimeException('Encoder must be callable.'); | ||
/** @var callable */ | ||
private $encoder; | ||
|
||
/** | ||
* FormatterMock constructor. | ||
* @param callable $encoder | ||
*/ | ||
public function __construct($encoder) | ||
{ | ||
if (!is_callable($encoder)) { | ||
throw new \RuntimeException('Encoder must be callable.'); | ||
} | ||
|
||
$this->encoder = $encoder; | ||
} | ||
|
||
$this->encoder = $encoder; | ||
} | ||
/** | ||
* Formats a log record. | ||
* | ||
* @param array $record A record to format | ||
* @return mixed The formatted record | ||
*/ | ||
public function format(array $record) | ||
{ | ||
return call_user_func($this->encoder, $record); | ||
} | ||
|
||
/** | ||
* Formats a log record. | ||
* | ||
* @param array $record A record to format | ||
* @return mixed The formatted record | ||
*/ | ||
public function format(array $record) | ||
{ | ||
return call_user_func($this->encoder, $record); | ||
/** | ||
* Formats a set of log records. | ||
* | ||
* @param array $records A set of records to format | ||
* @return mixed The formatted set of records | ||
*/ | ||
public function formatBatch(array $records) | ||
{ | ||
return array_map(array($this, 'format'), $records); | ||
} | ||
} | ||
|
||
/** | ||
* Formats a set of log records. | ||
* | ||
* @param array $records A set of records to format | ||
* @return mixed The formatted set of records | ||
*/ | ||
public function formatBatch(array $records) | ||
} else { | ||
class FormatterMock | ||
{ | ||
return array_map(array($this, 'format'), $records); | ||
/** @var callable */ | ||
private $encoder; | ||
|
||
/** | ||
* FormatterMock constructor. | ||
* @param callable $encoder | ||
*/ | ||
public function __construct($encoder) | ||
{ | ||
if (!is_callable($encoder)) { | ||
throw new \RuntimeException('Encoder must be callable.'); | ||
} | ||
|
||
$this->encoder = $encoder; | ||
} | ||
|
||
/** | ||
* Formats a log record. | ||
* | ||
* @param array $record A record to format | ||
* @return mixed The formatted record | ||
*/ | ||
public function format(array $record) | ||
{ | ||
return call_user_func($this->encoder, $record); | ||
} | ||
|
||
/** | ||
* Formats a set of log records. | ||
* | ||
* @param array $records A set of records to format | ||
* @return mixed The formatted set of records | ||
*/ | ||
public function formatBatch(array $records) | ||
{ | ||
return array_map(array($this, 'format'), $records); | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,43 +1,84 @@ | ||
<?php | ||
// @codingStandardsIgnoreFile | ||
|
||
namespace EnliteMonologTest\Service; | ||
|
||
use Monolog\Formatter\FormatterInterface; | ||
|
||
final class FormatterPrivateConstructorMock implements FormatterInterface | ||
{ | ||
/** | ||
* FormatterNamedFactoryMock constructor. | ||
*/ | ||
private function __construct() | ||
if (interface_exists(FormatterInterface::class)) { | ||
final class FormatterPrivateConstructorMock implements FormatterInterface | ||
{ | ||
} | ||
/** | ||
* FormatterNamedFactoryMock constructor. | ||
*/ | ||
private function __construct() | ||
{ | ||
} | ||
|
||
public static function create() | ||
{ | ||
return new self; | ||
} | ||
public static function create() | ||
{ | ||
return new self; | ||
} | ||
|
||
|
||
/** | ||
* Formats a log record. | ||
* | ||
* @param array $record A record to format | ||
* @return mixed The formatted record | ||
*/ | ||
public function format(array $record) | ||
{ | ||
return json_encode($record); | ||
} | ||
/** | ||
* Formats a log record. | ||
* | ||
* @param array $record A record to format | ||
* @return mixed The formatted record | ||
*/ | ||
public function format(array $record) | ||
{ | ||
return json_encode($record); | ||
} | ||
|
||
/** | ||
* Formats a set of log records. | ||
* | ||
* @param array $records A set of records to format | ||
* @return mixed The formatted set of records | ||
*/ | ||
public function formatBatch(array $records) | ||
/** | ||
* Formats a set of log records. | ||
* | ||
* @param array $records A set of records to format | ||
* @return mixed The formatted set of records | ||
*/ | ||
public function formatBatch(array $records) | ||
{ | ||
return array_map(array($this, 'format'), $records); | ||
} | ||
} | ||
} else { | ||
final class FormatterPrivateConstructorMock | ||
{ | ||
return array_map(array($this, 'format'), $records); | ||
/** | ||
* FormatterNamedFactoryMock constructor. | ||
*/ | ||
private function __construct() | ||
{ | ||
} | ||
|
||
public static function create() | ||
{ | ||
return new self; | ||
} | ||
|
||
|
||
/** | ||
* Formats a log record. | ||
* | ||
* @param array $record A record to format | ||
* @return mixed The formatted record | ||
*/ | ||
public function format(array $record) | ||
{ | ||
return json_encode($record); | ||
} | ||
|
||
/** | ||
* Formats a set of log records. | ||
* | ||
* @param array $records A set of records to format | ||
* @return mixed The formatted set of records | ||
*/ | ||
public function formatBatch(array $records) | ||
{ | ||
return array_map(array($this, 'format'), $records); | ||
} | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
oh, very nice. i wasn't expecting this. please give me a couple of days to really dig in.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I needed to define monolog:^1.3 because this version is first where psr-3 was implemented. And this interface is now a bridge between v1 and v2 in public interface of monolog.