Development

/branches/1.2/lib/log/sfVarLogger.class.php

You must first sign up to be able to contribute.

root/branches/1.2/lib/log/sfVarLogger.class.php

Revision 26989, 4.3 kB (checked in by FabianLange, 5 years ago)

[1.2, 1.3, 1.4] fixed typo in getting Priorities from sfVarLogger (fixes #7938)

  • Property svn:mime-type set to text/x-php
  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
Line 
1 <?php
2
3 /*
4  * This file is part of the symfony package.
5  * (c) Fabien Potencier <fabien.potencier@symfony-project.com>
6  *
7  * For the full copyright and license information, please view the LICENSE
8  * file that was distributed with this source code.
9  */
10
11 /**
12  * sfVarLogger logs messages within its instance for later use.
13  *
14  * @package    symfony
15  * @subpackage log
16  * @author     Fabien Potencier <fabien.potencier@symfony-project.com>
17  * @version    SVN: $Id$
18  */
19 class sfVarLogger extends sfLogger
20 {
21   protected
22     $logs          = array(),
23     $xdebugLogging = false;
24
25   /**
26    * Initializes this logger.
27    *
28    * Available options:
29    *
30    * - xdebug_logging: Whether to add xdebug trace to the logs (false by default).
31    *
32    * @param  sfEventDispatcher $dispatcher  A sfEventDispatcher instance
33    * @param  array             $options     An array of options.
34    *
35    * @return Boolean           true, if initialization completes successfully, otherwise false.
36    */
37   public function initialize(sfEventDispatcher $dispatcher, $options = array())
38   {
39     $this->xdebugLogging = isset($options['xdebug_logging']) ? $options['xdebug_logging'] : false;
40
41     // disable xdebug when an HTTP debug session exists (crashes Apache, see #2438)
42     if (isset($_GET['XDEBUG_SESSION_START']) || isset($_COOKIE['XDEBUG_SESSION']))
43     {
44       $this->xdebugLogging = false;
45     }
46
47     return parent::initialize($dispatcher, $options);
48   }
49
50   /**
51    * Gets the logs.
52    *
53    * Each log entry has the following attributes:
54    *
55    *  * priority
56    *  * time   
57    *  * message
58    *  * type
59    *  * debugStack
60    *
61    * @return array An array of logs
62    */
63   public function getLogs()
64   {
65     return $this->logs;
66   }
67
68   /**
69    * Returns all the types in the logs.
70    *
71    * @return array An array of types
72    */
73   public function getTypes()
74   {
75     $types = array();
76     foreach ($this->logs as $log)
77     {
78       if (!in_array($log['type'], $types))
79       {
80         $types[] = $log['type'];
81       }
82     }
83
84     sort($types);
85
86     return $types;
87   }
88
89   /**
90    * Returns all the priorities in the logs.
91    *
92    * @return array An array of priorities
93    */
94   public function getPriorities()
95   {
96     $priorities = array();
97     foreach ($this->logs as $log)
98     {
99       if (!in_array($log['priority'], $priorities))
100       {
101         $priorities[] = $log['priority'];
102       }
103     }
104
105     sort($priorities);
106
107     return $priorities;
108   }
109
110   /**
111    * Returns the highest priority in the logs.
112    *
113    * @return integer The highest priority
114    */
115   public function getHighestPriority()
116   {
117     $priority = 1000;
118     foreach ($this->logs as $log)
119     {
120       if ($log['priority'] < $priority)
121       {
122         $priority = $log['priority'];
123       }
124     }
125
126     return $priority;
127   }
128
129   /**
130    * Logs a message.
131    *
132    * @param string $message   Message
133    * @param string $priority  Message priority
134    */
135   protected function doLog($message, $priority)
136   {
137     // get log type in {}
138     $type = 'sfOther';
139     if (preg_match('/^\s*{([^}]+)}\s*(.+?)$/s', $message, $matches))
140     {
141       $type    = $matches[1];
142       $message = $matches[2];
143     }
144
145     $this->logs[] = array(
146       'priority'      => $priority,
147       'priority_name' => $this->getPriorityName($priority),
148       'time'          => time(),
149       'message'       => $message,
150       'type'          => $type,
151       'debug_stack'   => $this->getXDebugStack(),
152     );
153   }
154
155   /**
156    * Returns the xdebug stack.
157    *
158    * @return array The xdebug stack as an array
159    */
160   protected function getXDebugStack()
161   {
162     // if we have xdebug and dev has not disabled the feature, add some stack information
163     if (!$this->xdebugLogging || !function_exists('xdebug_get_function_stack'))
164     {
165       return array();
166     }
167
168     $debugStack = array();
169     foreach (xdebug_get_function_stack() as $i => $stack)
170     {
171       if (
172         (isset($stack['function']) && !in_array($stack['function'], array('emerg', 'alert', 'crit', 'err', 'warning', 'notice', 'info', 'debug', 'log')))
173         || !isset($stack['function'])
174       )
175       {
176         $tmp = '';
177         if (isset($stack['function']))
178         {
179           $tmp .= sprintf('in "%s" ', $stack['function']);
180         }
181         $tmp .= sprintf('from "%s" line %s', $stack['file'], $stack['line']);
182         $debugStack[] = $tmp;
183       }
184     }
185
186     return $debugStack;
187   }
188 }
189
Note: See TracBrowser for help on using the browser.