Skip to content

Commit

Permalink
Logger refactor: rewrote writers
Browse files Browse the repository at this point in the history
Rename method from _write() to doWrite().
Firebug writer is broken right now. It needs to be rewrite for the new request/response HTTP objects.
  • Loading branch information
b-durand committed Dec 13, 2011
1 parent d571a24 commit 6d22071
Show file tree
Hide file tree
Showing 20 changed files with 314 additions and 786 deletions.
8 changes: 4 additions & 4 deletions library/Zend/Log/Writer.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,26 +34,26 @@ interface Writer
/**
* Add a log filter to the writer
*
* @param int|\Zend\Log\Filter $filter
* @param int|Filter $filter
* @return Writer
*/
public function addFilter($filter);

/**
* Set a message formatter for the writer
*
* @param \Zend\Log\Formatter|Callable $formatter
* @param Formatter $formatter
* @return Writer
*/
public function setFormatter(Formatter $formatter);

/**
* Write a log message
*
* @param array|mixed $event
* @param array $event
* @return Writer
*/
public function write($event);
public function write(array $event);

/**
* Perform shutdown activities
Expand Down
74 changes: 26 additions & 48 deletions library/Zend/Log/Writer/AbstractWriter.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,43 +24,40 @@
*/
namespace Zend\Log\Writer;

use Zend\Log\Factory,
Zend\Log\Writer,
use Zend\Log\Writer,
Zend\Log\Filter,
Zend\Log\Formatter,
Zend\Log\Exception,
Zend\Config\Config;
Zend\Log\Exception;

/**
* @uses \Zend\Log\Exception\InvalidArgumentException
* @uses \Zend\Log\Factory
* @uses \Zend\Log\Filter\Priority
* @category Zend
* @package Zend_Log
* @subpackage Writer
* @copyright Copyright (c) 2005-2011 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/
abstract class AbstractWriter implements Writer, Factory
abstract class AbstractWriter implements Writer
{
/**
* @var array of Filter
* Filter chain
*
* @var array
*/
protected $_filters = array();
protected $filters = array();

/**
* Formats the log message before writing.
* Formats the log message before writing
*
* @var Formatter
*/
protected $_formatter;
protected $formatter;

/**
* Add a filter specific to this writer.
*
* @param Filter|int $filter
* @param Filter|int $filter
* @return AbstractWriter
* @throws Exception\InvalidArgumentException
* @return self
*/
public function addFilter($filter)
{
Expand All @@ -69,29 +66,32 @@ public function addFilter($filter)
}

if (!$filter instanceof Filter) {
throw new Exception\InvalidArgumentException('Invalid filter provided');
throw new Exception\InvalidArgumentException(sprintf(
'Filter must implement Zend\Log\Filter; received %s',
is_object($filter) ? get_class($filter) : gettype($filter)
));
}

$this->_filters[] = $filter;
$this->filters[] = $filter;
return $this;
}

/**
* Log a message to this writer.
*
* @param array $event log data event
* @param array $event log data event
* @return void
*/
public function write($event)
public function write(array $event)
{
foreach ($this->_filters as $filter) {
if (! $filter->accept($event)) {
foreach ($this->filters as $filter) {
if (!$filter->filter($event)) {
return;
}
}

// exception occurs on error
$this->_write($event);
$this->doWrite($event);
}

/**
Expand All @@ -102,7 +102,7 @@ public function write($event)
*/
public function setFormatter(Formatter $formatter)
{
$this->_formatter = $formatter;
$this->formatter = $formatter;
return $this;
}

Expand All @@ -115,32 +115,10 @@ public function shutdown()
{}

/**
* Write a message to the log.
* Write a message to the log
*
* @param array $event log data event
* @param array $event log data event
* @return void
*/
abstract protected function _write($event);

/**
* Validate and optionally convert the config to array
*
* @param array|Config $config Config or Array
* @return array
* @throws Exception\InvalidArgumentException
*/
static protected function _parseConfig($config)
{
if ($config instanceof Config) {
$config = $config->toArray();
}

if (!is_array($config)) {
throw new Exception\InvalidArgumentException(
'Configuration must be an array or instance of Zend\Config\Config'
);
}

return $config;
}
}
abstract protected function doWrite(array $event);
}
64 changes: 19 additions & 45 deletions library/Zend/Log/Writer/Db.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,68 +45,42 @@ class Db extends AbstractWriter
*
* @var DbAdapter
*/
protected $_db;
protected $db;

/**
* Name of the log table in the database
*
* @var string
*/
protected $_table;
protected $table;

/**
* Relates database columns names to log data field keys.
*
* @var null|array
*/
protected $_columnMap;
protected $columnMap;

/**
* Class constructor
* Constructor
*
* @param DbAdapter $db Database adapter instance
* @param string $table Log table in database
* @param DbAdapter $db Database adapter instance
* @param string $table Log table in database
* @param array $columnMap
* @return void
* @return Db
*/
public function __construct($db, $table, $columnMap = null)
{
$this->_db = $db;
$this->_table = $table;
$this->_columnMap = $columnMap;
}

/**
* Create a new instance of Zend_Log_Writer_Db
*
* @param array|\Zend\Config\Config $config
* @return self
*/
static public function factory($config = array())
{
$config = self::_parseConfig($config);
$config = array_merge(array(
'db' => null,
'table' => null,
'columnMap' => null,
), $config);

if (isset($config['columnmap'])) {
$config['columnMap'] = $config['columnmap'];
}

return new self(
$config['db'],
$config['table'],
$config['columnMap']
);
$this->db = $db;
$this->table = $table;
$this->columnMap = $columnMap;
}

/**
* Formatting is not possible on this writer
*
* @throws Exception\InvalidArgumentException
* @return void
* @throws Exception\InvalidArgumentException
*/
public function setFormatter(Formatter $formatter)
{
Expand All @@ -120,31 +94,31 @@ public function setFormatter(Formatter $formatter)
*/
public function shutdown()
{
$this->_db = null;
$this->db = null;
}

/**
* Write a message to the log.
*
* @param array $event event data
* @throws Exception\RuntimeException
* @param array $event event data
* @return void
* @throws Exception\RuntimeException
*/
protected function _write($event)
protected function doWrite(array $event)
{
if ($this->_db === null) {
if (null === $this->db) {
throw new Exception\RuntimeException('Database adapter is null');
}

if ($this->_columnMap === null) {
if (null === $this->columnMap) {
$dataToInsert = $event;
} else {
$dataToInsert = array();
foreach ($this->_columnMap as $columnName => $fieldKey) {
foreach ($this->columnMap as $columnName => $fieldKey) {
$dataToInsert[$columnName] = $event[$fieldKey];
}
}

$this->_db->insert($this->_table, $dataToInsert);
$this->db->insert($this->table, $dataToInsert);
}
}
Loading

0 comments on commit 6d22071

Please sign in to comment.