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

Update to monolog v2 #66

Merged
merged 13 commits into from
Jan 29, 2020
Merged
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
2 changes: 1 addition & 1 deletion .scrutinizer.yml
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:
-
Expand Down
16 changes: 8 additions & 8 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,49 +21,49 @@ jobs:
include:
- stage: test
name: 'Lint Syntax'
php: 7.1
php: 7.3
script:
- composer lint:syntax
- stage: test
name: 'Lint Style'
php: 7.1
php: 7.3
script:
- composer lint:style
- stage: test
name: 'Coverage'
php: 7.1
php: 7.3
script:
- composer test:coverage
- stage: test
name: 'Mutations'
php: 7.1
php: 7.3
script:
- composer require --dev infection/infection
- composer test:mutations
- stage: test
name: 'ZF2 Integration, DEPS=LATEST'
php: 7.1
php: 7.3
script:
- cd test/ZF2
- composer install --no-interaction
- vendor/bin/phpunit
- stage: test
name: 'ZF2 Integration, DEPS=LOWEST'
php: 7.1
php: 7.3
script:
- cd test/ZF2
- composer update --prefer-lowest --no-interaction
- vendor/bin/phpunit
- stage: test
name: 'ZF3 Integration, DEPS=LATEST'
php: 7.1
php: 7.3
script:
- cd test/ZF3
- composer install --no-interaction
- vendor/bin/phpunit
- stage: test
name: 'ZF3 Integration, DEPS=LOWEST'
php: 7.1
php: 7.3
script:
- cd test/ZF3
- composer update --prefer-lowest --no-interaction
Expand Down
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ USAGE

```php
// usage over service locator
$serviceLocator->get('EnliteMonologService')->addDebug('hello world');
$serviceLocator->get('EnliteMonologService')->debug('hello world');

use EnliteMonolog\Service\MonologServiceAwareInterface,
EnliteMonolog\Service\MonologServiceAwareTrait;
Expand All @@ -31,7 +31,7 @@ class MyService implements MonologServiceAwareInterface

public function whatever()
{
$this->getMonologService()->addDebug('hello world');
$this->getMonologService()->debug('hello world');
}
}

Expand Down Expand Up @@ -87,8 +87,8 @@ By default it write logs to `data/logs/application.log`. If you want change this
now you can use it

```php
$serviceLocator->get('EnliteMonologService')->addDebug('hello world');
$serviceLocator->get('MyChromeLogger')->addInfo('hello world');
$serviceLocator->get('EnliteMonologService')->debug('hello world');
$serviceLocator->get('MyChromeLogger')->debug('hello world');
```

## Contributing
Expand Down
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
"require": {
"php": "^7.1",
"container-interop/container-interop": "^1.0",
"monolog/monolog": "^1.0.2",
"monolog/monolog": "^1.3 || ^2.0",
Copy link
Collaborator

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.

Copy link
Contributor Author

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.

"zendframework/zend-modulemanager": "^2",
"zendframework/zend-stdlib": "^2 || ^3",
"zendframework/zend-servicemanager": "^2 || ^3"
Expand All @@ -30,7 +30,7 @@
"jakub-onderka/php-parallel-lint": "^1.0",
"johnkary/phpunit-speedtrap": "^3.0",
"squizlabs/php_codesniffer": "^3.0",
"phpunit/phpunit": "^8 | ^7.5.15"
"phpunit/phpunit": "^7.5.20 || ^8.5.2"
},
"autoload": {
"psr-4": {
Expand Down
16 changes: 13 additions & 3 deletions src/EnliteMonolog/Service/MonologServiceFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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');
}
Expand Down Expand Up @@ -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 {
Copy link
Collaborator

Choose a reason for hiding this comment

The 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?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

apologies, i now see that testCannotCreateWithFormatterWithoutCorrectInterface covers this condition for php:^7.2.

throw new RuntimeException(sprintf(
'Handler(%s) doesn\'t implements %s to attach formatter.',
$handlerClassName,
FormattableHandlerInterface::class
));
}
}

return $instance;
Expand Down
108 changes: 77 additions & 31 deletions test/EnliteMonologTest/Service/FormatterMock.php
Original file line number Diff line number Diff line change
@@ -1,46 +1,92 @@
<?php

// @codingStandardsIgnoreFile
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what are these about?

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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

Copy link
Collaborator

Choose a reason for hiding this comment

The 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 monolog:^1 support in the next major version of this package.


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);
}
}
}
99 changes: 70 additions & 29 deletions test/EnliteMonologTest/Service/FormatterPrivateConstructorMock.php
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);
}
}
}
Loading