7 * This source file is subject to the new BSD license that is bundled
8 * with this package in the file LICENSE.txt.
9 * It is also available through the world-wide-web at this URL:
10 * http://framework.zend.com/license/new-bsd
11 * If you did not receive a copy of the license and are unable to
12 * obtain it through the world-wide-web, please send an email
13 * to license@zend.com so we can send you a copy immediately.
18 * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
19 * @license http://framework.zend.com/license/new-bsd New BSD License
23 /** Zend_Log_Writer_Abstract */
24 require_once 'Zend/Log/Writer/Abstract.php';
30 * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
31 * @license http://framework.zend.com/license/new-bsd New BSD License
34 class Zend_Log_Writer_Db
extends Zend_Log_Writer_Abstract
37 * Database adapter instance
38 * @var Zend_Db_Adapter
43 * Name of the log table in the database
49 * Relates database columns names to log data field keys.
58 * @param Zend_Db_Adapter $db Database adapter instance
59 * @param string $table Log table in database
60 * @param array $columnMap
62 public function __construct($db, $table, $columnMap = null)
65 $this->_table
= $table;
66 $this->_columnMap
= $columnMap;
70 * Create a new instance of Zend_Log_Writer_Db
72 * @param array|Zend_Config $config
73 * @return Zend_Log_Writer_Db
74 * @throws Zend_Log_Exception
76 static public function factory($config)
78 $config = self
::_parseConfig($config);
79 $config = array_merge(array(
85 if (isset($config['columnmap'])) {
86 $config['columnMap'] = $config['columnmap'];
97 * Formatting is not possible on this writer
99 public function setFormatter(Zend_Log_Formatter_Interface
$formatter)
101 require_once 'Zend/Log/Exception.php';
102 throw new Zend_Log_Exception(get_class($this) . ' does not support formatting');
106 * Remove reference to database adapter
110 public function shutdown()
116 * Write a message to the log.
118 * @param array $event event data
121 protected function _write($event)
123 if ($this->_db
=== null) {
124 require_once 'Zend/Log/Exception.php';
125 throw new Zend_Log_Exception('Database adapter is null');
128 if ($this->_columnMap
=== null) {
129 $dataToInsert = $event;
131 $dataToInsert = array();
132 foreach ($this->_columnMap
as $columnName => $fieldKey) {
133 $dataToInsert[$columnName] = $event[$fieldKey];
137 $this->_db
->insert($this->_table
, $dataToInsert);