PageRenderTime 63ms CodeModel.GetById 28ms RepoModel.GetById 0ms app.codeStats 0ms

/inc/api/log.php

https://github.com/jizillon/phpzillon-browser
PHP | 181 lines | 114 code | 30 blank | 37 comment | 19 complexity | 01e77d6877509ded24dc09b6c3c20f5d MD5 | raw file
  1. <?php
  2. /* Licensed under the Apache License, Version 2.0
  3. * See the LICENSE and NOTICE file for further information
  4. */
  5. /**
  6. * Wrapper class for Zend_Log which reads configuration from api_config
  7. * and creates the corresponding Log objects.
  8. *
  9. * The configured logger is available through api_log::$logger.
  10. */
  11. class api_log {
  12. const EMERG = 0; // Emergency: system is unusable
  13. const ALERT = 1; // Alert: action must be taken immediately
  14. const CRIT = 2; // Critical: critical conditions
  15. const ERR = 3; // Error: error conditions
  16. const WARN = 4; // Warning: warning conditions
  17. const NOTICE = 5; // Notice: normal but significant condition
  18. const INFO = 6; // Informational: informational messages
  19. const DEBUG = 7; // Debug: debug messages
  20. /** Log: Configured logger. */
  21. public static $logger = null;
  22. /** api_log instance */
  23. public static $instance = null;
  24. /** lowest priority **/
  25. protected $priority = null;
  26. /** default priority **/
  27. protected static $defaultPriority = self::ERR;
  28. /** MockWriter for testing */
  29. public static $mockWriter = null;
  30. /**
  31. * Log a message if a logger is configured
  32. */
  33. public static function log($prio) {
  34. if (self::$instance === false) {
  35. return false;
  36. }
  37. if (self::$instance === null) {
  38. $config = api_config::getInstance()->log;
  39. if (empty($config) || !(self::$instance = api_log::getInstance())) {
  40. self::$instance = false;
  41. return false;
  42. }
  43. }
  44. $params = func_get_args();
  45. array_shift($params);
  46. return self::$instance->logMessage($params, $prio);
  47. }
  48. /**
  49. * Log a message if a logger is configured without having to give a priority
  50. * Only one parameter is supported, if you need more arguments, use api_log::log();
  51. *
  52. * @param $message The message to be logged
  53. */
  54. public static function dump($message) {
  55. return self::log(self::$defaultPriority, $message);
  56. }
  57. /**
  58. * Initialize the logger.
  59. */
  60. public function __construct() {
  61. if (self::$logger !== null) {
  62. // Already initialized
  63. return;
  64. }
  65. $configs = api_config::getInstance()->log;
  66. if (empty($configs[0]['class'])) {
  67. // Logging is not activated
  68. self::$logger = false;
  69. return;
  70. }
  71. self::$logger = new Zend_Log();
  72. foreach ($configs as $cfg) {
  73. $log = $this->createLogObject($cfg['class'], $cfg);
  74. if ($cfg['class'] == 'Writer_Mock') {
  75. self::$mockWriter = $log;
  76. }
  77. self::$logger->addWriter($log);
  78. }
  79. }
  80. /**
  81. * Gets an instance of api_log.
  82. * @param $forceReload bool: If true, forces instantiation of a
  83. * new instance. Used for testing.
  84. * @return api_log an api_log instance;
  85. */
  86. public static function getInstance($forceReload = FALSE) {
  87. if (!self::$instance instanceof api_log || $forceReload) {
  88. self::$logger = null;
  89. self::$instance = new api_log();
  90. }
  91. return self::$instance;
  92. }
  93. public function __call($method, $params) {
  94. $prio = self::getMaskFromLevel($method);
  95. $this->logMessage($params, $prio);
  96. }
  97. public function isLogging() {
  98. return self::$instance !== false && self::$logger !== false;
  99. }
  100. public function getPriority() {
  101. return $this->priority;
  102. }
  103. protected function createLogObject($name, $config) {
  104. $classname = 'Zend_Log_' . $name;
  105. $params = (array) (isset($config['cfg']) ? $config['cfg'] : array());
  106. if (empty($params)) {
  107. $object = new $classname();
  108. } else {
  109. $class = new ReflectionClass($classname);
  110. $object = $class->newInstanceArgs($params);
  111. }
  112. if (isset($config['priority'])) {
  113. $prio = $this->getMaskFromLevel($config['priority']);
  114. $object->addFilter(new Zend_Log_Filter_Priority($prio));
  115. if ($prio > $this->priority || !$this->priority) {
  116. $this->priority = $prio;
  117. }
  118. }
  119. return $object;
  120. }
  121. /**
  122. * Return a api_log mask for a string level.
  123. */
  124. protected function getMaskFromLevel($level) {
  125. $masks = array(
  126. 'EMERG' => api_log::EMERG,
  127. 'ALERT' => api_log::ALERT,
  128. 'CRIT' => api_log::CRIT,
  129. 'FATAL' => Zend_Log::CRIT,
  130. 'ERR' => api_log::ERR,
  131. 'ERROR' => Zend_Log::ERR,
  132. 'WARN' => api_log::WARN,
  133. 'NOTICE' => api_log::NOTICE,
  134. 'INFO' => api_log::INFO,
  135. 'DEBUG' => api_log::DEBUG);
  136. return $masks[strtoupper($level)];
  137. }
  138. protected function logMessage($params, $prio) {
  139. if (self::$logger === false) {
  140. return false;
  141. }
  142. $message = array_shift($params);
  143. if (!empty($params)) {
  144. $message = vsprintf($message, $params);
  145. }
  146. if (!is_int($prio)) {
  147. $prio = self::$defaultPriority;
  148. }
  149. return self::$logger->log($message, $prio);
  150. }
  151. }