Skip to content
This repository has been archived by the owner on Jan 30, 2020. It is now read-only.

Commit

Permalink
Conflicts
Browse files Browse the repository at this point in the history
  • Loading branch information
Show file tree
Hide file tree
Showing 27 changed files with 1,043 additions and 45 deletions.
10 changes: 9 additions & 1 deletion src/Formatter/Base.php
Expand Up @@ -32,10 +32,18 @@ class Base implements FormatterInterface
* Class constructor
*
* @see http://php.net/manual/en/function.date.php
* @param null|string $dateTimeFormat Format for DateTime objects
* @param null|string|array|Traversable $dateTimeFormat Format for DateTime objects
*/
public function __construct($dateTimeFormat = null)
{
if ($dateTimeFormat instanceof Traversable) {
$dateTimeFormat = iterator_to_array($dateTimeFormat);
}

if (is_array($dateTimeFormat)) {
$dateTimeFormat = isset($dateTimeFormat['dateTimeFormat'])? $dateTimeFormat['dateTimeFormat'] : null;
}

if (null !== $dateTimeFormat) {
$this->dateTimeFormat = $dateTimeFormat;
}
Expand Down
9 changes: 9 additions & 0 deletions src/Formatter/Db.php
Expand Up @@ -11,6 +11,7 @@
namespace Zend\Log\Formatter;

use DateTime;
use Traversable;

/**
* @category Zend
Expand All @@ -35,6 +36,14 @@ class Db implements FormatterInterface
*/
public function __construct($dateTimeFormat = null)
{
if ($dateTimeFormat instanceof Traversable) {
$dateTimeFormat = iterator_to_array($dateTimeFormat);
}

if (is_array($dateTimeFormat)) {
$dateTimeFormat = isset($dateTimeFormat['dateTimeFormat'])? $dateTimeFormat['dateTimeFormat'] : null;
}

if (null !== $dateTimeFormat) {
$this->setDateTimeFormat($dateTimeFormat);
}
Expand Down
16 changes: 12 additions & 4 deletions src/Formatter/FirePhp.php
Expand Up @@ -20,12 +20,20 @@ class FirePhp implements FormatterInterface
/**
* Formats the given event data into a single line to be written by the writer.
*
* @param array $event The event data which should be formatted.
* @return string
* @param array $event The event data which should be formatted.
* @return array line message and optionally label if 'extra' data exists.
*/
public function format($event)
{
return $event['message'];
$label = null;
if ( !empty($event['extra']) ) {
$line = $event['extra'];
$label = $event['message'];
} else {
$line = $event['message'];
}

return array($line, $label);
}

/**
Expand All @@ -41,7 +49,7 @@ public function getDateTimeFormat()
/**
* This method is implemented for FormatterInterface but not used.
*
* @param string $dateTimeFormat
* @param string $dateTimeFormat
* @return FormatterInterface
*/
public function setDateTimeFormat($dateTimeFormat)
Expand Down
11 changes: 10 additions & 1 deletion src/Formatter/Simple.php
Expand Up @@ -10,7 +10,7 @@

namespace Zend\Log\Formatter;

use DateTime;
use Traversable;
use Zend\Log\Exception;

/**
Expand Down Expand Up @@ -39,6 +39,15 @@ class Simple extends Base
*/
public function __construct($format = null, $dateTimeFormat = null)
{
if ($format instanceof Traversable) {
$format = iterator_to_array($format);
}

if (is_array($format)) {
$dateTimeFormat = isset($format['dateTimeFormat'])? $format['dateTimeFormat'] : null;
$format = isset($format['format'])? $format['format'] : null;
}

if (isset($format) && !is_string($format)) {
throw new Exception\InvalidArgumentException('Format must be a string');
}
Expand Down
160 changes: 151 additions & 9 deletions src/Logger.php
Expand Up @@ -59,13 +59,27 @@ class Logger implements LoggerInterface
*/
protected $writers;

/**
* Processors
*
* @var SplPriorityQueue
*/
protected $processors;

/**
* Writer plugins
*
* @var WriterPluginManager
*/
protected $writerPlugins;

/**
* Processor plugins
*
* @var ProcessorPluginManager
*/
protected $processorPlugins;

/**
* Registered error handler
*
Expand All @@ -83,12 +97,50 @@ class Logger implements LoggerInterface
/**
* Constructor
*
* @todo support configuration (writers, dateTimeFormat, and writer plugin manager)
* Set options for an logger. Accepted options are:
* - writers: array of writers to add to this logger
* - exceptionhandler: if true register this logger as exceptionhandler
* - errorhandler: if true register this logger as errorhandler
*
* @param array|\Traversable $options
* @return Logger
* @throws Exception\InvalidArgumentException
*/
public function __construct()
public function __construct(array $options = null)
{
$this->writers = new SplPriorityQueue();

if ($options instanceof Traversable) {
$options = ArrayUtils::iteratorToArray($options);
}

if (is_array($options)) {

if(isset($options['writers']) && is_array($options['writers'])) {
foreach($options['writers'] as $writer) {

if(!isset($writer['name'])) {
throw new Exception\InvalidArgumentException('Options must contain a name for the writer');
}

$priority = (isset($writer['priority'])) ? $writer['priority'] : null;
$writerOptions = (isset($writer['options'])) ? $writer['options'] : null;

$this->addWriter($writer['name'], $priority, $writerOptions);
}
}

if(isset($options['exceptionhandler']) && $options['exceptionhandler'] === true) {
self::registerExceptionHandler($this);
}

if(isset($options['errorhandler']) && $options['errorhandler'] === true) {
self::registerErrorHandler($this);
}

}

$this->processors = new SplPriorityQueue();
}

/**
Expand Down Expand Up @@ -206,6 +258,90 @@ public function setWriters(SplPriorityQueue $writers)
return $this;
}


/**
* Get processor plugin manager
*
* @return ProcessorPluginManager
*/
public function getProcessorPluginManager()
{
if (null === $this->processorPlugins) {
$this->setProcessorPluginManager(new ProcessorPluginManager());
}
return $this->processorPlugins;
}

/**
* Set processor plugin manager
*
* @param string|ProcessorPluginManager $plugins
* @return Logger
* @throws Exception\InvalidArgumentException
*/
public function setProcessorPluginManager($plugins)
{
if (is_string($plugins)) {
$plugins = new $plugins;
}
if (!$plugins instanceof ProcessorPluginManager) {
throw new Exception\InvalidArgumentException(sprintf(
'processor plugin manager must extend %s\ProcessorPluginManager; received %s',
__NAMESPACE__,
is_object($plugins) ? get_class($plugins) : gettype($plugins)
));
}

$this->processorPlugins = $plugins;
return $this;
}

/**
* Get processor instance
*
* @param string $name
* @param array|null $options
* @return Processor\ProcessorInterface
*/
public function processorPlugin($name, array $options = null)
{
return $this->getProcessorPluginManager()->get($name, $options);
}

/**
* Add a processor to a logger
*
* @param string|Processor\ProcessorInterface $processor
* @param int $priority
* @param array|null $options
* @return Logger
* @throws Exception\InvalidArgumentException
*/
public function addProcessor($processor, $priority = 1, array $options = null)
{
if (is_string($processor)) {
$processor = $this->processorPlugin($processor, $options);
} elseif (!$processor instanceof Processor\ProcessorInterface) {
throw new Exception\InvalidArgumentException(sprintf(
'Processor must implement Zend\Log\ProcessorInterface; received "%s"',
is_object($processor) ? get_class($processor) : gettype($processor)
));
}
$this->processors->insert($processor, $priority);

return $this;
}

/**
* Get processors
*
* @return SplPriorityQueue
*/
public function getProcessors()
{
return $this->processors;
}

/**
* Add a message as a log entry
*
Expand Down Expand Up @@ -250,14 +386,20 @@ public function log($priority, $message, $extra = array())
$message = var_export($message, true);
}

$event = array(
'timestamp' => $timestamp,
'priority' => (int) $priority,
'priorityName' => $this->priorities[$priority],
'message' => (string) $message,
'extra' => $extra
);

foreach($this->processors->toArray() as $processor) {
$event = $processor->process($event);
}

foreach ($this->writers->toArray() as $writer) {
$writer->write(array(
'timestamp' => $timestamp,
'priority' => (int) $priority,
'priorityName' => $this->priorities[$priority],
'message' => (string) $message,
'extra' => $extra
));
$writer->write($event);
}

return $this;
Expand Down
31 changes: 31 additions & 0 deletions src/LoggerServiceFactory.php
@@ -0,0 +1,31 @@
<?php
/**
* Zend Framework (http://framework.zend.com/)
*
* @link http://github.com/zendframework/zf2 for the canonical source repository
* @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
* @package Zend_Log
*/

namespace Zend\Log;

use Zend\ServiceManager\FactoryInterface;
use Zend\ServiceManager\ServiceLocatorInterface;

/**
* Logger.
*
* @category Zend
* @package Zend_Log
*/
class LoggerServiceFactory implements FactoryInterface
{
public function createService(ServiceLocatorInterface $serviceLocator)
{
// Configure the logger
$config = $serviceLocator->get('Config');
$logConfig = isset($config['log']) ? $config['log'] : array();
$logger = new Logger($logConfig);
}
}

0 comments on commit de3fd70

Please sign in to comment.