This repository has been archived by the owner on Jan 30, 2020. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'feature/6' into develop
Close #6
- Loading branch information
Showing
11 changed files
with
612 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
# PSR-3 Logger Interface compatibility | ||
|
||
[PSR-3 Logger Interface](https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-3-logger-interface.md) | ||
is a standards recommendation defining a common interface for logging libraries. The `zend-log` | ||
component predates it, and has minor incompatibilities, but starting with version 2.6 provides the | ||
following compatibility features: | ||
|
||
- PSR logger adapter | ||
- PSR logger writer | ||
- PSR placeholder processor | ||
|
||
## PsrLoggerAdapter | ||
|
||
`Zend\Log\PsrLoggerAdapter` wraps `Zend\Log\LoggerInterface`, allowing it to be used | ||
anywhere `Psr\Log\LoggerInterface` is expected. | ||
|
||
```php | ||
$zendLogLogger = new Zend\Log\Logger; | ||
|
||
$psrLogger = new Zend\Log\PsrLoggerAdapter($zendLogLogger); | ||
$psrLogger->log(Psr\Log\LogLevel::INFO, 'We have a PSR-compatible logger'); | ||
``` | ||
|
||
## PSR-3 log writer | ||
|
||
`Zend\Log\Writer\Psr` allows log messages and extras to be forwared to any PSR-3 compatible logger. | ||
As with any log writer, this has the added benefit that you filters can be used to limit forwarded | ||
messages. | ||
|
||
The writer needs a `Psr\Logger\LoggerInterface` instance to be useful, and fallbacks to | ||
`Psr\Log\NullLogger` if none is provided. There are three ways to provide the PSR logger instance to | ||
the log writer: | ||
|
||
```php | ||
// Via constructor parameter: | ||
$writer = new Zend\Log\Writer\Psr($psrLogger); | ||
|
||
// Via option: | ||
$writer = new Zend\Log\Writer\Psr(['logger' => $psrLogger]); | ||
|
||
// Via setter injection: | ||
$writer = new Zend\Log\Writer\Psr(); | ||
$writer->setLogger($psrLogger); | ||
``` | ||
|
||
## PSR-3 placeholder processor | ||
|
||
`Zend\Log\Processor\PsrPlaceholder` adds support for [PSR-3 message placeholders](https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-3-logger-interface.md#12-message). | ||
Placeholder names correspond to keys in the "extras" array passed when logging a message. | ||
|
||
Values can be of arbitrary type, including all scalars, and objects implementing `__toString`; | ||
objects not capable of string serialization will result in the fully-qualified class name being | ||
substituted. | ||
|
||
```php | ||
$logger = new Zend\Log\Logger; | ||
$logger->addProcessor(new Zend\Log\Processor\PsrPlaceholder); | ||
|
||
$logger->info('User with email {email} registered', ['email' => 'user@example.org']); | ||
// logs message 'User with email user@example.org registered' | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
<?php | ||
/** | ||
* Zend Framework (http://framework.zend.com/) | ||
* | ||
* @link http://github.com/zendframework/zf2 for the canonical source repository | ||
* @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com) | ||
* @license http://framework.zend.com/license/new-bsd New BSD License | ||
*/ | ||
|
||
namespace Zend\Log\Processor; | ||
|
||
/** | ||
* Processes an event message according to PSR-3 rules. | ||
* | ||
* This processor replaces `{foo}` with the value from `$extra['foo']`. | ||
*/ | ||
class PsrPlaceholder implements ProcessorInterface | ||
{ | ||
/** | ||
* @param array $event event data | ||
* @return array event data | ||
*/ | ||
public function process(array $event) | ||
{ | ||
if (false === strpos($event['message'], '{')) { | ||
return $event; | ||
} | ||
|
||
$replacements = []; | ||
foreach ($event['extra'] as $key => $val) { | ||
if (is_null($val) | ||
|| is_scalar($val) | ||
|| (is_object($val) && method_exists($val, "__toString")) | ||
) { | ||
$replacements['{'.$key.'}'] = $val; | ||
continue; | ||
} | ||
|
||
if (is_object($val)) { | ||
$replacements['{'.$key.'}'] = '[object '.get_class($val).']'; | ||
continue; | ||
} | ||
|
||
$replacements['{'.$key.'}'] = '['.gettype($val).']'; | ||
} | ||
|
||
$event['message'] = strtr($event['message'], $replacements); | ||
return $event; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
<?php | ||
/** | ||
* Zend Framework (http://framework.zend.com/) | ||
* | ||
* @link http://github.com/zendframework/zf2 for the canonical source repository | ||
* @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com) | ||
* @license http://framework.zend.com/license/new-bsd New BSD License | ||
*/ | ||
|
||
namespace Zend\Log; | ||
|
||
use Psr\Log\AbstractLogger as PsrAbstractLogger; | ||
use Psr\Log\InvalidArgumentException; | ||
use Psr\Log\LogLevel; | ||
|
||
/** | ||
* PSR-3 logger adapter for Zend\Log\LoggerInterface | ||
* | ||
* Decorates a LoggerInterface to allow it to be used anywhere a PSR-3 logger | ||
* is expected. | ||
*/ | ||
class PsrLoggerAdapter extends PsrAbstractLogger | ||
{ | ||
/** | ||
* Zend\Log logger | ||
* | ||
* @var LoggerInterface | ||
*/ | ||
protected $logger; | ||
|
||
/** | ||
* Map PSR-3 LogLevels to priority | ||
* | ||
* @var array | ||
*/ | ||
protected $psrPriorityMap = [ | ||
LogLevel::EMERGENCY => Logger::EMERG, | ||
LogLevel::ALERT => Logger::ALERT, | ||
LogLevel::CRITICAL => Logger::CRIT, | ||
LogLevel::ERROR => Logger::ERR, | ||
LogLevel::WARNING => Logger::WARN, | ||
LogLevel::NOTICE => Logger::NOTICE, | ||
LogLevel::INFO => Logger::INFO, | ||
LogLevel::DEBUG => Logger::DEBUG, | ||
]; | ||
|
||
/** | ||
* Constructor | ||
* | ||
* @param LoggerInterface $logger | ||
*/ | ||
public function __construct(LoggerInterface $logger) | ||
{ | ||
$this->logger = $logger; | ||
} | ||
|
||
/** | ||
* Returns composed LoggerInterface instance. | ||
* | ||
* @return LoggerInterface | ||
*/ | ||
public function getLogger() | ||
{ | ||
return $this->logger; | ||
} | ||
|
||
/** | ||
* Logs with an arbitrary level. | ||
* | ||
* @param mixed $level | ||
* @param string $message | ||
* @param array $context | ||
* @return null | ||
* @throws InvalidArgumentException if log level is not recognized | ||
*/ | ||
public function log($level, $message, array $context = []) | ||
{ | ||
if (! array_key_exists($level, $this->psrPriorityMap)) { | ||
throw new InvalidArgumentException(sprintf( | ||
'$level must be one of PSR-3 log levels; received %s', | ||
var_export($level, 1) | ||
)); | ||
} | ||
|
||
$priority = $this->psrPriorityMap[$level]; | ||
$this->logger->log($priority, $message, $context); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
<?php | ||
/** | ||
* Zend Framework (http://framework.zend.com/) | ||
* | ||
* @link http://github.com/zendframework/zf2 for the canonical source repository | ||
* @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com) | ||
* @license http://framework.zend.com/license/new-bsd New BSD License | ||
*/ | ||
|
||
namespace Zend\Log\Writer; | ||
|
||
use Psr\Log\LogLevel; | ||
use Psr\Log\LoggerAwareTrait as PsrLoggerAwareTrait; | ||
use Psr\Log\LoggerInterface as PsrLoggerInterface; | ||
use Psr\Log\NullLogger; | ||
use Zend\Log\Logger; | ||
|
||
/** | ||
* Proxies log messages to an existing PSR-3 compliant logger. | ||
*/ | ||
class Psr extends AbstractWriter | ||
{ | ||
use PsrLoggerAwareTrait; | ||
|
||
/** | ||
* Map priority to PSR-3 LogLevels | ||
* | ||
* @var int[] | ||
*/ | ||
protected $psrPriorityMap = [ | ||
Logger::EMERG => LogLevel::EMERGENCY, | ||
Logger::ALERT => LogLevel::ALERT, | ||
Logger::CRIT => LogLevel::CRITICAL, | ||
Logger::ERR => LogLevel::ERROR, | ||
Logger::WARN => LogLevel::WARNING, | ||
Logger::NOTICE => LogLevel::NOTICE, | ||
Logger::INFO => LogLevel::INFO, | ||
Logger::DEBUG => LogLevel::DEBUG, | ||
]; | ||
|
||
/** | ||
* Default log level (warning) | ||
* | ||
* @var int | ||
*/ | ||
protected $defaultLogLevel = LogLevel::WARNING; | ||
|
||
/** | ||
* Constructor | ||
* | ||
* Set options for a writer. Accepted options are: | ||
* | ||
* - filters: array of filters to add to this filter | ||
* - formatter: formatter for this writer | ||
* - logger: PsrLoggerInterface implementation | ||
* | ||
* @param array|Traversable|LoggerInterface $options | ||
* @throws Exception\InvalidArgumentException | ||
*/ | ||
public function __construct($options = null) | ||
{ | ||
if ($options instanceof PsrLoggerInterface) { | ||
$this->setLogger($options); | ||
} | ||
|
||
if ($options instanceof Traversable) { | ||
$options = iterator_to_array($options); | ||
} | ||
|
||
if (is_array($options) && isset($options['logger'])) { | ||
$this->setLogger($options['logger']); | ||
} | ||
|
||
parent::__construct($options); | ||
|
||
if (null === $this->logger) { | ||
$this->setLogger(new NullLogger); | ||
} | ||
} | ||
|
||
/** | ||
* Write a message to the PSR-3 compliant logger. | ||
* | ||
* @param array $event event data | ||
* @return void | ||
*/ | ||
protected function doWrite(array $event) | ||
{ | ||
$priority = $event['priority']; | ||
$message = $event['message']; | ||
$context = $event['extra']; | ||
|
||
$level = isset($this->psrPriorityMap[$priority]) | ||
? $this->psrPriorityMap[$priority] | ||
: $this->defaultLogLevel; | ||
|
||
$this->logger->log($level, $message, $context); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.