log.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  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. if(defined('USE_COROUTINE') && USE_COROUTINE === true) {
  105. $pid = getmypid();
  106. $cid = Swoole\Coroutine::getCid();
  107. $pid = "{$pid}-{$cid}";
  108. }
  109. else {
  110. $pid = posix_getpid();
  111. }
  112. $appid = empty(APP_ID) ? '' : APP_ID;
  113. $log_file = BASE_DATA_PATH . '/log/' . date('Ymd', time()) . '-' . $appid . '.log';
  114. if (self::$cur_file_name != $log_file) {
  115. if (self::$cur_file != null) {
  116. fclose(self::$cur_file);
  117. }
  118. self::$cur_file_name = $log_file;
  119. self::$cur_file = fopen($log_file, 'a+');
  120. }
  121. $content = "[{$pid} {$now}] {$level}: {$message}\r\n";
  122. $ret = fwrite(self::$cur_file, $content);
  123. if ($ret === false) {
  124. self::$cur_file = fopen($log_file, 'a+');
  125. fwrite(self::$cur_file, $content);
  126. }
  127. fflush(self::$cur_file);
  128. }
  129. public static function endl($lev = self::ERR)
  130. {
  131. $content = "\r\n";
  132. if ($lev == self::SQL && self::open_sql) {
  133. $log_file = BASE_DATA_PATH . '/log/' . date('Ymd', time()) . '.log';
  134. file_put_contents($log_file, $content, FILE_APPEND);
  135. return;
  136. }
  137. if ($lev >= self::cur_level) {
  138. $log_file = BASE_DATA_PATH . '/log/' . date('Ymd', time()) . '.log';
  139. file_put_contents($log_file, $content, FILE_APPEND);
  140. }
  141. }
  142. public static function msg()
  143. {
  144. $debugInfo = debug_backtrace();
  145. $stack = "[";
  146. foreach ($debugInfo as $key => $val) {
  147. if (array_key_exists("file", $val)) {
  148. $stack .= ",file:" . $val["file"];
  149. }
  150. if (array_key_exists("line", $val)) {
  151. $stack .= ",line:" . $val["line"];
  152. }
  153. if (array_key_exists("function", $val)) {
  154. $stack .= ",function:" . $val["function"];
  155. }
  156. }
  157. $stack .= "]";
  158. return $stack;
  159. }
  160. private static function get_level($lev)
  161. {
  162. if ($lev == self::INFO) return 'INFO';
  163. if ($lev == self::DEBUG) return 'DEBUG';
  164. if ($lev == self::WARING) return 'WARING';
  165. if ($lev == self::ERR) return 'ERR';
  166. if ($lev == self::RUN) return 'RUN';
  167. return 'Unknown';
  168. }
  169. public static function read()
  170. {
  171. return self::$log;
  172. }
  173. }