人人商城

log.class.php 5.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. <?php
  2. class PhpLog
  3. {
  4. const DEBUG = 1; const INFO = 2; const WARN = 3; const ERROR = 4; const FATAL = 5; const OFF = 6;
  5. const LOG_OPEN = 1;
  6. const OPEN_FAILED = 2;
  7. const LOG_CLOSED = 3;
  8. public $Log_Status = PhpLog::LOG_CLOSED;
  9. public $DateFormat= "Y-m-d G:i:s";
  10. public $MessageQueue;
  11. private $filename;
  12. private $log_file;
  13. private $priority = PhpLog::INFO;
  14. private $file_handle;
  15. public function __construct( $filepath, $timezone, $priority )
  16. {
  17. if ( $priority == PhpLog::OFF ) return;
  18. $this->filename = date('Y-m-d', time()) . '.log'; $this->log_file = $this->createPath($filepath, $this->filename);
  19. $this->MessageQueue = array();
  20. $this->priority = $priority;
  21. date_default_timezone_set($timezone);
  22. if ( !file_exists($filepath) ) {
  23. if(!empty($filepath)) {
  24. if(!($this->_createDir($filepath)))
  25. {
  26. die("创建目录失败!");
  27. }
  28. if ( !is_writable($this->log_file) )
  29. {
  30. $this->Log_Status = PhpLog::OPEN_FAILED;
  31. $this->MessageQueue[] = "The file exists, but could not be opened for writing. Check that appropriate permissions have been set.";
  32. return;
  33. }
  34. }
  35. }
  36. if ( $this->file_handle = fopen( $this->log_file , "a+" ) )
  37. {
  38. $this->Log_Status = PhpLog::LOG_OPEN;
  39. $this->MessageQueue[] = "The log file was opened successfully.";
  40. }
  41. else
  42. {
  43. $this->Log_Status = PhpLog::OPEN_FAILED;
  44. $this->MessageQueue[] = "The file could not be opened. Check permissions.";
  45. }
  46. return;
  47. }
  48. public function __destruct()
  49. {
  50. if ( $this->file_handle )
  51. fclose( $this->file_handle );
  52. }
  53. private function _createDir($dir)
  54. {
  55. return is_dir($dir) or (self::_createDir(dirname($dir)) and mkdir($dir, 0777));
  56. }
  57. private function createPath($dir, $filename)
  58. {
  59. if (empty($dir))
  60. {
  61. return $filename;
  62. }
  63. else
  64. {
  65. return $dir . "/" . $filename;
  66. }
  67. }
  68. public function LogInfo($line)
  69. {
  70. $sAarray = array();
  71. $sAarray = debug_backtrace();
  72. $sGetFilePath = $sAarray[0]["file"];
  73. $sGetFileLine = $sAarray[0]["line"];
  74. $this->Log( $line, PhpLog::INFO, $sGetFilePath, $sGetFileLine);
  75. unset($sAarray);
  76. unset($sGetFilePath);
  77. unset($sGetFileLine);
  78. }
  79. public function LogDebug($line)
  80. {
  81. $sAarray = array();
  82. $sAarray = debug_backtrace();
  83. $sGetFilePath = $sAarray[0]["file"];
  84. $sGetFileLine = $sAarray[0]["line"];
  85. $this->Log( $line, PhpLog::DEBUG, $sGetFilePath, $sGetFileLine);
  86. unset($sAarray);
  87. unset($sGetFilePath);
  88. unset($sGetFileLine);
  89. }
  90. public function LogWarn($line)
  91. {
  92. $sAarray = array();
  93. $sAarray = debug_backtrace();
  94. $sGetFilePath = $sAarray[0]["file"];
  95. $sGetFileLine = $sAarray[0]["line"];
  96. $this->Log( $line, PhpLog::WARN, $sGetFilePath, $sGetFileLine);
  97. unset($sAarray);
  98. unset($sGetFilePath);
  99. unset($sGetFileLine);
  100. }
  101. public function LogError($line)
  102. {
  103. $sAarray = array();
  104. $sAarray = debug_backtrace();
  105. $sGetFilePath = $sAarray[0]["file"];
  106. $sGetFileLine = $sAarray[0]["line"];
  107. $this->Log( $line, PhpLog::ERROR, $sGetFilePath, $sGetFileLine);
  108. unset($sAarray);
  109. unset($sGetFilePath);
  110. unset($sGetFileLine);
  111. }
  112. public function LogFatal($line)
  113. {
  114. $sAarray = array();
  115. $sAarray = debug_backtrace();
  116. $sGetFilePath = $sAarray[0]["file"];
  117. $sGetFileLine = $sAarray[0]["line"];
  118. $this->Log( $line, PhpLog::FATAL, $sGetFilePath, $sGetFileLine);
  119. unset($sAarray);
  120. unset($sGetFilePath);
  121. unset($sGetFileLine);
  122. }
  123. public function Log($line, $priority, $sFile, $iLine)
  124. {
  125. if ($iLine > 0)
  126. {
  127. $line = iconv('GBK', 'UTF-8', $line);
  128. if ( $this->priority <= $priority )
  129. {
  130. $status = $this->getTimeLine( $priority, $sFile, $iLine);
  131. $this->WriteFreeFormLine ( "$status $line \n" );
  132. }
  133. }
  134. else
  135. {
  136. $sAarray = array();
  137. $sAarray = debug_backtrace();
  138. $sGetFilePath = $sAarray[0]["file"];
  139. $sGetFileLine = $sAarray[0]["line"];
  140. if ( $this->priority <= $priority )
  141. {
  142. $status = $this->getTimeLine( $priority, $sGetFilePath, $sGetFileLine);
  143. unset($sAarray);
  144. unset($sGetFilePath);
  145. unset($sGetFileLine);
  146. $this->WriteFreeFormLine ( "$status $line \n" );
  147. }
  148. }
  149. }
  150. public function WriteFreeFormLine( $line )
  151. {
  152. if ( $this->Log_Status == PhpLog::LOG_OPEN && $this->priority != PhpLog::OFF )
  153. {
  154. if (fwrite( $this->file_handle , $line ) === false)
  155. {
  156. $this->MessageQueue[] = "The file could not be written to. Check that appropriate permissions have been set.";
  157. }
  158. }
  159. }
  160. private function getRemoteIP()
  161. {
  162. foreach (array('HTTP_CLIENT_IP', 'HTTP_X_FORWARDED_FOR', 'HTTP_X_FORWARDED', 'HTTP_X_CLUSTER_CLIENT_IP', 'HTTP_FORWARDED_FOR', 'HTTP_FORWARDED', 'REMOTE_ADDR') as $key)
  163. {
  164. if (array_key_exists($key, $_SERVER) === true)
  165. {
  166. foreach (explode(',', $_SERVER[$key]) as $ip)
  167. {
  168. $ip = trim($ip);
  169. if (!empty($ip))
  170. {
  171. return $ip;
  172. }
  173. }
  174. }
  175. }
  176. return "_NO_IP";
  177. }
  178. private function getTimeLine( $level, $FilePath, $FileLine)
  179. {
  180. $time = date( $this->DateFormat );
  181. $ip = $this->getRemoteIP();
  182. switch( $level )
  183. {
  184. case PhpLog::INFO:
  185. return "$time, " . "INFO, " . "$ip, " . "File[ $FilePath ], " . "Line[$FileLine]" . "------";
  186. case PhpLog::WARN:
  187. return "$time, " . "WARN, " . "$ip, " . "File[ $FilePath ], " . "Line[$FileLine]" . "------";
  188. case PhpLog::DEBUG:
  189. return "$time, " . "DEBUG, " . "$ip, " . "File[ $FilePath ], " . "Line[$FileLine]" . "------";
  190. case PhpLog::ERROR:
  191. return "$time, " . "ERROR, " . "$ip, " . "File[ $FilePath ], " . "Line[$FileLine]" . "------";
  192. case PhpLog::FATAL:
  193. return "$time, " . "FATAL, " . "$ip, " . "File[ $FilePath ], " . "Line[$FileLine]" . "------";
  194. default:
  195. return "$time, " . "LOG, " . "$ip, " . "File[ $FilePath ], " . "Line[$FileLine]" . "------";
  196. }
  197. }
  198. }
  199. ?>