10 defined(
'JPATH_PLATFORM') or die;
102 protected $configurations = array();
109 protected $loggers = array();
116 protected $lookup = array();
123 protected function __construct()
139 public static function add($entry, $priority = self::INFO, $category =
'', $date = null)
142 if (empty(self::$instance))
144 self::setInstance(
new JLog);
150 $entry =
new JLogEntry((
string) $entry, $priority, $category, $date);
153 self::$instance->addLogEntry($entry);
168 public static function addLogger(array $options, $priorities = self::ALL, $categories = array(), $exclude =
false)
171 if (empty(self::$instance))
173 self::setInstance(
new JLog);
177 if (empty($options[
'logger']))
179 $options[
'logger'] =
'formattedtext';
181 $options[
'logger'] = strtolower($options[
'logger']);
185 if (isset($options[
'callback']) && is_a($options[
'callback'],
'closure'))
187 $callback = $options[
'callback'];
188 $options[
'callback'] = spl_object_hash($options[
'callback']);
192 $signature = md5(serialize($options));
195 if (isset($callback))
197 $options[
'callback'] = $callback;
201 if (empty(self::$instance->configurations[$signature]))
203 self::$instance->configurations[$signature] = $options;
206 self::$instance->lookup[$signature] = (object) array(
207 'priorities' => $priorities,
208 'categories' => array_map(
'strtolower', (array) $categories),
209 'exclude' => (bool) $exclude);
222 public static function setInstance($instance)
224 if (($instance instanceof JLog) || $instance === null)
226 self::$instance = & $instance;
243 $loggers = $this->findLoggers($entry->priority, $entry->category);
245 foreach ((array) $loggers as $signature)
248 if (empty($this->loggers[$signature]))
251 $class =
'JLogLogger' . ucfirst($this->configurations[$signature][
'logger']);
253 if (class_exists($class))
255 $this->loggers[$signature] =
new $class($this->configurations[$signature]);
259 throw new RuntimeException(
'Unable to create a JLogLogger instance: ' . $class);
264 $this->loggers[$signature]->addEntry(clone($entry));
278 protected function findLoggers($priority, $category)
283 $priority = (int) $priority;
284 $category = strtolower($category);
287 foreach ((array) $this->lookup as $signature => $rules)
290 if ($priority & $rules->priorities)
295 if (empty($rules->categories) || !in_array($category, $rules->categories))
297 $loggers[] = $signature;
303 if (empty($category) || empty($rules->categories) || in_array($category, $rules->categories))
305 $loggers[] = $signature;