PageRenderTime 49ms CodeModel.GetById 26ms RepoModel.GetById 0ms app.codeStats 0ms

/script/lib/Log/error_log.php

https://bitbucket.org/ywarnier/chamilo-dev
PHP | 160 lines | 58 code | 20 blank | 82 comment | 6 complexity | f20cc3cda3044848728e3a1a63176449 MD5 | raw file
Possible License(s): GPL-2.0, BSD-3-Clause, LGPL-2.1, LGPL-3.0, GPL-3.0, MIT
  1. <?php
  2. /**
  3. * $Header$
  4. *
  5. * @version $Revision: 293927 $
  6. * @package Log
  7. */
  8. /**
  9. * The Log_error_log class is a concrete implementation of the Log abstract
  10. * class that logs messages using PHP's error_log() function.
  11. *
  12. * @author Jon Parise <jon@php.net>
  13. * @since Log 1.7.0
  14. * @package Log
  15. *
  16. * @example error_log.php Using the error_log handler.
  17. */
  18. class Log_error_log extends Log
  19. {
  20. /**
  21. * The error_log() log type.
  22. * @var integer
  23. * @access private
  24. */
  25. var $_type = PEAR_LOG_TYPE_SYSTEM;
  26. /**
  27. * The type-specific destination value.
  28. * @var string
  29. * @access private
  30. */
  31. var $_destination = '';
  32. /**
  33. * Additional headers to pass to the mail() function when the
  34. * PEAR_LOG_TYPE_MAIL type is used.
  35. * @var string
  36. * @access private
  37. */
  38. var $_extra_headers = '';
  39. /**
  40. * String containing the format of a log line.
  41. * @var string
  42. * @access private
  43. */
  44. var $_lineFormat = '%2$s: %4$s';
  45. /**
  46. * String containing the timestamp format. It will be passed directly to
  47. * strftime(). Note that the timestamp string will generated using the
  48. * current locale.
  49. * @var string
  50. * @access private
  51. */
  52. var $_timeFormat = '%b %d %H:%M:%S';
  53. /**
  54. * Constructs a new Log_error_log object.
  55. *
  56. * @param string $name One of the PEAR_LOG_TYPE_* constants.
  57. * @param string $ident The identity string.
  58. * @param array $conf The configuration array.
  59. * @param int $level Log messages up to and including this level.
  60. * @access public
  61. */
  62. function Log_error_log($name, $ident = '', $conf = array(), $level = PEAR_LOG_DEBUG)
  63. {
  64. $this->_id = md5(microtime());
  65. $this->_type = $name;
  66. $this->_ident = $ident;
  67. $this->_mask = Log :: UPTO($level);
  68. if (! empty($conf['destination']))
  69. {
  70. $this->_destination = $conf['destination'];
  71. }
  72. if (! empty($conf['extra_headers']))
  73. {
  74. $this->_extra_headers = $conf['extra_headers'];
  75. }
  76. if (! empty($conf['lineFormat']))
  77. {
  78. $this->_lineFormat = str_replace(array_keys($this->_formatMap), array_values($this->_formatMap), $conf['lineFormat']);
  79. }
  80. if (! empty($conf['timeFormat']))
  81. {
  82. $this->_timeFormat = $conf['timeFormat'];
  83. }
  84. }
  85. /**
  86. * Opens the handler.
  87. *
  88. * @access public
  89. * @since Log 1.9.6
  90. */
  91. function open()
  92. {
  93. $this->_opened = true;
  94. return true;
  95. }
  96. /**
  97. * Closes the handler.
  98. *
  99. * @access public
  100. * @since Log 1.9.6
  101. */
  102. function close()
  103. {
  104. $this->_opened = false;
  105. return true;
  106. }
  107. /**
  108. * Logs $message using PHP's error_log() function. The message is also
  109. * passed along to any Log_observer instances that are observing this Log.
  110. *
  111. * @param mixed $message String or object containing the message to log.
  112. * @param string $priority The priority of the message. Valid
  113. * values are: PEAR_LOG_EMERG, PEAR_LOG_ALERT,
  114. * PEAR_LOG_CRIT, PEAR_LOG_ERR, PEAR_LOG_WARNING,
  115. * PEAR_LOG_NOTICE, PEAR_LOG_INFO, and PEAR_LOG_DEBUG.
  116. * @return boolean True on success or false on failure.
  117. * @access public
  118. */
  119. function log($message, $priority = null)
  120. {
  121. /* If a priority hasn't been specified, use the default value. */
  122. if ($priority === null)
  123. {
  124. $priority = $this->_priority;
  125. }
  126. /* Abort early if the priority is above the maximum logging level. */
  127. if (! $this->_isMasked($priority))
  128. {
  129. return false;
  130. }
  131. /* Extract the string representation of the message. */
  132. $message = $this->_extractMessage($message);
  133. /* Build the string containing the complete log line. */
  134. $line = $this->_format($this->_lineFormat, strftime($this->_timeFormat), $priority, $message);
  135. /* Pass the log line and parameters to the error_log() function. */
  136. $success = error_log($line, $this->_type, $this->_destination, $this->_extra_headers);
  137. $this->_announce(array('priority' => $priority, 'message' => $message));
  138. return $success;
  139. }
  140. }