-
Notifications
You must be signed in to change notification settings - Fork 0
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
Initial work on moving the Processor for messages to a separate class #21
base: v2_future
Are you sure you want to change the base?
Changes from all commits
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 |
---|---|---|
|
@@ -2,12 +2,15 @@ | |
|
||
namespace Productsup\Flexilog\Handler; | ||
|
||
use Productsup\Flexilog\Processor\ProcessorInterface; | ||
|
||
/** | ||
* Abstract Handler to simplify the implementation of a Handler Interface | ||
*/ | ||
abstract class AbstractHandler implements HandlerInterface | ||
{ | ||
protected $logger = null; | ||
protected $processor = null; | ||
public $verbose = 0; | ||
public $minLevel = 7; | ||
const LOG_LEVELS = array( | ||
|
@@ -30,9 +33,20 @@ abstract class AbstractHandler implements HandlerInterface | |
* | ||
* @param string $minimalLevel the minimal severity of the LogLevel to start logging with | ||
* @param integer $verbose the Verbosity of the Log | ||
* @param array $additionalParameters optional additional parameters required for the Handler | ||
* @param \Productsup\Flexilog\Processor $processor the Processor to be used for the Handler, if none supplied | ||
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. \Productsup\Flexilog\Processor can be replaced with ProcessorInterface |
||
* the DefaultProcessor one will be used. | ||
*/ | ||
public function __construct($minimalLevel = 'debug', $verbose = 0) | ||
public function __construct($minimalLevel = 'debug', | ||
$verbose = 0, | ||
array $additionalParameters = array(), | ||
ProcessorInterface $processor = null) | ||
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. PSR-2 Compliant would be:
|
||
{ | ||
if (is_null($processor)) { | ||
$this->processor = new \Productsup\Flexilog\Processor\DefaultProcessor(); | ||
} else { | ||
$this->processor = $processor; | ||
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. $this->processor = $processor ?? new \Productsup\Flexilog\Processor\DefaultProcessor; 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. That would make the entire codebase PHP7 minimum, since this feature didn't exist before. http://php.net/manual/en/migration70.new-features.php#migration70.new-features.null-coalesce-op Currently the codebase is PHP5.6 compatible. 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 about:
|
||
} | ||
$this->verbose = $verbose; | ||
if (array_key_exists($minimalLevel, self::LOG_LEVELS)) { | ||
$this->minLevel = self::LOG_LEVELS[$minimalLevel]; | ||
|
@@ -46,88 +60,30 @@ public function init() | |
{ | ||
} | ||
|
||
/** | ||
* Set the Logger for the Handler | ||
* | ||
* @param \Productsup\Flexilog\Logger $logger | ||
* | ||
* @return HandlerInterface $this | ||
*/ | ||
public function setLogger(\Productsup\Flexilog\Logger $logger) | ||
public function setProcessor(\Productsup\Flexilog\Processor $processor) | ||
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. Add docblock. It the property $processor now a Processor or a ProcessorInterface? Big difference imo. |
||
{ | ||
$this->logger = $logger; | ||
$this->processor = $processor; | ||
|
||
return $this; | ||
} | ||
|
||
/** | ||
* Interpolates context values into the message placeholders. | ||
* | ||
* @param string $message Message to Log with Placeholders, defined by {curly}-braces. | ||
* @param array $context Key/Value array with properties for the Placeholders. | ||
* | ||
* @return string $message Message with Placeholders replaced by the context. | ||
*/ | ||
public function interpolate($message, array $context = array()) | ||
public function getProcessor() | ||
{ | ||
// build a replacement array with braces around the context keys | ||
$replace = array(); | ||
foreach ($context as $key => $val) { | ||
if (is_numeric($val)) { | ||
$val = (string) $val; | ||
} | ||
if (is_string($val)) { | ||
$replace['{'.$key.'}'] = $val; | ||
} | ||
} | ||
|
||
// interpolate replacement values into the message and return | ||
return strtr($message, $replace); | ||
return $this->processor; | ||
} | ||
|
||
/** | ||
* Prepare the Context before interpolation | ||
* Turns Objects into String representations. | ||
* Set the Logger for the Handler | ||
* | ||
* @param array $context Key/Value array with properties for the Placeholders. | ||
* @param \Productsup\Flexilog\Logger $logger | ||
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. Is there a Flexilog/LoggerInterface? Which theoretically another logger could create a Flexilog compatible class? |
||
* | ||
* @return array $conext Cleaned context | ||
* @return HandlerInterface $this | ||
*/ | ||
public function prepareContext(array $context) | ||
public function setLogger(\Productsup\Flexilog\Logger $logger) | ||
{ | ||
// cleanup any thrown exceptions | ||
foreach ($context as $contextKey => $contextObject) { | ||
// some reserved keywords | ||
$reserved = array('date'); | ||
if (in_array($contextKey, $reserved)) { | ||
// prepend with an underscore | ||
$newkey = '_'.$contextKey; | ||
$context[$newkey] = $context[$contextKey]; | ||
unset($context[$contextKey]); | ||
$contextKey = $newkey; | ||
} | ||
|
||
if ($contextObject instanceof \Exception) { | ||
$contextObject = $contextObject->__toString(); | ||
} elseif (is_array($contextObject)) { | ||
$contextObject = json_encode($contextObject, true); | ||
} elseif ($contextObject instanceof \DateTime) { | ||
$contextObject = $contextObject->format(\DateTime::RFC3339); | ||
} elseif (is_object($contextObject)) { | ||
$contextObject = $contextObject->__toString(); | ||
} elseif (is_resource($contextObject)) { | ||
$contextObject = get_resource_type($contextObject); | ||
} | ||
|
||
$context[$contextKey] = $contextObject; | ||
|
||
// clean empty values | ||
if (empty($contextObject) && (string) $contextObject !== '0') { | ||
unset($context[$contextKey]); | ||
} | ||
} | ||
$this->logger = $logger; | ||
|
||
return $context; | ||
return $this; | ||
} | ||
|
||
/** | ||
|
@@ -148,8 +104,8 @@ public function prepare($level, $message, array $context = array()) | |
$logInfo->validate(); | ||
$context = array_merge($context, $logInfo->getData()); | ||
$context['loglevel'] = $level; | ||
$context = $this->prepareContext($context); | ||
$message = $this->interpolate($message, $context); | ||
$context = $this->processor->prepareContext($context); | ||
$message = $this->processor->interpolate($message, $context); | ||
|
||
return array($message, $context); | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,6 +2,8 @@ | |
|
||
namespace Productsup\Flexilog\Handler; | ||
|
||
use Productsup\Flexilog\Processor\ProcessorInterface; | ||
|
||
/** | ||
* Write to a specified File | ||
*/ | ||
|
@@ -14,13 +16,16 @@ class FileHandler extends AbstractHandler | |
* | ||
* @param array $additionalParameters Pass an array with the `filename` as a key/value to be used. | ||
*/ | ||
public function __construct($minimalLevel, $verbose, $additionalParameters = array()) | ||
public function __construct($minimalLevel = 'debug', | ||
$verbose = 0, | ||
array $additionalParameters = array(), | ||
ProcessorInterface $processor = null) | ||
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. Not PSR-2 compliant. See http://www.php-fig.org/psr/psr-2/#44-method-arguments |
||
{ | ||
if (!isset($additionalParameters['filename'])) { | ||
throw new \Exception('Filename parameter must be set'); | ||
} | ||
$filename = $additionalParameters['filename']; | ||
parent::__construct($minimalLevel, $verbose); | ||
parent::__construct($minimalLevel, $verbose, $additionalParameters, $processor); | ||
if ((!file_exists($filename) && file_put_contents($filename, '') === false) ||!is_writable($filename)) { | ||
throw new \Exception('No write permission on file:'.$filename); | ||
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. Exception not listed in docblock, please add:
|
||
} | ||
|
@@ -35,13 +40,12 @@ public function __construct($minimalLevel, $verbose, $additionalParameters = arr | |
*/ | ||
public function write($level, $message, array $context = array()) | ||
{ | ||
$line = sprintf('%s %s: %s%s', date('H:i:s'), strtoupper($level), $message, PHP_EOL); | ||
$this->writeToFile($line); | ||
$this->writeToFile($this->processor->decorateMessage($level, $message, $context)); | ||
|
||
if ($this->verbose >= 1) { | ||
$this->writeToFile("Extra Variables:".PHP_EOL); | ||
$this->writeToFile($this->processor->contextSeparator()); | ||
foreach ($context as $contextKey => $contextObject) { | ||
$this->writeToFile(sprintf("\t%s: %s%s", $contextKey, $contextObject, PHP_EOL)); | ||
$this->writeToFile($this->processor->decorateContext($level, $contextKey, $contextObject)); | ||
} | ||
} | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,6 +2,7 @@ | |
|
||
namespace Productsup\Flexilog\Handler; | ||
|
||
use Productsup\Flexilog\Processor\ProcessorInterface; | ||
use Productsup\Flexilog\Exception\HandlerException; | ||
use Productsup\Flexilog\Exception\HandlerConnectionException; | ||
use Gelf; | ||
|
@@ -19,13 +20,16 @@ class GelfHandler extends AbstractHandler | |
* | ||
* @param $additionalParameters array Pass the `server` and `port` as a key/value array | ||
*/ | ||
public function __construct($minimalLevel, $verbose, $additionalParameters = array()) | ||
public function __construct($minimalLevel = 'debug', | ||
$verbose = 0, | ||
array $additionalParameters = array(), | ||
ProcessorInterface $processor = null) | ||
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. Not PSR2 compliant. |
||
{ | ||
if (!isset($additionalParameters['server'])) { | ||
throw new HandlerException('Server parameter must be set inside the $additionalParameters'); | ||
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. Exception not listed in docblock, should be:
|
||
} | ||
$port = isset($additionalParameters['port']) ? $additionalParameters['port'] : Gelf\Transport\UdpTransport::DEFAULT_PORT; | ||
parent::__construct($minimalLevel, $verbose); | ||
parent::__construct($minimalLevel, $verbose, $additionalParameters, $processor); | ||
$this->transport = new Gelf\Transport\UdpTransport( | ||
$additionalParameters['server'], | ||
$port, | ||
|
@@ -79,8 +83,8 @@ public function write($level, $message, array $context = array()) | |
if (isset($context['fullMessage'])) { | ||
$fullMessage = $context['fullMessage']; | ||
unset($context['fullMessage']); | ||
$fullMessage = $this->interpolate($fullMessage, $this->logger->getLogInfo()->getData()); | ||
$fullMessage = $this->interpolate($fullMessage, $context); | ||
$fullMessage = $this->processor->interpolate($fullMessage, $this->logger->getLogInfo()->getData()); | ||
$fullMessage = $this->processor->interpolate($fullMessage, $context); | ||
$splitFullMessage = $this->splitMessage($fullMessage); | ||
} | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,11 +2,26 @@ | |
|
||
namespace Productsup\Flexilog\Handler; | ||
|
||
use \Productsup\Flexilog\Processor\ProcessorInterface; | ||
|
||
/** | ||
* Handler Interface for the Flexilog endpoint handlers | ||
*/ | ||
interface HandlerInterface | ||
{ | ||
/** | ||
* Construct the Handler, optionally with a minimal logging level | ||
* | ||
* @param string $minimalLevel the minimal severity of the LogLevel to start logging with | ||
* @param integer $verbose the Verbosity of the Log | ||
* @param array $additionalParameters optional additional parameters required for the Handler | ||
* @param \Productsup\Flexilog\Processor $processor the Processor to be used for the Handler, if none supplied | ||
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. Code states fourth parameter should be ProcessorInterface, docblock states Proccesor. |
||
* the DefaultProcessor one will be used. | ||
*/ | ||
public function __construct($minimalLevel = 'debug', | ||
$verbose = 0, | ||
array $additionalParameters = array(), | ||
ProcessorInterface $processor = null); | ||
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. Not PSR2 compliant. |
||
|
||
/** | ||
* Write received Log information through the Handlers mechanism | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
<?php | ||
|
||
namespace Productsup\Flexilog\Handler; | ||
|
||
use Productsup\Flexilog\Processor\ProcessorInterface; | ||
use Productsup\Flexilog\Processor\LogfileProcessor; | ||
|
||
/** | ||
* Write to a specified File | ||
*/ | ||
class LogfileHandler extends FileHandler | ||
{ | ||
/** | ||
* {@inheritDoc} | ||
*/ | ||
public function __construct($minimalLevel = 'debug', | ||
$verbose = 0, | ||
array $additionalParameters = array(), | ||
ProcessorInterface $processor = null) | ||
{ | ||
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. Not PSR2 compliant. |
||
$processor = new LogfileProcessor(); | ||
parent::__construct($minimalLevel, $verbose, $additionalParameters, $processor); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,6 +2,7 @@ | |
|
||
namespace Productsup\Flexilog\Handler; | ||
|
||
use Productsup\Flexilog\Processor\ProcessorInterface; | ||
use Productsup\Flexilog\Exception\HandlerException; | ||
use Productsup\Flexilog\Exception\HandlerConnectionException; | ||
use Redis; | ||
|
@@ -18,7 +19,10 @@ class RedisHandler extends AbstractHandler | |
/** | ||
* {@inheritDoc} | ||
*/ | ||
public function __construct($minimalLevel, $verbose, $additionalParameters = array()) | ||
public function __construct($minimalLevel = 'debug', | ||
$verbose = 0, | ||
array $additionalParameters = array(), | ||
ProcessorInterface $processor = null) | ||
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. Not PSR2 compliant. |
||
{ | ||
if (!class_exists('Redis')) { | ||
throw new HandlerException('Class Redis is not found'); | ||
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. Exception not documented in docblock. |
||
|
@@ -35,7 +39,7 @@ public function __construct($minimalLevel, $verbose, $additionalParameters = arr | |
} | ||
$this->redis = new Redis(); | ||
$this->redisConfig = $redisConfig; | ||
parent::__construct($minimalLevel, $verbose); | ||
parent::__construct($minimalLevel, $verbose, $additionalParameters, $processor); | ||
} | ||
|
||
/** | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,6 +2,7 @@ | |
|
||
namespace Productsup\Flexilog\Handler; | ||
|
||
use Productsup\Flexilog\Processor\ProcessorInterface; | ||
use League; | ||
|
||
/** | ||
|
@@ -14,9 +15,12 @@ class ShellHandler extends AbstractHandler | |
/** | ||
* {@inheritDoc} | ||
*/ | ||
public function __construct($minimalLevel = 'debug', $verbose = 0) | ||
public function __construct($minimalLevel = 'debug', | ||
$verbose = 0, | ||
array $additionalParameters = array(), | ||
ProcessorInterface $processor = null) | ||
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. Not PSR2 compliant. |
||
{ | ||
parent::__construct($minimalLevel, $verbose); | ||
parent::__construct($minimalLevel, $verbose, $additionalParameters, $processor); | ||
$this->CLImate = new League\CLImate\CLImate(); | ||
$this->CLImate->output->defaultTo('error'); | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,6 +2,7 @@ | |
|
||
namespace Productsup\Flexilog\Handler; | ||
|
||
use Productsup\Flexilog\Processor\ProcessorInterface; | ||
use Productsup\Flexilog\Log\LogLevel; | ||
use Productsup\Flexilog\Exception\HandlerException; | ||
use Symfony\Component\Console\Output\OutputInterface; | ||
|
@@ -43,12 +44,15 @@ class SymfonyConsoleHandler extends AbstractHandler | |
/** | ||
* {@inheritDoc} | ||
*/ | ||
public function __construct($minimalLevel = 'debug', $verbose = 0, $additionalParameters = array()) | ||
public function __construct($minimalLevel = 'debug', | ||
$verbose = 0, | ||
array $additionalParameters = array(), | ||
ProcessorInterface $processor = null) | ||
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. Not PSR2 compliant. |
||
{ | ||
if (!isset($additionalParameters['outputInterface'])) { | ||
throw new HandlerException('OutputInterface parameter must be set'); | ||
} | ||
parent::__construct($minimalLevel, $verbose); | ||
parent::__construct($minimalLevel, $verbose, $additionalParameters, $processor); | ||
$this->outputInterface = $additionalParameters['outputInterface']; | ||
} | ||
|
||
|
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.
Add docblock: