~chroot64bit/zivios/gentoo-experimental

« back to all changes in this revision

Viewing changes to application/library/Zend/Log.php

  • Committer: Mustafa A. Hashmi
  • Date: 2008-12-04 13:32:21 UTC
  • Revision ID: mhashmi@zivios.org-20081204133221-0nd1trunwevijj38
Inclusion of new installation framework with ties to zend layout and dojo layout

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
<?php
 
2
/**
 
3
 * Zend Framework
 
4
 *
 
5
 * LICENSE
 
6
 *
 
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.
 
14
 *
 
15
 * @category   Zend
 
16
 * @package    Zend_Log
 
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 $
 
20
 */
 
21
 
 
22
/** Zend_Log_Filter_Priority */
 
23
require_once 'Zend/Log/Filter/Priority.php';
 
24
 
 
25
/**
 
26
 * @category   Zend
 
27
 * @package    Zend_Log
 
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 $
 
31
 */
 
32
class Zend_Log
 
33
{
 
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
 
42
 
 
43
    /**
 
44
     * @var array of priorities where the keys are the
 
45
     * priority numbers and the values are the priority names
 
46
     */
 
47
    protected $_priorities = array();
 
48
 
 
49
    /**
 
50
     * @var array of Zend_Log_Writer_Abstract
 
51
     */
 
52
    protected $_writers = array();
 
53
 
 
54
    /**
 
55
     * @var array of Zend_Log_Filter_Interface
 
56
     */
 
57
    protected $_filters = array();
 
58
 
 
59
    /**
 
60
     * @var array of extra log event
 
61
     */
 
62
    protected $_extras = array();
 
63
 
 
64
    /**
 
65
     * Class constructor.  Create a new logger
 
66
     *
 
67
     * @param Zend_Log_Writer_Abstract|null  $writer  default writer
 
68
     */
 
69
    public function __construct(Zend_Log_Writer_Abstract $writer = null)
 
70
    {
 
71
        $r = new ReflectionClass($this);
 
72
        $this->_priorities = array_flip($r->getConstants());
 
73
 
 
74
        if ($writer !== null) {
 
75
            $this->addWriter($writer);
 
76
        }
 
77
    }
 
78
 
 
79
    /**
 
80
     * Class destructor.  Shutdown log writers
 
81
     *
 
82
     * @return void
 
83
     */
 
84
    public function __destruct()
 
85
    {
 
86
        foreach($this->_writers as $writer) {
 
87
            $writer->shutdown();
 
88
        }
 
89
    }
 
90
 
 
91
    /**
 
92
     * Undefined method handler allows a shortcut:
 
93
     *   $log->priorityName('message')
 
94
     *     instead of
 
95
     *   $log->log('message', Zend_Log::PRIORITY_NAME)
 
96
     *
 
97
     * @param  string  $method  priority name
 
98
     * @param  string  $params  message to log
 
99
     * @return void
 
100
     * @throws Zend_Log_Exception
 
101
     */
 
102
    public function __call($method, $params)
 
103
    {
 
104
        $priority = strtoupper($method);
 
105
        if (($priority = array_search($priority, $this->_priorities)) !== false) {
 
106
            $this->log(array_shift($params), $priority);
 
107
        } else {
 
108
            /** @see Zend_Log_Exception */
 
109
            require_once 'Zend/Log/Exception.php';
 
110
            throw new Zend_Log_Exception('Bad log priority');
 
111
        }
 
112
    }
 
113
 
 
114
    /**
 
115
     * Log a message at a priority
 
116
     *
 
117
     * @param  string   $message   Message to log
 
118
     * @param  integer  $priority  Priority of message
 
119
     * @return void
 
120
     * @throws Zend_Log_Exception
 
121
     */
 
122
    public function log($message, $priority)
 
123
    {
 
124
        // sanity checks
 
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');
 
129
        }
 
130
 
 
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');
 
135
        }
 
136
 
 
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]),
 
142
                              $this->_extras);
 
143
 
 
144
        // abort if rejected by the global filters
 
145
        foreach ($this->_filters as $filter) {
 
146
            if (! $filter->accept($event)) {
 
147
                return;
 
148
            }
 
149
        }
 
150
 
 
151
        // send to each writer
 
152
        foreach ($this->_writers as $writer) {
 
153
            $writer->write($event);
 
154
        }
 
155
    }
 
156
 
 
157
    /**
 
158
     * Add a custom priority
 
159
     *
 
160
     * @param  string   $name      Name of priority
 
161
     * @param  integer  $priority  Numeric priority
 
162
     * @throws Zend_Log_InvalidArgumentException
 
163
     */
 
164
    public function addPriority($name, $priority)
 
165
    {
 
166
        // Priority names must be uppercase for predictability.
 
167
        $name = strtoupper($name);
 
168
 
 
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');
 
174
        }
 
175
 
 
176
        $this->_priorities[$priority] = $name;
 
177
    }
 
178
 
 
179
    /**
 
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.
 
183
     *
 
184
     * @param  int|Zend_Log_Filter_Interface $filter
 
185
     * @return void
 
186
     */
 
187
    public function addFilter($filter)
 
188
    {
 
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');
 
195
        }
 
196
 
 
197
        $this->_filters[] = $filter;
 
198
    }
 
199
 
 
200
    /**
 
201
     * Add a writer.  A writer is responsible for taking a log
 
202
     * message and writing it out to storage.
 
203
     *
 
204
     * @param  Zend_Log_Writer_Abstract $writer
 
205
     * @return void
 
206
     */
 
207
    public function addWriter(Zend_Log_Writer_Abstract $writer)
 
208
    {
 
209
        $this->_writers[] = $writer;
 
210
    }
 
211
 
 
212
    /**
 
213
     * Set an extra item to pass to the log writers.
 
214
     *
 
215
     * @param  $name    Name of the field
 
216
     * @param  $value   Value of the field
 
217
     * @return void
 
218
     */
 
219
    public function setEventItem($name, $value) {
 
220
        $this->_extras = array_merge($this->_extras, array($name => $value));
 
221
    }
 
222
 
 
223
}