PageRenderTime 45ms CodeModel.GetById 18ms RepoModel.GetById 1ms app.codeStats 0ms

/gulliver/thirdparty/pear/Log/file.php

https://github.com/caracolazo/pm
PHP | 339 lines | 144 code | 46 blank | 149 comment | 32 complexity | 200e586e72ba267d6d81132f2e5b9de0 MD5 | raw file
Possible License(s): BSD-3-Clause, BSD-2-Clause, Apache-2.0, LGPL-2.1, GPL-3.0
  1. <?php
  2. /**
  3. * $Header: /repository/pear/Log/Log/file.php,v 1.46 2006/12/07 04:15:02 jon Exp $
  4. *
  5. * @version $Revision: 1.46 $
  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. if (is_resource($this->_fp)) {
  222. return fflush($this->_fp);
  223. }
  224. return false;
  225. }
  226. /**
  227. * Logs $message to the output window. The message is also passed along
  228. * to any Log_observer instances that are observing this Log.
  229. *
  230. * @param mixed $message String or object containing the message to log.
  231. * @param string $priority The priority of the message. Valid
  232. * values are: PEAR_LOG_EMERG, PEAR_LOG_ALERT,
  233. * PEAR_LOG_CRIT, PEAR_LOG_ERR, PEAR_LOG_WARNING,
  234. * PEAR_LOG_NOTICE, PEAR_LOG_INFO, and PEAR_LOG_DEBUG.
  235. * @return boolean True on success or false on failure.
  236. * @access public
  237. */
  238. function log($message, $priority = null)
  239. {
  240. // define constants, because there are times when the page is rendered outside a workspace
  241. if ( !defined('PM_PID') ) {
  242. define ('PM_PID', rand(0,1000) );
  243. }
  244. if ( !defined('SYS_SYS') ) {
  245. define ('SYS_SYS', '' );
  246. }
  247. /* If a priority hasn't been specified, use the default value. */
  248. if ($priority === null) {
  249. $priority = $this->_priority;
  250. }
  251. /* Abort early if the priority is above the maximum logging level. */
  252. if (!$this->_isMasked($priority)) {
  253. return false;
  254. }
  255. /* If the log file isn't already open, open it now. */
  256. if (!$this->_opened && !$this->open()) {
  257. return false;
  258. }
  259. /* Extract the string representation of the message. */
  260. $message = $this->_extractMessage($message);
  261. /*********************** change here >> depth value *************/
  262. $uri = $_SERVER['REQUEST_URI'];
  263. $backTrace = $this->_getBacktraceVars( 5 );
  264. $method = $_SERVER['REQUEST_METHOD'];
  265. $ip = getenv('REMOTE_ADDR');
  266. $myPid = PM_PID;
  267. //$request variable will have all the values in POST and GET
  268. $request = '';
  269. foreach( $_POST as $k => $v ) $request .= ($request!='' ? "\t" : '') . $k . '='.$v;
  270. foreach( $_GET as $k => $v ) $request .= ($request!='' ? "\t" : '') . $k . '='.$v;
  271. //exact time with microseconds
  272. $t = explode(' ',microtime(false));
  273. list($usec, $sec) = explode(' ', microtime());
  274. $micro = date('H:i:s.') . sprintf("%04d", floor($usec * 10000 ));
  275. /* Build the string containing the complete message */
  276. $msg = sprintf ( "%s|%s|%s|%05d|%s|%s|%s|%s|%s\n", $micro,SYS_SYS,$ip, $myPid, $message, $backTrace[3], $method, $uri, $request);
  277. /* Build the string containing the complete log line. */
  278. $line = $this->_format('%4$s', $micro,'',$msg );
  279. /* If locking is enabled, acquire an exclusive lock on the file. */
  280. if ($this->_locking) {
  281. flock($this->_fp, LOCK_EX);
  282. }
  283. /* Write the log line to the log file. */
  284. $success = (fwrite($this->_fp, $line) !== false);
  285. /* Unlock the file now that we're finished writing to it. */
  286. if ($this->_locking) {
  287. flock($this->_fp, LOCK_UN);
  288. }
  289. /* Notify observers about this log message. */
  290. $this->_announce(array('priority' => $priority, 'message' => $message));
  291. return $success;
  292. }
  293. }