log.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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. $content = "[{$pid} {$now}] {$level}: {$message}\r\n";
  66. if(self::open_sql) {
  67. $log_file = BASE_DATA_PATH.'/log/'.date('Ymd',time()).'.log';
  68. file_put_contents($log_file,$content, FILE_APPEND);
  69. }
  70. self::add_sql_log($message);
  71. return;
  72. }
  73. if($lev >= self::cur_level && $lev <= self::RUN) {
  74. $level = self::get_level($lev);
  75. $log_file = BASE_DATA_PATH . '/log/' . date('Ymd', time()) . '.log';
  76. $content = "[{$pid} {$now}] {$level}: {$message}\r\n";
  77. file_put_contents($log_file, $content, FILE_APPEND);
  78. }
  79. if($lev == self::ERR) {
  80. self::msg();
  81. }
  82. }
  83. public static function endl($lev = self::ERR)
  84. {
  85. $content = "\r\n";
  86. if($lev == self::SQL && self::open_sql) {
  87. $log_file = BASE_DATA_PATH.'/log/'.date('Ymd',time()).'.log';
  88. file_put_contents($log_file,$content, FILE_APPEND);
  89. return;
  90. }
  91. if($lev >= self::cur_level) {
  92. $log_file = BASE_DATA_PATH . '/log/' . date('Ymd', time()) . '.log';
  93. file_put_contents($log_file, $content, FILE_APPEND);
  94. }
  95. }
  96. public static function msg()
  97. {
  98. $debugInfo = debug_backtrace();
  99. $stack = "[";
  100. foreach($debugInfo as $key => $val){
  101. if(array_key_exists("file", $val)){
  102. $stack .= ",file:" . $val["file"];
  103. }
  104. if(array_key_exists("line", $val)){
  105. $stack .= ",line:" . $val["line"];
  106. }
  107. if(array_key_exists("function", $val)){
  108. $stack .= ",function:" . $val["function"];
  109. }
  110. }
  111. $stack .= "]";
  112. return $stack;
  113. }
  114. private static function get_level($lev)
  115. {
  116. if($lev == self::INFO) return 'INFO';
  117. if($lev == self::DEBUG) return 'DEBUG';
  118. if($lev == self::WARING) return 'WARING';
  119. if($lev == self::ERR) return 'ERR';
  120. if($lev == self::RUN) return 'RUN';
  121. return 'Unknown';
  122. }
  123. public static function read()
  124. {
  125. return self::$log;
  126. }
  127. }