log.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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. public static function record($message, $lev = self::ERR)
  53. {
  54. $now = @date('Y-m-d H:i:s', time());
  55. $pid = posix_getpid();
  56. if($lev == self::WAIT_HANDLE) {
  57. $level = 'WAIT_HANDLE';
  58. $log_file = BASE_DATA_PATH.'/log/'.date('Ymd',time()).'-wait.log';
  59. $content = "[{$pid} {$now}] {$level}: {$message}\r\n";
  60. file_put_contents($log_file,$content, FILE_APPEND);
  61. return;
  62. }
  63. if($lev == self::SQL) {
  64. $level = 'SQL';
  65. if(self::open_sql) {
  66. self::write($message,$level);
  67. }
  68. //self::add_sql_log($message);
  69. return;
  70. }
  71. if($lev >= self::cur_level && $lev <= self::RUN) {
  72. $level = self::get_level($lev);
  73. self::write($message,$level);
  74. }
  75. if($lev == self::ERR) {
  76. self::msg();
  77. }
  78. }
  79. private static $cur_file_name;
  80. private static $cur_file = null;
  81. private static function write($message,$level)
  82. {
  83. $now = @date('Y-m-d H:i:s', time());
  84. $pid = posix_getpid();
  85. $log_file = BASE_DATA_PATH . '/log/' . date('Ymd', time()) . '.log';
  86. if(self::$cur_file_name != $log_file)
  87. {
  88. if(self::$cur_file != null) {
  89. fclose(self::$cur_file);
  90. }
  91. self::$cur_file = fopen($log_file,'a');
  92. }
  93. $content = "[{$pid} {$now}] {$level}: {$message}\r\n";
  94. $ret = fwrite(self::$cur_file,$content);
  95. if($ret === false) {
  96. self::$cur_file = fopen($log_file,'a');
  97. fwrite(self::$cur_file,$content);
  98. }
  99. }
  100. public static function endl($lev = self::ERR)
  101. {
  102. $content = "\r\n";
  103. if($lev == self::SQL && self::open_sql) {
  104. $log_file = BASE_DATA_PATH.'/log/'.date('Ymd',time()).'.log';
  105. file_put_contents($log_file,$content, FILE_APPEND);
  106. return;
  107. }
  108. if($lev >= self::cur_level) {
  109. $log_file = BASE_DATA_PATH . '/log/' . date('Ymd', time()) . '.log';
  110. file_put_contents($log_file, $content, FILE_APPEND);
  111. }
  112. }
  113. public static function msg()
  114. {
  115. $debugInfo = debug_backtrace();
  116. $stack = "[";
  117. foreach($debugInfo as $key => $val){
  118. if(array_key_exists("file", $val)){
  119. $stack .= ",file:" . $val["file"];
  120. }
  121. if(array_key_exists("line", $val)){
  122. $stack .= ",line:" . $val["line"];
  123. }
  124. if(array_key_exists("function", $val)){
  125. $stack .= ",function:" . $val["function"];
  126. }
  127. }
  128. $stack .= "]";
  129. return $stack;
  130. }
  131. private static function get_level($lev)
  132. {
  133. if($lev == self::INFO) return 'INFO';
  134. if($lev == self::DEBUG) return 'DEBUG';
  135. if($lev == self::WARING) return 'WARING';
  136. if($lev == self::ERR) return 'ERR';
  137. if($lev == self::RUN) return 'RUN';
  138. return 'Unknown';
  139. }
  140. public static function read()
  141. {
  142. return self::$log;
  143. }
  144. }