PageRenderTime 137ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/script/lib/Log/error_log.php

https://bitbucket.org/chamilo/chamilo/
PHP | 160 lines | 58 code | 20 blank | 82 comment | 6 complexity | 868236a4150c15fa69989c2ca1c55a65 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(),
  63. $level = PEAR_LOG_DEBUG)
  64. {
  65. $this->_id = md5(microtime());
  66. $this->_type = $name;
  67. $this->_ident = $ident;
  68. $this->_mask = Log::UPTO($level);
  69. if (!empty($conf['destination'])) {
  70. $this->_destination = $conf['destination'];
  71. }
  72. if (!empty($conf['extra_headers'])) {
  73. $this->_extra_headers = $conf['extra_headers'];
  74. }
  75. if (!empty($conf['lineFormat'])) {
  76. $this->_lineFormat = str_replace(array_keys($this->_formatMap),
  77. array_values($this->_formatMap),
  78. $conf['lineFormat']);
  79. }
  80. if (!empty($conf['timeFormat'])) {
  81. $this->_timeFormat = $conf['timeFormat'];
  82. }
  83. }
  84. /**
  85. * Opens the handler.
  86. *
  87. * @access public
  88. * @since Log 1.9.6
  89. */
  90. function open()
  91. {
  92. $this->_opened = true;
  93. return true;
  94. }
  95. /**
  96. * Closes the handler.
  97. *
  98. * @access public
  99. * @since Log 1.9.6
  100. */
  101. function close()
  102. {
  103. $this->_opened = false;
  104. return true;
  105. }
  106. /**
  107. * Logs $message using PHP's error_log() function. The message is also
  108. * passed along to any Log_observer instances that are observing this Log.
  109. *
  110. * @param mixed $message String or object containing the message to log.
  111. * @param string $priority The priority of the message. Valid
  112. * values are: PEAR_LOG_EMERG, PEAR_LOG_ALERT,
  113. * PEAR_LOG_CRIT, PEAR_LOG_ERR, PEAR_LOG_WARNING,
  114. * PEAR_LOG_NOTICE, PEAR_LOG_INFO, and PEAR_LOG_DEBUG.
  115. * @return boolean True on success or false on failure.
  116. * @access public
  117. */
  118. function log($message, $priority = null)
  119. {
  120. /* If a priority hasn't been specified, use the default value. */
  121. if ($priority === null) {
  122. $priority = $this->_priority;
  123. }
  124. /* Abort early if the priority is above the maximum logging level. */
  125. if (!$this->_isMasked($priority)) {
  126. return false;
  127. }
  128. /* Extract the string representation of the message. */
  129. $message = $this->_extractMessage($message);
  130. /* Build the string containing the complete log line. */
  131. $line = $this->_format($this->_lineFormat,
  132. strftime($this->_timeFormat),
  133. $priority, $message);
  134. /* Pass the log line and parameters to the error_log() function. */
  135. $success = error_log($line, $this->_type, $this->_destination,
  136. $this->_extra_headers);
  137. $this->_announce(array('priority' => $priority, 'message' => $message));
  138. return $success;
  139. }
  140. }