log.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  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 = [];
  31. private static $sqlog = false;
  32. public static function start_sql_log()
  33. {
  34. self::$sqlog = [];
  35. }
  36. public static function sql_log()
  37. {
  38. if (is_array(self::$sqlog)) {
  39. return self::$sqlog;
  40. } else {
  41. return [];
  42. }
  43. }
  44. public static function end_sql_log()
  45. {
  46. self::$sqlog = false;
  47. }
  48. private static function add_sql_log($log)
  49. {
  50. if (is_array(self::$sqlog)) {
  51. self::$sqlog[] = $log;
  52. }
  53. }
  54. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  55. static private $cur_path_file_name = '';
  56. static private $cur_path_file;
  57. public static function record_path($content)
  58. {
  59. $path_file = BASE_DATA_PATH . '/log/' . date('Ymd', time()) . '_path.log';
  60. if (self::$cur_path_file_name != $path_file) {
  61. if (self::$cur_path_file != null) {
  62. fclose(self::$cur_path_file);
  63. }
  64. self::$cur_path_file = fopen($path_file, 'a');
  65. }
  66. if (@flock(self::$cur_path_file, LOCK_EX)) {
  67. fwrite(self::$cur_path_file, $content);
  68. fwrite(self::$cur_path_file, "\r\n");
  69. @flock(self::$cur_path_file, LOCK_UN);
  70. }
  71. }
  72. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  73. public static function record($message, $lev = self::ERR)
  74. {
  75. $now = date('Y-m-d H:i:s', time());
  76. $pid = posix_getpid();
  77. if ($lev == self::WAIT_HANDLE) {
  78. $level = 'WAIT_HANDLE';
  79. $log_file = BASE_DATA_PATH . '/log/' . date('Ymd', time()) . '-wait.log';
  80. $content = "[{$pid} {$now}] {$level}: {$message}\r\n";
  81. file_put_contents($log_file, $content, FILE_APPEND);
  82. return;
  83. }
  84. if ($lev == self::SQL) {
  85. $level = 'SQL';
  86. if (self::open_sql) {
  87. self::write($message, $level);
  88. }
  89. return;
  90. }
  91. if ($lev >= self::cur_level && $lev <= self::RUN) {
  92. $level = self::get_level($lev);
  93. self::write($message, $level);
  94. }
  95. if ($lev == self::ERR) {
  96. self::msg();
  97. }
  98. }
  99. private static $cur_file_name;
  100. private static $cur_file = null;
  101. private static function write($message, $level)
  102. {
  103. $now = @date('Y-m-d H:i:s', time());
  104. $pid = posix_getpid();
  105. $appid = empty(APP_ID) ? '' : APP_ID;
  106. $log_file = BASE_DATA_PATH . '/log/' . date('Ymd', time()) . '-' . $appid . '.log';
  107. if (self::$cur_file_name != $log_file) {
  108. if (self::$cur_file != null) {
  109. fclose(self::$cur_file);
  110. }
  111. self::$cur_file_name = $log_file;
  112. self::$cur_file = fopen($log_file, 'a+');
  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. fwrite(self::$cur_file, $content);
  119. }
  120. fflush(self::$cur_file);
  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. }