PageRenderTime 30ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/src/DocBlox/Core/Log.php

http://github.com/mvriel/Docblox
PHP | 196 lines | 91 code | 28 blank | 77 comment | 11 complexity | 07ccbc7bc06db5f753eba53f7fa946f9 MD5 | raw file
Possible License(s): LGPL-3.0, BSD-3-Clause, CC-BY-SA-3.0
  1. <?php
  2. /**
  3. * DocBlox
  4. *
  5. * PHP Version 5
  6. *
  7. * @category DocBlox
  8. * @package Core
  9. * @author Mike van Riel <mike.vanriel@naenius.com>
  10. * @copyright 2010-2011 Mike van Riel / Naenius (http://www.naenius.com)
  11. * @license http://www.opensource.org/licenses/mit-license.php MIT
  12. * @link http://docblox-project.org
  13. */
  14. /**
  15. * Logger used to capture any messages via any stream.
  16. *
  17. * @category DocBlox
  18. * @package Core
  19. * @author Mike van Riel <mike.vanriel@naenius.com>
  20. * @license http://www.opensource.org/licenses/mit-license.php MIT
  21. * @link http://docblox-project.org
  22. */
  23. class DocBlox_Core_Log
  24. {
  25. /** @var string Emergency: system is unstable */
  26. const EMERG = Zend_Log::EMERG;
  27. /** @var string Alert: action must be taken immediately */
  28. const ALERT = Zend_Log::ALERT;
  29. /** @var string Critical: critical conditions */
  30. const CRIT = Zend_Log::CRIT;
  31. /** @var string Error: error conditions */
  32. const ERR = Zend_Log::ERR;
  33. /** @var string Warning: warning conditions */
  34. const WARN = Zend_Log::WARN;
  35. /** @var string Notice: normal but significant condition */
  36. const NOTICE = Zend_Log::NOTICE;
  37. /** @var string Informational: informational messages */
  38. const INFO = Zend_Log::INFO;
  39. /** @var string Debug: debug messages */
  40. const DEBUG = Zend_Log::DEBUG;
  41. /** @var string Quiet: disables logging */
  42. const QUIET = -1;
  43. /** @var string Output will only be sent to stdout */
  44. const FILE_STDOUT = 'php://stdout';
  45. /** @var int Only log messages that equal or exceed this. */
  46. protected $threshold = self::DEBUG;
  47. /** @var string The name of the file/stream where the logs are written to. */
  48. protected $filename = '';
  49. /** @var Zend_Log The logger to use for storing information. */
  50. protected $logger = null;
  51. /**
  52. * Initialize the logger.
  53. *
  54. * @param string $file May also be the FILE_STDOUT constant to output to STDOUT.
  55. */
  56. public function __construct($file)
  57. {
  58. // only do the file checks if it is an actual file.
  59. if ($file !== self::FILE_STDOUT) {
  60. // replace APP_ROOT and DATE variables
  61. $file = str_replace(
  62. array(
  63. '{APP_ROOT}',
  64. '{DATE}'
  65. ),
  66. array(
  67. DocBlox_Core_Abstract::config()->paths->application,
  68. date('YmdHis')
  69. ),
  70. $file
  71. );
  72. // check if the given file location is writable; if not: output an error
  73. if (!is_writeable(dirname($file))) {
  74. $this->logger = new Zend_Log(new Zend_Log_Writer_Null());
  75. $this->log(
  76. 'The log directory does not appear to be writable; tried '
  77. . 'to log to: ' . $file . ', disabled logging to file',
  78. self::ERR
  79. );
  80. $this->filename = null;
  81. return;
  82. }
  83. }
  84. $this->filename = $file;
  85. $this->logger = new Zend_Log(new Zend_Log_Writer_Stream(fopen($file, 'w')));
  86. }
  87. /**
  88. * Returns the name of the file/stream where the output is written to or
  89. * null if it is send to the void.
  90. *
  91. * @return null|string
  92. */
  93. public function getFilename()
  94. {
  95. return $this->filename;
  96. }
  97. /**
  98. * Sets the logging threshold; anything more detailed than the given level
  99. * will not be logged.
  100. *
  101. * @param int $threshold The min level that will be logged.
  102. *
  103. * @return void
  104. */
  105. public function setThreshold($threshold)
  106. {
  107. if (is_object($threshold) && (get_class($threshold) === 'sfEvent')) {
  108. $threshold = $threshold[0];
  109. }
  110. if (!is_numeric($threshold)) {
  111. if (!defined('DocBlox_Core_Log::' . strtoupper($threshold))) {
  112. throw new InvalidArgumentException(
  113. 'Expected one of the constants of the DocBlox_Core_Log class, '
  114. . '"' . $threshold . '" received'
  115. );
  116. }
  117. $constant = 'DocBlox_Core_Log::' . strtoupper($threshold);
  118. $threshold = constant($constant);
  119. }
  120. $this->threshold = $threshold;
  121. }
  122. /**
  123. * Returns the threshold for this logger.
  124. *
  125. * @return int
  126. */
  127. public function getThreshold()
  128. {
  129. return $this->threshold;
  130. }
  131. /**
  132. * Log the given data; if it is something else than a string it will be
  133. * var_dumped and then logged.
  134. *
  135. * @param mixed $data The data to log.
  136. * @param int|string $level The level of the message to log.
  137. *
  138. * @return void
  139. */
  140. public function log($data, $level = self::INFO)
  141. {
  142. // we explicitly use the get_class method to prevent a hard dependency
  143. // to the sfEvent class; this way the connection is implicit and doesn't
  144. // it matter to DocBlox_Core_Log whether it is loaded or not.
  145. if (is_object($data) && (get_class($data) === 'sfEvent')) {
  146. // if this is an event; replace our data to cope with that
  147. $level = $data['priority'];
  148. $data = $data['message'];
  149. }
  150. // is the log level is below the priority; just skip this
  151. if ($this->getThreshold() < $level) {
  152. return;
  153. }
  154. // if the given is not a string then we var dump the object|array to
  155. // inspect it
  156. if (!is_string($data)) {
  157. ob_start();
  158. var_dump($data);
  159. $data = ob_get_clean();
  160. }
  161. $memory = number_format(round(memory_get_usage() / 1024 / 1024, 2), 2);
  162. $data = (($this->getThreshold() == Zend_Log::DEBUG)
  163. ? '[' . $memory . 'mb]: '
  164. : '')
  165. . $data;
  166. $this->logger->log($data, $level);
  167. }
  168. }