PageRenderTime 44ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/php/Log/file.php

https://bitbucket.org/adarshj/convenient_website
PHP | 286 lines | 111 code | 39 blank | 136 comment | 18 complexity | a048cfa6877065832efe2adc4374f3bc MD5 | raw file
Possible License(s): Apache-2.0, MPL-2.0-no-copyleft-exception, LGPL-2.1, BSD-2-Clause, GPL-2.0, LGPL-3.0
  1. <?php
  2. /**
  3. * $Header: /repository/pear/Log/Log/file.php,v 1.37 2004/01/19 08:02:40 jon Exp $
  4. *
  5. * @version $Revision: 1.37 $
  6. * @package Log
  7. */
  8. /**
  9. * The Log_file class is a concrete implementation of the Log abstract
  10. * class that logs messages to a text file.
  11. *
  12. * @author Jon Parise <jon@php.net>
  13. * @author Roman Neuhauser <neuhauser@bellavista.cz>
  14. * @since Log 1.0
  15. * @package Log
  16. *
  17. * @example file.php Using the file handler.
  18. */
  19. class Log_file extends Log
  20. {
  21. /**
  22. * String containing the name of the log file.
  23. * @var string
  24. * @access private
  25. */
  26. var $_filename = 'php.log';
  27. /**
  28. * Handle to the log file.
  29. * @var resource
  30. * @access private
  31. */
  32. var $_fp = false;
  33. /**
  34. * Should new log entries be append to an existing log file, or should the
  35. * a new log file overwrite an existing one?
  36. * @var boolean
  37. * @access private
  38. */
  39. var $_append = true;
  40. /**
  41. * Integer (in octal) containing the log file's permissions mode.
  42. * @var integer
  43. * @access private
  44. */
  45. var $_mode = 0644;
  46. /**
  47. * String containing the format of a log line.
  48. * @var string
  49. * @access private
  50. */
  51. var $_lineFormat = '%1$s %2$s [%3$s] %4$s';
  52. /**
  53. * String containing the timestamp format. It will be passed directly to
  54. * strftime(). Note that the timestamp string will generated using the
  55. * current locale.
  56. * @var string
  57. * @access private
  58. */
  59. var $_timeFormat = '%b %d %H:%M:%S';
  60. /**
  61. * Hash that maps canonical format keys to position arguments for the
  62. * "line format" string.
  63. * @var array
  64. * @access private
  65. */
  66. var $_formatMap = array('%{timestamp}' => '%1$s',
  67. '%{ident}' => '%2$s',
  68. '%{priority}' => '%3$s',
  69. '%{message}' => '%4$s',
  70. '%\{' => '%%{');
  71. /**
  72. * String containing the end-on-line character sequence.
  73. * @var string
  74. * @access private
  75. */
  76. var $_eol = "\n";
  77. /**
  78. * Constructs a new Log_file object.
  79. *
  80. * @param string $name Ignored.
  81. * @param string $ident The identity string.
  82. * @param array $conf The configuration array.
  83. * @param int $level Log messages up to and including this level.
  84. * @access public
  85. */
  86. function Log_file($name, $ident = '', $conf = array(),
  87. $level = PEAR_LOG_DEBUG)
  88. {
  89. $this->_id = md5(microtime());
  90. $this->_filename = $name;
  91. $this->_ident = $ident;
  92. $this->_mask = Log::UPTO($level);
  93. if (isset($conf['append'])) {
  94. $this->_append = $conf['append'];
  95. }
  96. if (!empty($conf['mode'])) {
  97. $this->_mode = $conf['mode'];
  98. }
  99. if (!empty($conf['lineFormat'])) {
  100. $this->_lineFormat = str_replace(array_keys($this->_formatMap),
  101. array_values($this->_formatMap),
  102. $conf['lineFormat']);
  103. }
  104. if (!empty($conf['timeFormat'])) {
  105. $this->_timeFormat = $conf['timeFormat'];
  106. }
  107. if (!empty($conf['eol'])) {
  108. $this->_eol = $conf['eol'];
  109. } else {
  110. $this->_eol = (strstr(PHP_OS, 'WIN')) ? "\r\n" : "\n";
  111. }
  112. register_shutdown_function(array(&$this, '_Log_file'));
  113. }
  114. /**
  115. * Destructor
  116. */
  117. function _Log_file()
  118. {
  119. if ($this->_opened) {
  120. $this->close();
  121. }
  122. }
  123. /**
  124. * Creates the given directory path. If the parent directories don't
  125. * already exist, they will be created, too.
  126. *
  127. * @param string $path The full directory path to create.
  128. * @param integer $mode The permissions mode with which the
  129. * directories will be created.
  130. *
  131. * @return True if the full path is successfully created or already
  132. * exists.
  133. *
  134. * @access private
  135. */
  136. function _mkpath($path, $mode = 0700)
  137. {
  138. static $depth = 0;
  139. /* Guard against potentially infinite recursion. */
  140. if ($depth++ > 25) {
  141. trigger_error("_mkpath(): Maximum recursion depth (25) exceeded",
  142. E_USER_WARNING);
  143. return false;
  144. }
  145. /* We're only interested in the directory component of the path. */
  146. $path = dirname($path);
  147. /* If the directory already exists, return success immediately. */
  148. if (is_dir($path)) {
  149. $depth = 0;
  150. return true;
  151. }
  152. /*
  153. * In order to understand recursion, you must first understand
  154. * recursion ...
  155. */
  156. if ($this->_mkpath($path, $mode) === false) {
  157. return false;
  158. }
  159. return @mkdir($path, $mode);
  160. }
  161. /**
  162. * Opens the log file for output. If the specified log file does not
  163. * already exist, it will be created. By default, new log entries are
  164. * appended to the end of the log file.
  165. *
  166. * This is implicitly called by log(), if necessary.
  167. *
  168. * @access public
  169. */
  170. function open()
  171. {
  172. if (!$this->_opened) {
  173. /* If the log file's directory doesn't exist, create it. */
  174. if (!is_dir(dirname($this->_filename))) {
  175. $this->_mkpath($this->_filename);
  176. }
  177. /* Obtain a handle to the log file. */
  178. $this->_fp = fopen($this->_filename, ($this->_append) ? 'a' : 'w');
  179. $this->_opened = ($this->_fp !== false);
  180. /* Attempt to set the log file's mode. */
  181. @chmod($this->_filename, $this->_mode);
  182. }
  183. return $this->_opened;
  184. }
  185. /**
  186. * Closes the log file if it is open.
  187. *
  188. * @access public
  189. */
  190. function close()
  191. {
  192. /* If the log file is open, close it. */
  193. if ($this->_opened && fclose($this->_fp)) {
  194. $this->_opened = false;
  195. }
  196. return ($this->_opened === false);
  197. }
  198. /**
  199. * Flushes all pending data to the file handle.
  200. *
  201. * @access public
  202. * @since Log 1.8.2
  203. */
  204. function flush()
  205. {
  206. return fflush($this->_fp);
  207. }
  208. /**
  209. * Logs $message to the output window. The message is also passed along
  210. * to any Log_observer instances that are observing this Log.
  211. *
  212. * @param mixed $message String or object containing the message to log.
  213. * @param string $priority The priority of the message. Valid
  214. * values are: PEAR_LOG_EMERG, PEAR_LOG_ALERT,
  215. * PEAR_LOG_CRIT, PEAR_LOG_ERR, PEAR_LOG_WARNING,
  216. * PEAR_LOG_NOTICE, PEAR_LOG_INFO, and PEAR_LOG_DEBUG.
  217. * @return boolean True on success or false on failure.
  218. * @access public
  219. */
  220. function log($message, $priority = null)
  221. {
  222. /* If a priority hasn't been specified, use the default value. */
  223. if ($priority === null) {
  224. $priority = $this->_priority;
  225. }
  226. /* Abort early if the priority is above the maximum logging level. */
  227. if (!$this->_isMasked($priority)) {
  228. return false;
  229. }
  230. /* If the log file isn't already open, open it now. */
  231. if (!$this->_opened && !$this->open()) {
  232. return false;
  233. }
  234. /* Extract the string representation of the message. */
  235. $message = $this->_extractMessage($message);
  236. /* Build the string containing the complete log line. */
  237. $line = sprintf($this->_lineFormat, strftime($this->_timeFormat),
  238. $this->_ident, $this->priorityToString($priority),
  239. $message) . $this->_eol;
  240. /* Write the log line to the log file. */
  241. $success = (fwrite($this->_fp, $line) !== false);
  242. /* Notify observers about this log message. */
  243. $this->_announce(array('priority' => $priority, 'message' => $message));
  244. return $success;
  245. }
  246. }
  247. ?>