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.
17
* @copyright Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
18
* @license http://framework.zend.com/license/new-bsd New BSD License
19
* @version $Id: Log.php 9382 2008-05-05 18:55:55Z doctorrock83 $
22
/** Zend_Log_Filter_Priority */
23
require_once 'Zend/Log/Filter/Priority.php';
28
* @copyright Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
29
* @license http://framework.zend.com/license/new-bsd New BSD License
30
* @version $Id: Log.php 9382 2008-05-05 18:55:55Z doctorrock83 $
34
const EMERG = 0; // Emergency: system is unusable
35
const ALERT = 1; // Alert: action must be taken immediately
36
const CRIT = 2; // Critical: critical conditions
37
const ERR = 3; // Error: error conditions
38
const WARN = 4; // Warning: warning conditions
39
const NOTICE = 5; // Notice: normal but significant condition
40
const INFO = 6; // Informational: informational messages
41
const DEBUG = 7; // Debug: debug messages
44
* @var array of priorities where the keys are the
45
* priority numbers and the values are the priority names
47
protected $_priorities = array();
50
* @var array of Zend_Log_Writer_Abstract
52
protected $_writers = array();
55
* @var array of Zend_Log_Filter_Interface
57
protected $_filters = array();
60
* @var array of extra log event
62
protected $_extras = array();
65
* Class constructor. Create a new logger
67
* @param Zend_Log_Writer_Abstract|null $writer default writer
69
public function __construct(Zend_Log_Writer_Abstract $writer = null)
71
$r = new ReflectionClass($this);
72
$this->_priorities = array_flip($r->getConstants());
74
if ($writer !== null) {
75
$this->addWriter($writer);
80
* Class destructor. Shutdown log writers
84
public function __destruct()
86
foreach($this->_writers as $writer) {
92
* Undefined method handler allows a shortcut:
93
* $log->priorityName('message')
95
* $log->log('message', Zend_Log::PRIORITY_NAME)
97
* @param string $method priority name
98
* @param string $params message to log
100
* @throws Zend_Log_Exception
102
public function __call($method, $params)
104
$priority = strtoupper($method);
105
if (($priority = array_search($priority, $this->_priorities)) !== false) {
106
$this->log(array_shift($params), $priority);
108
/** @see Zend_Log_Exception */
109
require_once 'Zend/Log/Exception.php';
110
throw new Zend_Log_Exception('Bad log priority');
115
* Log a message at a priority
117
* @param string $message Message to log
118
* @param integer $priority Priority of message
120
* @throws Zend_Log_Exception
122
public function log($message, $priority)
125
if (empty($this->_writers)) {
126
/** @see Zend_Log_Exception */
127
require_once 'Zend/Log/Exception.php';
128
throw new Zend_Log_Exception('No writers were added');
131
if (! isset($this->_priorities[$priority])) {
132
/** @see Zend_Log_Exception */
133
require_once 'Zend/Log/Exception.php';
134
throw new Zend_Log_Exception('Bad log priority');
137
// pack into event required by filters and writers
138
$event = array_merge(array('timestamp' => date('c'),
139
'message' => $message,
140
'priority' => $priority,
141
'priorityName' => $this->_priorities[$priority]),
144
// abort if rejected by the global filters
145
foreach ($this->_filters as $filter) {
146
if (! $filter->accept($event)) {
151
// send to each writer
152
foreach ($this->_writers as $writer) {
153
$writer->write($event);
158
* Add a custom priority
160
* @param string $name Name of priority
161
* @param integer $priority Numeric priority
162
* @throws Zend_Log_InvalidArgumentException
164
public function addPriority($name, $priority)
166
// Priority names must be uppercase for predictability.
167
$name = strtoupper($name);
169
if (isset($this->_priorities[$priority])
170
|| array_search($name, $this->_priorities)) {
171
/** @see Zend_Log_Exception */
172
require_once 'Zend/Log/Exception.php';
173
throw new Zend_Log_Exception('Existing priorities cannot be overwritten');
176
$this->_priorities[$priority] = $name;
180
* Add a filter that will be applied before all log writers.
181
* Before a message will be received by any of the writers, it
182
* must be accepted by all filters added with this method.
184
* @param int|Zend_Log_Filter_Interface $filter
187
public function addFilter($filter)
189
if (is_integer($filter)) {
190
$filter = new Zend_Log_Filter_Priority($filter);
191
} elseif(!is_object($filter) || ! $filter instanceof Zend_Log_Filter_Interface) {
192
/** @see Zend_Log_Exception */
193
require_once 'Zend/Log/Exception.php';
194
throw new Zend_Log_Exception('Invalid filter provided');
197
$this->_filters[] = $filter;
201
* Add a writer. A writer is responsible for taking a log
202
* message and writing it out to storage.
204
* @param Zend_Log_Writer_Abstract $writer
207
public function addWriter(Zend_Log_Writer_Abstract $writer)
209
$this->_writers[] = $writer;
213
* Set an extra item to pass to the log writers.
215
* @param $name Name of the field
216
* @param $value Value of the field
219
public function setEventItem($name, $value) {
220
$this->_extras = array_merge($this->_extras, array($name => $value));