Download project

This commit is contained in:
Roman Pyrih
2024-11-20 09:09:44 +01:00
parent 547a138d6a
commit 5ff041757f
40737 changed files with 7766183 additions and 0 deletions

View File

@@ -0,0 +1,4 @@
build
.idea
composer.lock
vendor

View File

@@ -0,0 +1,6 @@
filter:
paths: [src/*]
tools:
php_sim: true
php_pdepend: true
php_analyzer: true

View File

@@ -0,0 +1,20 @@
language: php
php:
- 5.5
- 5.6
- 7.0
- hhvm
matrix:
include:
- php: 5.5
env: 'COMPOSER_FLAGS="--prefer-stable --prefer-lowest"'
before_script:
- travis_retry composer self-update
- travis_retry composer update ${COMPOSER_FLAGS} --no-interaction
script:
- vendor/bin/phpunit
- vendor/bin/phpcs --standard=PSR2 src tests

View File

@@ -0,0 +1,18 @@
## 0.10 (2016-05-22)
This release has a number of BC breaks and refactorings. Specifically:
- The Formatter interface has been completely reworked to allow direct access to the logger (more below).
- Serializers have been replaced by Normalizers. This allows you to pipe the data into the log context in its original form.
The old interface for a formatter allowed you to return strings which were then passed to the logger for you. This was the simplest interface but it didn't allow using a number of advanced PSR-3 features like contexts or conditionally changing the log level based on the type of command or exception.
The new Formatter interface passes the logger to you directly so you can log yourself in any way you choose. We've also changed the Serializer to a Normalizer. This might seem tiny but the difference is important: rather than slamming objects down into strings, we now convert them to arrays so they can be passed to the PSR-3 context. This is awesome if you're using more powerful log aggregation/search tools.
We still ship with a couple of simple Formatters built in. These are great examples of how to build your own if you need really advanced logging features and we recommend writing your own instead of adding more features to these.
There's one last change: because the actual logging is now done in Formatters instead of the middleware, you now pass the default log levels to the Formatter rather than the middleware. This is only a convenience for our built-in ones to get you a little further down the road, you don't need to do this for your own formatters.
For more informations:
- [#5](https://github.com/thephpleague/tactician-logger/pull/5): Rather than directly serializing command data into the message, the data is now piped to the log context parameter. This makes it much easier to parse for folks using more complex logging tools.
- [#7](https://github.com/thephpleague/tactician-logger/pull/7): Further reworks inspired by a review of #5.

View File

@@ -0,0 +1,34 @@
# tactician-logger
[![Latest Version](https://img.shields.io/github/release/thephpleague/tactician-logger.svg?style=flat-square)](https://github.com/thephpleague/tactician-logger/releases)
[![Software License](https://img.shields.io/badge/license-MIT-brightgreen.svg?style=flat-square)](LICENSE.md)
[![Build Status](https://img.shields.io/travis/thephpleague/tactician-logger/master.svg?style=flat-square)](https://travis-ci.org/thephpleague/tactician-logger)
[![Quality Score](https://img.shields.io/scrutinizer/g/thephpleague/tactician-logger.svg?style=flat-square)](https://scrutinizer-ci.com/g/thephpleague/tactician-logger)
This package adds support for logging incoming commands to any PSR-3 compliant logger.
## Install
Via Composer
``` bash
$ composer require league/tactician-logger
```
## Usage
See [the plugin documentation page](http://tactician.thephpleague.com/plugins/logger/) for examples and setup instructions
## Testing
``` bash
$ ./vendor/bin/phpunit
```
## Security
Disclosure information can be found on [the main Tactician repo](https://github.com/thephpleague/tactician#security).
## License
The MIT License (MIT). Please see [License File](LICENSE.md) for more information.

View File

@@ -0,0 +1,36 @@
{
"name": "league/tactician-logger",
"description": "Adds PSR-3 logging support to the Tactician command bus",
"keywords": [
"tactician",
"log",
"logging"
],
"homepage": "https://github.com/thephpleague/tactician-logger",
"license": "MIT",
"authors": [
{
"name": "Ross Tuck"
}
],
"require": {
"php" : ">=5.5.0",
"psr/log": "~1.0",
"league/tactician": "^1.0"
},
"require-dev": {
"phpunit/phpunit" : "4.*",
"mockery/mockery": "^0.9",
"squizlabs/php_codesniffer": "~2.3"
},
"autoload": {
"psr-4": {
"League\\Tactician\\Logger\\": "src"
}
},
"autoload-dev": {
"psr-4": {
"League\\Tactician\\Logger\\Tests\\": "tests"
}
}
}

View File

@@ -0,0 +1,32 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit bootstrap="vendor/autoload.php"
backupGlobals="false"
backupStaticAttributes="false"
colors="true"
verbose="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
processIsolation="false"
stopOnFailure="false">
<testsuites>
<testsuite name="League Test Suite">
<directory>tests</directory>
</testsuite>
</testsuites>
<filter>
<whitelist>
<directory suffix=".php">src/</directory>
</whitelist>
</filter>
<listeners>
<listener class='Mockery\Adapter\Phpunit\TestListener' />
</listeners>
<logging>
<log type="tap" target="build/report.tap"/>
<log type="junit" target="build/report.junit.xml"/>
<log type="coverage-html" target="build/coverage" charset="UTF-8" yui="true" highlight="true"/>
<log type="coverage-text" target="build/coverage.txt"/>
<log type="coverage-clover" target="build/logs/clover.xml"/>
</logging>
</phpunit>

View File

@@ -0,0 +1,70 @@
<?php
namespace League\Tactician\Logger\Formatter;
use Exception;
use Psr\Log\LoggerInterface;
use Psr\Log\LogLevel;
/**
* Returns log messages only dump the Command & Exception's class names.
*/
class ClassNameFormatter implements Formatter
{
/**
* @var string
*/
private $commandReceivedLevel;
/**
* @var string
*/
private $commandSucceededLevel;
/**
* @var string
*/
private $commandFailedLevel;
/**
* @param string $commandReceivedLevel
* @param string $commandSucceededLevel
* @param string $commandFailedLevel
*/
public function __construct(
$commandReceivedLevel = LogLevel::DEBUG,
$commandSucceededLevel = LogLevel::DEBUG,
$commandFailedLevel = LogLevel::ERROR
) {
$this->commandReceivedLevel = $commandReceivedLevel;
$this->commandSucceededLevel = $commandSucceededLevel;
$this->commandFailedLevel = $commandFailedLevel;
}
/**
* {@inheritDoc}
*/
public function logCommandReceived(LoggerInterface $logger, $command)
{
$logger->log($this->commandReceivedLevel, 'Command received: ' . get_class($command), []);
}
/**
* {@inheritDoc}
*/
public function logCommandSucceeded(LoggerInterface $logger, $command, $returnValue)
{
$logger->log($this->commandSucceededLevel, 'Command succeeded: ' . get_class($command), []);
}
/**
* {@inheritDoc}
*/
public function logCommandFailed(LoggerInterface $logger, $command, Exception $e)
{
$logger->log(
$this->commandFailedLevel,
'Command failed: ' . get_class($command),
['exception' => $e]
);
}
}

View File

@@ -0,0 +1,91 @@
<?php
namespace League\Tactician\Logger\Formatter;
use League\Tactician\Logger\PropertyNormalizer\PropertyNormalizer;
use League\Tactician\Logger\PropertyNormalizer\SimplePropertyNormalizer;
use Exception;
use Psr\Log\LoggerInterface;
use Psr\Log\LogLevel;
/**
* Formatter that includes the Command's name and properties for more detail
*/
class ClassPropertiesFormatter implements Formatter
{
/**
* @var PropertyNormalizer
*/
private $normalizer;
/**
* @var string
*/
private $commandReceivedLevel;
/**
* @var string
*/
private $commandSucceededLevel;
/**
* @var string
*/
private $commandFailedLevel;
/**
* @param PropertyNormalizer $normalizer
* @param string $commandReceivedLevel
* @param string $commandSucceededLevel
* @param string $commandFailedLevel
*/
public function __construct(
PropertyNormalizer $normalizer = null,
$commandReceivedLevel = LogLevel::DEBUG,
$commandSucceededLevel = LogLevel::DEBUG,
$commandFailedLevel = LogLevel::ERROR
) {
$this->normalizer = $normalizer ?: new SimplePropertyNormalizer();
$this->commandReceivedLevel = $commandReceivedLevel;
$this->commandSucceededLevel = $commandSucceededLevel;
$this->commandFailedLevel = $commandFailedLevel;
}
/**
* {@inheritDoc}
*/
public function logCommandReceived(LoggerInterface $logger, $command)
{
$logger->log(
$this->commandReceivedLevel,
'Command received: ' . get_class($command),
['command' => $this->normalizer->normalize($command)]
);
}
/**
* {@inheritDoc}
*/
public function logCommandSucceeded(LoggerInterface $logger, $command, $returnValue)
{
$logger->log(
$this->commandSucceededLevel,
'Command succeeded: ' . get_class($command),
[
'command' => $this->normalizer->normalize($command)
]
);
}
/**
* {@inheritDoc}
*/
public function logCommandFailed(LoggerInterface $logger, $command, Exception $e)
{
$logger->log(
$this->commandFailedLevel,
'Command failed: ' . get_class($command),
['exception' => $e]
);
}
}

View File

@@ -0,0 +1,46 @@
<?php
namespace League\Tactician\Logger\Formatter;
use Exception;
use Psr\Log\LoggerInterface;
/**
* Converts incoming Commands into log messages.
*
* Each method is written for a particular command path. A formatter class
* should take the given command, format it to a message and pass it to the
* given logger (with the desired log level).
*
* For an example of what this all looks like, take a look at the
* ClassNameFormatter example bundled with this package.
*
* A formatter may also use PSR-3 log contexts to pass extra info to the logger
* about the commands, return values and errors it receives. For more
* information about log contexts, see the PSR-3 specification.
* @see https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-3-logger-interface.md#13-context
*/
interface Formatter
{
/**
* @param LoggerInterface $logger
* @param object $command
* @return void
*/
public function logCommandReceived(LoggerInterface $logger, $command);
/**
* @param LoggerInterface $logger
* @param object $command
* @param mixed $returnValue
* @return void
*/
public function logCommandSucceeded(LoggerInterface $logger, $command, $returnValue);
/**
* @param LoggerInterface $logger
* @param object $command
* @param Exception $e
* @return void
*/
public function logCommandFailed(LoggerInterface $logger, $command, Exception $e);
}

View File

@@ -0,0 +1,53 @@
<?php
namespace League\Tactician\Logger;
use League\Tactician\Logger\Formatter\Formatter;
use League\Tactician\Middleware;
use Psr\Log\LoggerInterface;
use Exception;
/**
* Add support for writing a message to the log whenever a command is received,
* handled or failed.
*/
class LoggerMiddleware implements Middleware
{
/**
* @var LoggerInterface
*/
private $logger;
/**
* @var Formatter
*/
private $formatter;
/**
* @param Formatter $formatter
* @param LoggerInterface $logger
*/
public function __construct(Formatter $formatter, LoggerInterface $logger)
{
$this->formatter = $formatter;
$this->logger = $logger;
}
/**
* {@inheritdoc}
*/
public function execute($command, callable $next)
{
$this->formatter->logCommandReceived($this->logger, $command);
try {
$returnValue = $next($command);
} catch (Exception $e) {
$this->formatter->logCommandFailed($this->logger, $command, $e);
throw $e;
}
$this->formatter->logCommandSucceeded($this->logger, $command, $returnValue);
return $returnValue;
}
}

View File

@@ -0,0 +1,19 @@
<?php
namespace League\Tactician\Logger\PropertyNormalizer;
/**
* Normalize value into scalars, usually to put them in a log message's context
*
* If given an object, return an array of properties. If given scalars, just
* return them directly.
*
* Implementations should work on any value, not just commands or exceptions.
*/
interface PropertyNormalizer
{
/**
* @param mixed $value
* @return string
*/
public function normalize($value);
}

View File

@@ -0,0 +1,52 @@
<?php
namespace League\Tactician\Logger\PropertyNormalizer;
use ReflectionClass;
/**
* Quick'n'dirty property normalizer that logs the first level properties
*
* Does not recurse into sub-objects or arrays.
*
* This is done in an extremely inefficient manner, so please never use this in
* a production context, only for local debugging.
*/
class SimplePropertyNormalizer implements PropertyNormalizer
{
/**
* @param object $command
* @return array
*/
public function normalize($command)
{
$reflectionClass = new ReflectionClass(get_class($command));
$properties = [];
foreach ($reflectionClass->getProperties() as $property) {
$property->setAccessible(true);
$properties[$property->getName()] = $this->formatValue($property->getValue($command));
}
return $properties;
}
/**
* Return the given (property) value as a descriptive string
*
* @param mixed $value Can be literally anything
* @return string
*/
protected function formatValue($value)
{
switch (gettype($value)) {
case 'object':
return 'object(' . get_class($value) . ')';
case 'array':
return '*array*';
case 'resource':
return 'resource(' . get_resource_type($value) . ')';
default:
return $value;
}
}
}