PageRenderTime 26ms CodeModel.GetById 18ms app.highlight 6ms RepoModel.GetById 1ms app.codeStats 0ms

/administrator/components/com_zoo/framework/helpers/log.php

https://bitbucket.org/organicdevelopment/joomla-2.5
PHP | 213 lines | 57 code | 28 blank | 128 comment | 5 complexity | 425b5b2aa5124a7b1af22b6cffdb73ce MD5 | raw file
  1<?php
  2/**
  3* @package   com_zoo
  4* @author    YOOtheme http://www.yootheme.com
  5* @copyright Copyright (C) YOOtheme GmbH
  6* @license   http://www.gnu.org/licenses/gpl.html GNU/GPL
  7*/
  8
  9/**
 10 * Log helper class
 11 * 
 12 * @package Framework.Helpers
 13 */
 14class LogHelper extends AppHelper {
 15
 16	/**
 17	 * The event to trigger when loggin
 18	 * 
 19	 * @var string
 20	 * @since 1.0.0
 21	 */
 22	protected $_event = 'app:log';
 23
 24	/**
 25	 * Class Constructor
 26	 * 
 27	 * @param App $app A reference to the global App object
 28	 */
 29	public function __construct($app) {
 30		parent::__construct($app);
 31
 32		// load class
 33		$this->app->loader->register('AppLogger', 'classes:logger.php');
 34	}
 35
 36	/**
 37	 * Log a success message
 38	 * 
 39	 * @param string $message The message to log
 40	 * @param string $type The type of the message
 41	 * 
 42	 * @see _notify
 43	 * 
 44	 * @since 1.0.0
 45	 */
 46	public function success($message, $type = null) {
 47		$this->_notify(AppLogger::LEVEL_SUCCESS, $message, $type);
 48	}
 49
 50	/**
 51	 * Log an info message
 52	 * 
 53	 * @param string $message The message to log
 54	 * @param string $type The type of the message
 55	 * 
 56	 * @see _notify
 57	 * 
 58	 * @since 1.0.0
 59	 */
 60	public function info($message, $type = null) {
 61		$this->_notify(AppLogger::LEVEL_INFO, $message, $type);
 62	}
 63
 64	/**
 65	 * Log a notice message
 66	 * 
 67	 * @param string $message The message to log
 68	 * @param string $type The type of the message
 69	 * 
 70	 * @see _notify
 71	 * 
 72	 * @since 1.0.0
 73	 */
 74	public function notice($message, $type = null) {
 75		$this->_notify(AppLogger::LEVEL_NOTICE, $message, $type);
 76	}
 77
 78	/**
 79	 * Log a warning message
 80	 * 
 81	 * @param string $message The message to log
 82	 * @param string $type The type of the message
 83	 * 
 84	 * @see _notify
 85	 * 
 86	 * @since 1.0.0
 87	 */
 88	public function warning($message, $type = null) {
 89		$this->_notify(AppLogger::LEVEL_WARNING, $message, $type);
 90	}
 91
 92	/**
 93	 * Log an error message
 94	 * 
 95	 * @param string $message The message to log
 96	 * @param string $type The type of the message
 97	 * 
 98	 * @see _notify
 99	 * 
100	 * @since 1.0.0
101	 */
102	public function error($message, $type = null) {
103		$this->_notify(AppLogger::LEVEL_ERROR, $message, $type);
104	}
105
106	/**
107	 * Log a debug message
108	 * 
109	 * @param string $message The message to log
110	 * @param string $type The type of the message
111	 * 
112	 * @see _notify
113	 * 
114	 * @since 1.0.0
115	 */
116	public function debug($message, $type = null) {
117		$this->_notify(AppLogger::LEVEL_DEBUG, $message, $type);
118	}
119
120	/**
121	 * Create a logger object
122	 * 
123	 * @param string $type The type of logger to create
124	 * @param array $args The parameters to pass to the logger class
125	 * 
126	 * @return AppLogger The logger object
127	 * 
128	 * @since 1.0.0
129	 */
130	public function createLogger($type, $args = array()) {
131		
132		// load data class
133		$class = $type.'Logger';
134		$this->app->loader->register($class, 'loggers:'.strtolower($type).'.php');
135
136		// use reflection for logger creation
137		if (count($args) > 0) {
138			$reflection = new ReflectionClass($class);
139			$logger = $reflection->newInstanceArgs($args);
140		} else {
141			$logger = new $class();
142		}
143
144		return $this->addLogger($logger);
145	}
146
147	/**
148	 * Connect the logger object to the log event
149	 * 
150	 * @param AppLogger $logger The logger object
151	 * 
152	 * @return AppLogger The logger object
153	 * 
154	 * @since 1.0.0
155	 */
156	public function addLogger($logger) {
157		
158		// set app
159		$logger->app = $this->app;
160		
161		// add logger to application log event
162		$this->app->event->dispatcher->connect($this->_event, array($logger, 'listen'));
163
164		return $logger;
165	}
166
167	/**
168	 * Disconnect the logger object from the event
169	 * 
170	 * @param AppLogger $logger The logger object
171	 * 
172	 * @return AppLogger The logger object
173	 * 
174	 * @since 1.0.0
175	 */
176	public function removeLogger($logger) {
177		
178		// remove logger from application log event
179		$this->app->event->dispatcher->disconnect($this->_event, array($logger, 'listen'));
180
181		return $logger;
182	}
183
184	/**
185	 * Trigger the event with the log message
186	 * 
187	 * @param int $level The level of the log
188	 * @param string $message The log message
189	 * @param string $type The type of the log
190	 * 
191	 * @since 1.0.0
192	 */
193	protected function _notify($level, $message, $type = null) {
194
195		// auto-detect type
196		if ($type == null) {
197			
198			// get backtrace
199			$backtrace = debug_backtrace();
200			if (isset($backtrace[2]['class'])) {
201				$type = $backtrace[2]['class'];
202			} elseif (isset($backtrace[2]['object'])) {
203				$type = get_class($backtrace[2]['object']);
204			}
205
206		}
207
208		// fire event
209	    $this->app->event->dispatcher->notify($this->app->event->create($this, $this->_event, compact('level', 'message', 'type')));
210		
211	}
212
213}