PageRenderTime 40ms CodeModel.GetById 16ms 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
Possible License(s): LGPL-3.0, GPL-2.0, MIT, BSD-3-Clause, LGPL-2.1
  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. * Log helper class
  10. *
  11. * @package Framework.Helpers
  12. */
  13. class LogHelper extends AppHelper {
  14. /**
  15. * The event to trigger when loggin
  16. *
  17. * @var string
  18. * @since 1.0.0
  19. */
  20. protected $_event = 'app:log';
  21. /**
  22. * Class Constructor
  23. *
  24. * @param App $app A reference to the global App object
  25. */
  26. public function __construct($app) {
  27. parent::__construct($app);
  28. // load class
  29. $this->app->loader->register('AppLogger', 'classes:logger.php');
  30. }
  31. /**
  32. * Log a success message
  33. *
  34. * @param string $message The message to log
  35. * @param string $type The type of the message
  36. *
  37. * @see _notify
  38. *
  39. * @since 1.0.0
  40. */
  41. public function success($message, $type = null) {
  42. $this->_notify(AppLogger::LEVEL_SUCCESS, $message, $type);
  43. }
  44. /**
  45. * Log an info message
  46. *
  47. * @param string $message The message to log
  48. * @param string $type The type of the message
  49. *
  50. * @see _notify
  51. *
  52. * @since 1.0.0
  53. */
  54. public function info($message, $type = null) {
  55. $this->_notify(AppLogger::LEVEL_INFO, $message, $type);
  56. }
  57. /**
  58. * Log a notice message
  59. *
  60. * @param string $message The message to log
  61. * @param string $type The type of the message
  62. *
  63. * @see _notify
  64. *
  65. * @since 1.0.0
  66. */
  67. public function notice($message, $type = null) {
  68. $this->_notify(AppLogger::LEVEL_NOTICE, $message, $type);
  69. }
  70. /**
  71. * Log a warning message
  72. *
  73. * @param string $message The message to log
  74. * @param string $type The type of the message
  75. *
  76. * @see _notify
  77. *
  78. * @since 1.0.0
  79. */
  80. public function warning($message, $type = null) {
  81. $this->_notify(AppLogger::LEVEL_WARNING, $message, $type);
  82. }
  83. /**
  84. * Log an error message
  85. *
  86. * @param string $message The message to log
  87. * @param string $type The type of the message
  88. *
  89. * @see _notify
  90. *
  91. * @since 1.0.0
  92. */
  93. public function error($message, $type = null) {
  94. $this->_notify(AppLogger::LEVEL_ERROR, $message, $type);
  95. }
  96. /**
  97. * Log a debug message
  98. *
  99. * @param string $message The message to log
  100. * @param string $type The type of the message
  101. *
  102. * @see _notify
  103. *
  104. * @since 1.0.0
  105. */
  106. public function debug($message, $type = null) {
  107. $this->_notify(AppLogger::LEVEL_DEBUG, $message, $type);
  108. }
  109. /**
  110. * Create a logger object
  111. *
  112. * @param string $type The type of logger to create
  113. * @param array $args The parameters to pass to the logger class
  114. *
  115. * @return AppLogger The logger object
  116. *
  117. * @since 1.0.0
  118. */
  119. public function createLogger($type, $args = array()) {
  120. // load data class
  121. $class = $type.'Logger';
  122. $this->app->loader->register($class, 'loggers:'.strtolower($type).'.php');
  123. // use reflection for logger creation
  124. if (count($args) > 0) {
  125. $reflection = new ReflectionClass($class);
  126. $logger = $reflection->newInstanceArgs($args);
  127. } else {
  128. $logger = new $class();
  129. }
  130. return $this->addLogger($logger);
  131. }
  132. /**
  133. * Connect the logger object to the log event
  134. *
  135. * @param AppLogger $logger The logger object
  136. *
  137. * @return AppLogger The logger object
  138. *
  139. * @since 1.0.0
  140. */
  141. public function addLogger($logger) {
  142. // set app
  143. $logger->app = $this->app;
  144. // add logger to application log event
  145. $this->app->event->dispatcher->connect($this->_event, array($logger, 'listen'));
  146. return $logger;
  147. }
  148. /**
  149. * Disconnect the logger object from the event
  150. *
  151. * @param AppLogger $logger The logger object
  152. *
  153. * @return AppLogger The logger object
  154. *
  155. * @since 1.0.0
  156. */
  157. public function removeLogger($logger) {
  158. // remove logger from application log event
  159. $this->app->event->dispatcher->disconnect($this->_event, array($logger, 'listen'));
  160. return $logger;
  161. }
  162. /**
  163. * Trigger the event with the log message
  164. *
  165. * @param int $level The level of the log
  166. * @param string $message The log message
  167. * @param string $type The type of the log
  168. *
  169. * @since 1.0.0
  170. */
  171. protected function _notify($level, $message, $type = null) {
  172. // auto-detect type
  173. if ($type == null) {
  174. // get backtrace
  175. $backtrace = debug_backtrace();
  176. if (isset($backtrace[2]['class'])) {
  177. $type = $backtrace[2]['class'];
  178. } elseif (isset($backtrace[2]['object'])) {
  179. $type = get_class($backtrace[2]['object']);
  180. }
  181. }
  182. // fire event
  183. $this->app->event->dispatcher->notify($this->app->event->create($this, $this->_event, compact('level', 'message', 'type')));
  184. }
  185. }