PageRenderTime 50ms CodeModel.GetById 22ms RepoModel.GetById 1ms app.codeStats 0ms

/www/pear/Log/file.php

https://bitbucket.org/wayfarer/verse
PHP | 312 lines | 125 code | 43 blank | 144 comment | 29 complexity | 69cdc7860933302b4764f731b96a0c53 MD5 | raw file
Possible License(s): ISC, AGPL-3.0, LGPL-2.1, BSD-3-Clause, LGPL-3.0
  1. <?php
  2. /**
  3. * $Header: /repository/pear/Log/Log/file.php,v 1.45 2006/01/11 07:56:37 jon Exp $
  4. *
  5. * @version $Revision: 1.45 $
  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. * Should advisory file locking (i.e., flock()) be used?
  42. * @var boolean
  43. * @access private
  44. */
  45. var $_locking = false;
  46. /**
  47. * Integer (in octal) containing the log file's permissions mode.
  48. * @var integer
  49. * @access private
  50. */
  51. var $_mode = 0644;
  52. /**
  53. * Integer (in octal) specifying the file permission mode that will be
  54. * used when creating directories that do not already exist.
  55. * @var integer
  56. * @access private
  57. */
  58. var $_dirmode = 0755;
  59. /**
  60. * String containing the format of a log line.
  61. * @var string
  62. * @access private
  63. */
  64. var $_lineFormat = '%1$s %2$s [%3$s] %4$s';
  65. /**
  66. * String containing the timestamp format. It will be passed directly to
  67. * strftime(). Note that the timestamp string will generated using the
  68. * current locale.
  69. * @var string
  70. * @access private
  71. */
  72. var $_timeFormat = '%b %d %H:%M:%S';
  73. /**
  74. * String containing the end-on-line character sequence.
  75. * @var string
  76. * @access private
  77. */
  78. var $_eol = "\n";
  79. /**
  80. * Constructs a new Log_file object.
  81. *
  82. * @param string $name Ignored.
  83. * @param string $ident The identity string.
  84. * @param array $conf The configuration array.
  85. * @param int $level Log messages up to and including this level.
  86. * @access public
  87. */
  88. function Log_file($name, $ident = '', $conf = array(),
  89. $level = PEAR_LOG_DEBUG)
  90. {
  91. $this->_id = md5(microtime());
  92. $this->_filename = $name;
  93. $this->_ident = $ident;
  94. $this->_mask = Log::UPTO($level);
  95. if (isset($conf['append'])) {
  96. $this->_append = $conf['append'];
  97. }
  98. if (isset($conf['locking'])) {
  99. $this->_locking = $conf['locking'];
  100. }
  101. if (!empty($conf['mode'])) {
  102. if (is_string($conf['mode'])) {
  103. $this->_mode = octdec($conf['mode']);
  104. } else {
  105. $this->_mode = $conf['mode'];
  106. }
  107. }
  108. if (!empty($conf['dirmode'])) {
  109. if (is_string($conf['dirmode'])) {
  110. $this->_dirmode = octdec($conf['dirmode']);
  111. } else {
  112. $this->_dirmode = $conf['dirmode'];
  113. }
  114. }
  115. if (!empty($conf['lineFormat'])) {
  116. $this->_lineFormat = str_replace(array_keys($this->_formatMap),
  117. array_values($this->_formatMap),
  118. $conf['lineFormat']);
  119. }
  120. if (!empty($conf['timeFormat'])) {
  121. $this->_timeFormat = $conf['timeFormat'];
  122. }
  123. if (!empty($conf['eol'])) {
  124. $this->_eol = $conf['eol'];
  125. } else {
  126. $this->_eol = (strstr(PHP_OS, 'WIN')) ? "\r\n" : "\n";
  127. }
  128. register_shutdown_function(array(&$this, '_Log_file'));
  129. }
  130. /**
  131. * Destructor
  132. */
  133. function _Log_file()
  134. {
  135. if ($this->_opened) {
  136. $this->close();
  137. }
  138. }
  139. /**
  140. * Creates the given directory path. If the parent directories don't
  141. * already exist, they will be created, too.
  142. *
  143. * This implementation is inspired by Python's os.makedirs function.
  144. *
  145. * @param string $path The full directory path to create.
  146. * @param integer $mode The permissions mode with which the
  147. * directories will be created.
  148. *
  149. * @return True if the full path is successfully created or already
  150. * exists.
  151. *
  152. * @access private
  153. */
  154. function _mkpath($path, $mode = 0700)
  155. {
  156. /* Separate the last pathname component from the rest of the path. */
  157. $head = dirname($path);
  158. $tail = basename($path);
  159. /* Make sure we've split the path into two complete components. */
  160. if (empty($tail)) {
  161. $head = dirname($path);
  162. $tail = basename($path);
  163. }
  164. /* Recurse up the path if our current segment does not exist. */
  165. if (!empty($head) && !empty($tail) && !is_dir($head)) {
  166. $this->_mkpath($head, $mode);
  167. }
  168. /* Create this segment of the path. */
  169. return @mkdir($head, $mode);
  170. }
  171. /**
  172. * Opens the log file for output. If the specified log file does not
  173. * already exist, it will be created. By default, new log entries are
  174. * appended to the end of the log file.
  175. *
  176. * This is implicitly called by log(), if necessary.
  177. *
  178. * @access public
  179. */
  180. function open()
  181. {
  182. if (!$this->_opened) {
  183. /* If the log file's directory doesn't exist, create it. */
  184. if (!is_dir(dirname($this->_filename))) {
  185. $this->_mkpath($this->_filename, $this->_dirmode);
  186. }
  187. /* Determine whether the log file needs to be created. */
  188. $creating = !file_exists($this->_filename);
  189. /* Obtain a handle to the log file. */
  190. $this->_fp = fopen($this->_filename, ($this->_append) ? 'a' : 'w');
  191. /* We consider the file "opened" if we have a valid file pointer. */
  192. $this->_opened = ($this->_fp !== false);
  193. /* Attempt to set the file's permissions if we just created it. */
  194. if ($creating && $this->_opened) {
  195. chmod($this->_filename, $this->_mode);
  196. }
  197. }
  198. return $this->_opened;
  199. }
  200. /**
  201. * Closes the log file if it is open.
  202. *
  203. * @access public
  204. */
  205. function close()
  206. {
  207. /* If the log file is open, close it. */
  208. if ($this->_opened && fclose($this->_fp)) {
  209. $this->_opened = false;
  210. }
  211. return ($this->_opened === false);
  212. }
  213. /**
  214. * Flushes all pending data to the file handle.
  215. *
  216. * @access public
  217. * @since Log 1.8.2
  218. */
  219. function flush()
  220. {
  221. return fflush($this->_fp);
  222. }
  223. /**
  224. * Logs $message to the output window. The message is also passed along
  225. * to any Log_observer instances that are observing this Log.
  226. *
  227. * @param mixed $message String or object containing the message to log.
  228. * @param string $priority The priority of the message. Valid
  229. * values are: PEAR_LOG_EMERG, PEAR_LOG_ALERT,
  230. * PEAR_LOG_CRIT, PEAR_LOG_ERR, PEAR_LOG_WARNING,
  231. * PEAR_LOG_NOTICE, PEAR_LOG_INFO, and PEAR_LOG_DEBUG.
  232. * @return boolean True on success or false on failure.
  233. * @access public
  234. */
  235. function log($message, $priority = null)
  236. {
  237. /* If a priority hasn't been specified, use the default value. */
  238. if ($priority === null) {
  239. $priority = $this->_priority;
  240. }
  241. /* Abort early if the priority is above the maximum logging level. */
  242. if (!$this->_isMasked($priority)) {
  243. return false;
  244. }
  245. /* If the log file isn't already open, open it now. */
  246. if (!$this->_opened && !$this->open()) {
  247. return false;
  248. }
  249. /* Extract the string representation of the message. */
  250. $message = $this->_extractMessage($message);
  251. /* Build the string containing the complete log line. */
  252. $line = $this->_format($this->_lineFormat,
  253. strftime($this->_timeFormat),
  254. $priority, $message) . $this->_eol;
  255. /* If locking is enabled, acquire an exclusive lock on the file. */
  256. if ($this->_locking) {
  257. flock($this->_fp, LOCK_EX);
  258. }
  259. /* Write the log line to the log file. */
  260. $success = (fwrite($this->_fp, $line) !== false);
  261. /* Unlock the file now that we're finished writing to it. */
  262. if ($this->_locking) {
  263. flock($this->_fp, LOCK_UN);
  264. }
  265. /* Notify observers about this log message. */
  266. $this->_announce(array('priority' => $priority, 'message' => $message));
  267. return $success;
  268. }
  269. }