[ZF-10089] Zend_Log
[zend.git] / library / Zend / Log / Writer / Db.php
blob4fc13a0e6f80915653115b5cba86cf9c123eb73f
1 <?php
2 /**
3 * Zend Framework
5 * LICENSE
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.
15 * @category Zend
16 * @package Zend_Log
17 * @subpackage Writer
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
20 * @version $Id$
23 /** Zend_Log_Writer_Abstract */
24 require_once 'Zend/Log/Writer/Abstract.php';
26 /**
27 * @category Zend
28 * @package Zend_Log
29 * @subpackage Writer
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
32 * @version $Id$
34 class Zend_Log_Writer_Db extends Zend_Log_Writer_Abstract
36 /**
37 * Database adapter instance
38 * @var Zend_Db_Adapter
40 private $_db;
42 /**
43 * Name of the log table in the database
44 * @var string
46 private $_table;
48 /**
49 * Relates database columns names to log data field keys.
51 * @var null|array
53 private $_columnMap;
55 /**
56 * Class constructor
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)
64 $this->_db = $db;
65 $this->_table = $table;
66 $this->_columnMap = $columnMap;
69 /**
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(
80 'db' => null,
81 'table' => null,
82 'columnMap' => null,
83 ), $config);
85 if (isset($config['columnmap'])) {
86 $config['columnMap'] = $config['columnmap'];
89 return new self(
90 $config['db'],
91 $config['table'],
92 $config['columnMap']
96 /**
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
108 * @return void
110 public function shutdown()
112 $this->_db = null;
116 * Write a message to the log.
118 * @param array $event event data
119 * @return void
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;
130 } else {
131 $dataToInsert = array();
132 foreach ($this->_columnMap as $columnName => $fieldKey) {
133 $dataToInsert[$columnName] = $event[$fieldKey];
137 $this->_db->insert($this->_table, $dataToInsert);