log.class.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. <?php
  2. use Monolog\Logger;
  3. /**
  4. * Since static constructors do not exist in php the line below serves as a
  5. * substitute.
  6. *
  7. * Log is used by install which do not use autoload at the moment. If it becomes
  8. * the case then the line below may/should be moved to the main autoloading
  9. * function.
  10. */
  11. Log::register_autoload();
  12. /**
  13. * Provides access to the main log - i.e. stderr - and allows to register events.
  14. * It is a facade to the monolog library:
  15. *
  16. * Log::error('message');
  17. * Log::warning('message');
  18. * ...
  19. *
  20. *
  21. * Note:
  22. * This class uses a static approach which has the benefit of being simpler but do
  23. * no allow as much freedom as using an object approche. Another approach could be
  24. *
  25. * Chamilo::log()->error('message');
  26. * Chamilo::log()->warning('message');
  27. *
  28. * To somewhat alleviate this issue the user can register a different logger if hew
  29. * wants.
  30. *
  31. * @license see /license.txt
  32. * @author Laurent Opprecht <laurent@opprecht.info> for the Univesity of Geneva
  33. */
  34. class Log
  35. {
  36. public static function register_autoload()
  37. {
  38. static $has_run = false;
  39. if ($has_run) {
  40. return true;
  41. }
  42. /*$directory = api_get_path(LIBRARY_PATH) . 'symfony';
  43. if (!class_exists('Doctrine\Common\ClassLoader', false)) {
  44. require_once $directory . '/Doctrine/Common/ClassLoader.php';
  45. }
  46. $loader = new Doctrine\Common\ClassLoader('Monolog', $directory);
  47. $loader->register();*/
  48. $has_run = true;
  49. }
  50. /**
  51. *
  52. * @return \Monolog\Logger
  53. */
  54. public static function default_logger()
  55. {
  56. $name = 'chamilo';
  57. $result = new Logger($name);
  58. $handler = new Monolog\Handler\StreamHandler('php://stderr');
  59. $handler->setFormatter(new Monolog\Formatter\LineFormatter('[%datetime%] [%level_name%] [%channel%] %message%' . PHP_EOL, 'Y-m-d H:i:s')); //%context% %extra%
  60. $result->pushHandler($handler);
  61. return $result;
  62. }
  63. private static $logger = null;
  64. /**
  65. *
  66. * @return \Monolog\Logger
  67. */
  68. public static function logger()
  69. {
  70. if (empty(self::$logger)) {
  71. self::$logger = self::default_logger();
  72. }
  73. return self::$logger;
  74. }
  75. public static function set_logger($value)
  76. {
  77. self::$logger = $value;
  78. }
  79. /**
  80. * Returns the
  81. * @param type $index
  82. * @return type
  83. */
  84. public static function frame($index)
  85. {
  86. $result = debug_backtrace();
  87. return isset($result[$index]) ? $result[$index] : array();
  88. }
  89. public static function write($level, $message, $context = array())
  90. {
  91. /*
  92. * Note that the same could be done with a monolog processor.
  93. */
  94. if (!isset($context['file'])) {
  95. $trace = debug_backtrace();
  96. $trace = $trace[1];
  97. $context['file'] = $trace['file'];
  98. $context['line'] = $trace['line'];
  99. }
  100. $file = $context['file'];
  101. $root = realpath(api_get_path(SYS_PATH));
  102. $file = str_replace($root, '', $file);
  103. $file = trim($file, DIRECTORY_SEPARATOR);
  104. $line = $context['line'];
  105. $line = str_pad($line, 4, ' ', STR_PAD_LEFT);
  106. $message = "[$file:$line] " . $message;
  107. self::logger()->addRecord($level, $message, $context);
  108. }
  109. /**
  110. * Adds a log record at the DEBUG level.
  111. *
  112. * This method allows to have an easy ZF compatibility.
  113. *
  114. * @param string $message The log message
  115. * @param array $context The log context
  116. * @return Boolean Whether the record has been processed
  117. */
  118. public static function debug($message, array $context = array())
  119. {
  120. return self::write(Logger::DEBUG, $message, $context);
  121. }
  122. /**
  123. * Adds a log record at the INFO level.
  124. *
  125. * This method allows to have an easy ZF compatibility.
  126. *
  127. * @param string $message The log message
  128. * @param array $context The log context
  129. * @return Boolean Whether the record has been processed
  130. */
  131. public static function info($message, array $context = array())
  132. {
  133. // return self::write(Logger::INFO, self::message($message), $context);
  134. return self::write(Logger::INFO, $message, $context);
  135. }
  136. /**
  137. * Adds a log record at the INFO level.
  138. *
  139. * This method allows to have an easy ZF compatibility.
  140. *
  141. * @param string $message The log message
  142. * @param array $context The log context
  143. * @return Boolean Whether the record has been processed
  144. */
  145. public static function notice($message, array $context = array())
  146. {
  147. return self::write(Logger::INFO, $message, $context);
  148. }
  149. /**
  150. * Adds a log record at the WARNING level.
  151. *
  152. * This method allows to have an easy ZF compatibility.
  153. *
  154. * @param string $message The log message
  155. * @param array $context The log context
  156. * @return Boolean Whether the record has been processed
  157. */
  158. public static function warning($message, array $context = array())
  159. {
  160. return self::write(Logger::WARNING, $message, $context);
  161. }
  162. /**
  163. * Adds a log record at the ERROR level.
  164. *
  165. * This method allows to have an easy ZF compatibility.
  166. *
  167. * @param string $message The log message
  168. * @param array $context The log context
  169. * @return Boolean Whether the record has been processed
  170. */
  171. public static function error($message, array $context = array())
  172. {
  173. return self::write(Logger::ERROR, $message, $context);
  174. }
  175. /**
  176. * Adds a log record at the CRITICAL level.
  177. *
  178. * This method allows to have an easy ZF compatibility.
  179. *
  180. * @param string $message The log message
  181. * @param array $context The log context
  182. * @return Boolean Whether the record has been processed
  183. */
  184. public static function crit($message, array $context = array())
  185. {
  186. return self::write(Logger::CRITICAL, $message, $context);
  187. }
  188. /**
  189. * Adds a log record at the ALERT level.
  190. *
  191. * This method allows to have an easy ZF compatibility.
  192. *
  193. * @param string $message The log message
  194. * @param array $context The log context
  195. * @return Boolean Whether the record has been processed
  196. */
  197. public static function alert($message, array $context = array())
  198. {
  199. return self::write(Logger::ALERT, $message, $context);
  200. }
  201. }