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

Commit

Permalink
Merge branch 'feature/logger-constructor-options' into develop
Browse files Browse the repository at this point in the history
Close #3086
  • Loading branch information
weierophinney committed Dec 11, 2012
2 parents 67cd670 + f7b2430 commit 364c93b
Show file tree
Hide file tree
Showing 15 changed files with 228 additions and 33 deletions.
21 changes: 15 additions & 6 deletions library/Zend/Log/Writer/AbstractWriter.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,13 +84,22 @@ public function __construct($options = null)

if (is_array($options)) {

if(isset($options['filters']) && is_array($options['filters'])) {
foreach($options['filters'] as $filter) {
if(!isset($filter['name'])) {
throw new Exception\InvalidArgumentException('Options must contain a name for the filter');
if(isset($options['filters'])) {
$filters = $options['filters'];
if(is_string($filters) || $filters instanceof Filter\FilterInterface) {
$this->addFilter($filters);
} elseif(is_array($filters)) {
foreach($filters as $filter) {
if(is_string($filter) || $filter instanceof Filter\FilterInterface) {
$this->addFilter($filter);
} elseif(is_array($filter)) {
if(!isset($filter['name'])) {
throw new Exception\InvalidArgumentException('Options must contain a name for the filter');
}
$filterOptions = (isset($filter['options'])) ? $filter['options'] : null;
$this->addFilter($filter['name'], $filterOptions);
}
}
$filterOptions = (isset($filter['options'])) ? $filter['options'] : null;
$this->addFilter($filter['name'], $filterOptions);
}
}

Expand Down
19 changes: 17 additions & 2 deletions library/Zend/Log/Writer/ChromePhp.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,12 @@

namespace Zend\Log\Writer;

use Traversable;
use Zend\Log\Writer\ChromePhp\ChromePhpBridge;
use Zend\Log\Writer\ChromePhp\ChromePhpInterface;
use Zend\Log\Formatter\ChromePhp as ChromePhpFormatter;
use Zend\Log\Logger;
use Zend\Log\Exception;

/**
* @category Zend
Expand All @@ -32,11 +34,24 @@ class ChromePhp extends AbstractWriter
/**
* Initializes a new instance of this class.
*
* @param null|ChromePhpInterface $instance An instance of ChromePhpInterface
* @param null|ChromePhpInterface|array|Traversable $instance An instance of ChromePhpInterface
* that should be used for logging
*/
public function __construct(ChromePhpInterface $instance = null)
public function __construct($instance = null)
{
if ($instance instanceof Traversable) {
$inatce = iterator_to_array($instance);
}

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

if(!($instance instanceof ChromePhpInterface || $instance === null)) {
throw new Exception\InvalidArgumentException('You must pass a valid Zend\Log\Writer\ChromePhp\ChromePhpInterface');
}

$this->chromephp = $instance === null ? $this->getChromePhp() : $instance;
$this->formatter = new ChromePhpFormatter();
}
Expand Down
1 change: 1 addition & 0 deletions library/Zend/Log/Writer/Db.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ public function __construct($db, $tableName = null, array $columnMap = null, $se
}

if (is_array($db)) {
parent::__construct($db);
$separator = isset($db['separator']) ? $db['separator'] : null;
$columnMap = isset($db['column']) ? $db['column'] : null;
$tableName = isset($db['table']) ? $db['table'] : null;
Expand Down
20 changes: 18 additions & 2 deletions library/Zend/Log/Writer/FirePhp.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,12 @@

namespace Zend\Log\Writer;

use Traversable;
use FirePHP as FirePHPService;
use Zend\Log\Formatter\FirePhp as FirePhpFormatter;
use Zend\Log\Logger;
use Zend\Log\Exception;
use FirePhp\FirePhpInterface;

/**
* @category Zend
Expand All @@ -32,11 +34,25 @@ class FirePhp extends AbstractWriter
/**
* Initializes a new instance of this class.
*
* @param null|FirePhp\FirePhpInterface $instance An instance of FirePhpInterface
* @param null|FirePhp\FirePhpInterface|array|Traversable $instance An instance of FirePhpInterface
* that should be used for logging
*/
public function __construct(FirePhp\FirePhpInterface $instance = null)
public function __construct($instance = null)
{
if ($instance instanceof Traversable) {
$instance = iterator_to_array($instance);
}

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

if ($instance instanceof FirePhpInterface) {
throw new Exception\InvalidArgumentException('You must pass a valid FirePhp\FirePhpInterface');
}


$this->firephp = $instance;
$this->formatter = new FirePhpFormatter();
}
Expand Down
5 changes: 4 additions & 1 deletion library/Zend/Log/Writer/Mail.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ public function __construct($mail, Transport\TransportInterface $transport = nul
}

if (is_array($mail)) {
parent::__construct($mail);
$transport = isset($mail['transport']) ? $mail['transport'] : null;
$mail = isset($mail['mail']) ? $mail['mail'] : null;
}
Expand All @@ -110,7 +111,9 @@ public function __construct($mail, Transport\TransportInterface $transport = nul
}
$this->setTransport($transport);

$this->formatter = new SimpleFormatter();
if($this->formatter === null) {
$this->formatter = new SimpleFormatter();
}
}

/**
Expand Down
28 changes: 15 additions & 13 deletions library/Zend/Log/Writer/MongoDB.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,7 @@
use Mongo;
use MongoDate;
use Traversable;
use Zend\Log\Exception\InvalidArgumentException;
use Zend\Log\Exception\RuntimeException;
use Zend\Log\Exception;
use Zend\Log\Formatter\FormatterInterface;
use Zend\Stdlib\ArrayUtils;

Expand Down Expand Up @@ -52,27 +51,30 @@ class MongoDB extends AbstractWriter
* @param array $saveOptions
* @throws Exception\InvalidArgumentException
*/
public function __construct($mongo, $database, $collection, array $saveOptions = array())
public function __construct($mongo, $database = null, $collection = null, array $saveOptions = array())
{
if ($mongo instanceof Traversable) {
// Configuration may be multi-dimensional due to save options
$mongo = ArrayUtils::iteratorToArray($mongo);
}
if (is_array($mongo)) {
parent::__construct($mongo);
$saveOptions = isset($mongo['save_options']) ? $mongo['save_options'] : null;
$collection = isset($mongo['collection']) ? $mongo['collection'] : null;
if (null === $collection) {
throw new Exception\InvalidArgumentException(
$database = isset($mongo['database']) ? $mongo['database'] : null;
$mongo = isset($mongo['mongo']) ? $mongo['mongo'] : null;
}

if (null === $collection) {
throw new Exception\InvalidArgumentException(
'The collection parameter cannot be empty'
);
}
$database = isset($mongo['database']) ? $mongo['database'] : null;
if (null === $database) {
throw new Exception\InvalidArgumentException(
);
}

if (null === $database) {
throw new Exception\InvalidArgumentException(
'The database parameter cannot be empty'
);
}
$mongo = isset($mongo['mongo']) ? $mongo['mongo'] : null;
);
}

if (!($mongo instanceof Mongo)) {
Expand Down
5 changes: 4 additions & 1 deletion library/Zend/Log/Writer/Stream.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ public function __construct($streamOrUrl, $mode = null, $logSeparator = null)
}

if (is_array($streamOrUrl)) {
parent::__construct($streamOrUrl);
$mode = isset($streamOrUrl['mode']) ? $streamOrUrl['mode'] : null;
$logSeparator = isset($streamOrUrl['log_separator']) ? $streamOrUrl['log_separator'] : null;
$streamOrUrl = isset($streamOrUrl['stream']) ? $streamOrUrl['stream'] : null;
Expand Down Expand Up @@ -96,7 +97,9 @@ public function __construct($streamOrUrl, $mode = null, $logSeparator = null)
$this->setLogSeparator($logSeparator);
}

$this->formatter = new SimpleFormatter();
if($this->formatter === null) {
$this->formatter = new SimpleFormatter();
}
}

/**
Expand Down
26 changes: 19 additions & 7 deletions library/Zend/Log/Writer/Syslog.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

namespace Zend\Log\Writer;

use Traversable;
use Zend\Log\Exception;
use Zend\Log\Logger;
use Zend\Log\Formatter\Simple as SimpleFormatter;
Expand Down Expand Up @@ -87,23 +88,34 @@ class Syslog extends AbstractWriter
* @param array $params Array of options; may include "application" and "facility" keys
* @return Syslog
*/
public function __construct(array $params = array())
public function __construct($params = null)
{
if (isset($params['application'])) {
$this->appName = $params['application'];
if ($params instanceof Traversable) {
$params = iterator_to_array($params);
}

$runInitializeSyslog = true;
if (isset($params['facility'])) {
$this->setFacility($params['facility']);
$runInitializeSyslog = false;

if (is_array($params)) {
parent::__construct($params);

if (isset($params['application'])) {
$this->appName = $params['application'];
}

if (isset($params['facility'])) {
$this->setFacility($params['facility']);
$runInitializeSyslog = false;
}
}

if ($runInitializeSyslog) {
$this->initializeSyslog();
}

$this->setFormatter(new SimpleFormatter('%message%'));
if($this->formatter === null) {
$this->setFormatter(new SimpleFormatter('%message%'));
}
}

/**
Expand Down
4 changes: 3 additions & 1 deletion library/Zend/Log/Writer/ZendMonitor.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,10 @@ class ZendMonitor extends AbstractWriter
*
* @return ZendMonitor
*/
public function __construct()
public function __construct($options = null)
{
parent::__construct($options);

if (!function_exists('monitor_custom_event')) {
$this->isEnabled = false;
}
Expand Down
17 changes: 17 additions & 0 deletions tests/ZendTest/Log/Writer/ChromePhpTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,4 +80,21 @@ public function testWriteDisabled()
));
$this->assertTrue(empty($this->chromephp->calls));
}

public function testConstructWithOptions()
{
$formatter = new \Zend\Log\Formatter\Simple();
$filter = new \Zend\Log\Filter\Mock();
$writer = new ChromePhp(array(
'filters' => $filter,
'formatter' => $formatter,
'instance' => $this->chromephp,
));
$this->assertTrue($writer->getChromePhp() instanceof ChromePhpInterface);
$this->assertAttributeInstanceOf('Zend\Log\Formatter\ChromePhp', 'formatter', $writer);

$filters = self::readAttribute($writer, 'filters');
$this->assertCount(1, $filters);
$this->assertEquals($filter, $filters[0]);
}
}
19 changes: 19 additions & 0 deletions tests/ZendTest/Log/Writer/DbTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -228,4 +228,23 @@ public function testWriteDateTimeAsExtraValue()
'extra_request_time' => $date->format(FormatterInterface::DEFAULT_DATETIME_FORMAT)
)), $this->db->calls['execute'][0]);
}

public function testConstructWithOptions()
{
$formatter = new \Zend\Log\Formatter\Simple();
$filter = new \Zend\Log\Filter\Mock();
$writer = new DbWriter(array(
'filters' => $filter,
'formatter' => $formatter,
'table' => $this->tableName,
'db' => $this->db,

));
$this->assertInstanceOf('Zend\Log\Writer\Db', $writer);
$this->assertAttributeEquals($this->tableName, 'tableName', $writer);

$filters = self::readAttribute($writer, 'filters');
$this->assertCount(1, $filters);
$this->assertEquals($filter, $filters[0]);
}
}
17 changes: 17 additions & 0 deletions tests/ZendTest/Log/Writer/FirePhpTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,4 +88,21 @@ public function testWriteDisabled()
));
$this->assertTrue(empty($this->firephp->calls));
}

public function testConstructWithOptions()
{
$formatter = new \Zend\Log\Formatter\Simple();
$filter = new \Zend\Log\Filter\Mock();
$writer = new FirePhp(array(
'filters' => $filter,
'formatter' => $formatter,
'instance' => $this->firephp,
));
$this->assertTrue($writer->getFirePhp() instanceof FirePhpInterface);
$this->assertAttributeInstanceOf('Zend\Log\Formatter\FirePhp', 'formatter', $writer);

$filters = self::readAttribute($writer, 'filters');
$this->assertCount(1, $filters);
$this->assertEquals($filter, $filters[0]);
}
}
30 changes: 30 additions & 0 deletions tests/ZendTest/Log/Writer/MailTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,4 +83,34 @@ public function testSetSubjectPrependText()
$this->assertContains('an info message', $contents);
$this->assertContains('Subject: test', $contents);
}

public function testConstructWithOptions()
{
$message = new MailMessage();
$transport = new Transport\File();
$options = new Transport\FileOptions(array(
'path' => __DIR__,
'callback' => function (Transport\File $transport) {
return MailTest::FILENAME;
},
));
$transport->setOptions($options);

$formatter = new \Zend\Log\Formatter\Simple();
$filter = new \Zend\Log\Filter\Mock();
$writer = new MailWriter(array(
'filters' => $filter,
'formatter' => $formatter,
'mail' => $message,
'transport' => $transport,
));

$this->assertAttributeEquals($message, 'mail', $writer);
$this->assertAttributeEquals($transport, 'transport', $writer);
$this->assertAttributeEquals($formatter, 'formatter', $writer);

$filters = self::readAttribute($writer, 'filters');
$this->assertCount(1, $filters);
$this->assertEquals($filter, $filters[0]);
}
}
Loading

0 comments on commit 364c93b

Please sign in to comment.