log.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. <?php
  2. /**
  3. * 记录日志
  4. ***/
  5. class scope_trace
  6. {
  7. private $mTag;
  8. public function __construct($tag)
  9. {
  10. $this->mTag = $tag;
  11. Log::record("{$this->mTag} begin----------------------------", Log::DEBUG);
  12. }
  13. public function __destruct()
  14. {
  15. Log::record("{$this->mTag} end ----------------------------", Log::DEBUG);
  16. }
  17. }
  18. class Log
  19. {
  20. const open_sql = true;
  21. const SQL = 1;
  22. const INFO = 2;
  23. const DEBUG = 3;
  24. const WARING = 4;
  25. const ERR = 5;
  26. const RUN = 6;
  27. const WAIT_HANDLE = 10;
  28. const cur_level = self::DEBUG;
  29. private static $log = [];
  30. private static $sqlog = false;
  31. public static function start_sql_log()
  32. {
  33. self::$sqlog = [];
  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. {
  45. self::$sqlog = false;
  46. }
  47. private static function add_sql_log($log)
  48. {
  49. if (is_array(self::$sqlog)) {
  50. self::$sqlog[] = $log;
  51. }
  52. }
  53. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  54. static private $cur_path_file_name = '';
  55. static private $cur_path_file;
  56. public static function record_path($content)
  57. {
  58. $path_file = BASE_DATA_PATH . '/log/' . date('Ymd', time()) . '_path.log';
  59. if (self::$cur_path_file_name != $path_file) {
  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. $appid = empty(APP_ID) ? '' : APP_ID;
  105. $log_file = BASE_DATA_PATH . '/log/' . date('Ymd', time()) . '-' . $appid . '.log';
  106. if (self::$cur_file_name != $log_file) {
  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. }
  113. $content = "[{$pid} {$now}] {$level}: {$message}\r\n";
  114. $ret = fwrite(self::$cur_file, $content);
  115. if ($ret === false) {
  116. self::$cur_file = fopen($log_file, 'a+');
  117. fwrite(self::$cur_file, $content);
  118. }
  119. fflush(self::$cur_file);
  120. }
  121. public static function endl($lev = self::ERR)
  122. {
  123. $content = "\r\n";
  124. if ($lev == self::SQL && self::open_sql) {
  125. $log_file = BASE_DATA_PATH . '/log/' . date('Ymd', time()) . '.log';
  126. file_put_contents($log_file, $content, FILE_APPEND);
  127. return;
  128. }
  129. if ($lev >= self::cur_level) {
  130. $log_file = BASE_DATA_PATH . '/log/' . date('Ymd', time()) . '.log';
  131. file_put_contents($log_file, $content, FILE_APPEND);
  132. }
  133. }
  134. public static function msg()
  135. {
  136. $debugInfo = debug_backtrace();
  137. $stack = "[";
  138. foreach ($debugInfo as $key => $val) {
  139. if (array_key_exists("file", $val)) {
  140. $stack .= ",file:" . $val["file"];
  141. }
  142. if (array_key_exists("line", $val)) {
  143. $stack .= ",line:" . $val["line"];
  144. }
  145. if (array_key_exists("function", $val)) {
  146. $stack .= ",function:" . $val["function"];
  147. }
  148. }
  149. $stack .= "]";
  150. return $stack;
  151. }
  152. private static function get_level($lev)
  153. {
  154. if ($lev == self::INFO) return 'INFO';
  155. if ($lev == self::DEBUG) return 'DEBUG';
  156. if ($lev == self::WARING) return 'WARING';
  157. if ($lev == self::ERR) return 'ERR';
  158. if ($lev == self::RUN) return 'RUN';
  159. return 'Unknown';
  160. }
  161. public static function read()
  162. {
  163. return self::$log;
  164. }
  165. }