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

Allow setting formatter for Zend\Log\Writer\Db via config options #5882

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions library/Zend/Log/Writer/AbstractWriter.php
Original file line number Diff line number Diff line change
Expand Up @@ -306,6 +306,26 @@ public function setFormatter($formatter, array $options = null)
return $this;
}

/**
* Get formatter
*
* @return Formatter\FormatterInterface
*/
public function getFormatter()
{
return $this->formatter;
}

/**
* Check if the writer has a formatter
*
* @return bool
*/
public function hasFormatter()
{
return $this->formatter instanceof Formatter\FormatterInterface;
}

/**
* Set convert write errors to exception flag
*
Expand Down
5 changes: 3 additions & 2 deletions library/Zend/Log/Writer/Db.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
use Traversable;
use Zend\Db\Adapter\Adapter;
use Zend\Log\Exception;
use Zend\Log\Formatter;
use Zend\Log\Formatter\Db as DbFormatter;

class Db extends AbstractWriter
Expand Down Expand Up @@ -87,7 +86,9 @@ public function __construct($db, $tableName = null, array $columnMap = null, $se
$this->separator = $separator;
}

$this->setFormatter(new DbFormatter());
if (!$this->hasFormatter()) {
$this->setFormatter(new DbFormatter());
}
}

/**
Expand Down
30 changes: 30 additions & 0 deletions tests/ZendTest/Log/Writer/AbstractTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -102,4 +102,34 @@ public function testConstructorWithOptions()
$this->assertInstanceOf('Zend\Log\Filter\Priority', $filters[1]);
$this->assertEquals(3, $this->readAttribute($filters[1], 'priority'));
}

/**
* @covers Zend\Log\Writer\AbstractWriter::getFormatter
*/
public function testFormatterDefaultsToNull()
{
$this->assertNull($this->_writer->getFormatter());
}

/**
* @covers Zend\Log\Writer\AbstractWriter::getFormatter
* @covers Zend\Log\Writer\AbstractWriter::setFormatter
*/
public function testCanSetFormatter()
{
$formatter = new SimpleFormatter;
$this->_writer->setFormatter($formatter);
$this->assertSame($formatter, $this->_writer->getFormatter());
}

/**
* @covers Zend\Log\Writer\AbstractWriter::hasFormatter
*/
public function testHasFormatter()
{
$this->assertFalse($this->_writer->hasFormatter());

$this->_writer->setFormatter(new SimpleFormatter);
$this->assertTrue($this->_writer->hasFormatter());
}
}
6 changes: 6 additions & 0 deletions tests/ZendTest/Log/Writer/DbTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -242,5 +242,11 @@ public function testConstructWithOptions()
$filters = self::readAttribute($writer, 'filters');
$this->assertCount(1, $filters);
$this->assertEquals($filter, $filters[0]);

$registeredFormatter = self::readAttribute($writer, 'formatter');
$this->assertSame($formatter, $registeredFormatter);

$registeredDb = self::readAttribute($writer, 'db');
$this->assertSame($this->db, $registeredDb);
}
}