log.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. <?php
  2. /**
  3. * 记录日志
  4. ***/
  5. defined('InShopNC') or exit('Access Invalid!');
  6. class scope_trace
  7. {
  8. private $mTag;
  9. public function __construct($tag)
  10. {
  11. $this->mTag = $tag;
  12. Log::record("{$this->mTag} begin----------------------------",Log::DEBUG);
  13. }
  14. public function __destruct()
  15. {
  16. Log::record("{$this->mTag} end ----------------------------",Log::DEBUG);
  17. }
  18. }
  19. class Log
  20. {
  21. const open_sql = true;
  22. const SQL = 1;
  23. const INFO = 2;
  24. const DEBUG = 3;
  25. const WARING = 4;
  26. const ERR = 5;
  27. const RUN = 6;
  28. const WAIT_HANDLE = 10;
  29. const cur_level = self::DEBUG;
  30. private static $log = array();
  31. private static $sqlog = false;
  32. public static function start_sql_log() {
  33. self::$sqlog = array();
  34. }
  35. public static function sql_log()
  36. {
  37. if(is_array(self::$sqlog)) {
  38. return self::$sqlog;
  39. } else {
  40. return [];
  41. }
  42. }
  43. public static function end_sql_log() {
  44. self::$sqlog = false;
  45. }
  46. private static function add_sql_log($log)
  47. {
  48. if(is_array(self::$sqlog)) {
  49. self::$sqlog[] = $log;
  50. }
  51. }
  52. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  53. static private $cur_path_file_name = '';
  54. static private $cur_path_file;
  55. public static function record_path($content)
  56. {
  57. $path_file = BASE_DATA_PATH . '/log/' . date('Ymd', time()) . '_path.log';
  58. if(self::$cur_path_file_name != $path_file)
  59. {
  60. if(self::$cur_path_file != null) {
  61. fclose(self::$cur_path_file);
  62. }
  63. self::$cur_path_file = fopen($path_file,'a');
  64. }
  65. if (@flock(self::$cur_path_file, LOCK_EX)) {
  66. fwrite(self::$cur_path_file,$content);
  67. fwrite(self::$cur_path_file,"\r\n");
  68. @flock(self::$cur_path_file, LOCK_UN);
  69. }
  70. }
  71. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  72. public static function record($message, $lev = self::ERR)
  73. {
  74. $now = @date('Y-m-d H:i:s', time());
  75. $pid = posix_getpid();
  76. if($lev == self::WAIT_HANDLE) {
  77. $level = 'WAIT_HANDLE';
  78. $log_file = BASE_DATA_PATH.'/log/'.date('Ymd',time()).'-wait.log';
  79. $content = "[{$pid} {$now}] {$level}: {$message}\r\n";
  80. file_put_contents($log_file,$content, FILE_APPEND);
  81. return;
  82. }
  83. if($lev == self::SQL) {
  84. $level = 'SQL';
  85. if(self::open_sql) {
  86. self::write($message,$level);
  87. }
  88. return;
  89. }
  90. if($lev >= self::cur_level && $lev <= self::RUN) {
  91. $level = self::get_level($lev);
  92. self::write($message,$level);
  93. }
  94. if($lev == self::ERR) {
  95. self::msg();
  96. }
  97. }
  98. private static $cur_file_name;
  99. private static $cur_file = null;
  100. private static function write($message,$level)
  101. {
  102. $now = @date('Y-m-d H:i:s', time());
  103. $pid = posix_getpid();
  104. $log_file = BASE_DATA_PATH . '/log/' . date('Ymd', time()) . '.log';
  105. if(self::$cur_file_name != $log_file)
  106. {
  107. if(self::$cur_file != null) {
  108. fclose(self::$cur_file);
  109. }
  110. self::$cur_file_name = $log_file;
  111. self::$cur_file = fopen($log_file,'a+');
  112. chmod($log_file,0777);
  113. }
  114. $content = "[{$pid} {$now}] {$level}: {$message}\r\n";
  115. $ret = fwrite(self::$cur_file,$content);
  116. if($ret === false) {
  117. self::$cur_file = fopen($log_file,'a+');
  118. chmod($log_file,0777);
  119. fwrite(self::$cur_file,$content);
  120. }
  121. }
  122. public static function endl($lev = self::ERR)
  123. {
  124. $content = "\r\n";
  125. if($lev == self::SQL && self::open_sql) {
  126. $log_file = BASE_DATA_PATH.'/log/'.date('Ymd',time()).'.log';
  127. file_put_contents($log_file,$content, FILE_APPEND);
  128. return;
  129. }
  130. if($lev >= self::cur_level) {
  131. $log_file = BASE_DATA_PATH . '/log/' . date('Ymd', time()) . '.log';
  132. file_put_contents($log_file, $content, FILE_APPEND);
  133. }
  134. }
  135. public static function msg()
  136. {
  137. $debugInfo = debug_backtrace();
  138. $stack = "[";
  139. foreach($debugInfo as $key => $val){
  140. if(array_key_exists("file", $val)){
  141. $stack .= ",file:" . $val["file"];
  142. }
  143. if(array_key_exists("line", $val)){
  144. $stack .= ",line:" . $val["line"];
  145. }
  146. if(array_key_exists("function", $val)){
  147. $stack .= ",function:" . $val["function"];
  148. }
  149. }
  150. $stack .= "]";
  151. return $stack;
  152. }
  153. private static function get_level($lev)
  154. {
  155. if($lev == self::INFO) return 'INFO';
  156. if($lev == self::DEBUG) return 'DEBUG';
  157. if($lev == self::WARING) return 'WARING';
  158. if($lev == self::ERR) return 'ERR';
  159. if($lev == self::RUN) return 'RUN';
  160. return 'Unknown';
  161. }
  162. public static function read()
  163. {
  164. return self::$log;
  165. }
  166. }