log.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. <?php
  2. /**
  3. * 记录日志
  4. ***/
  5. defined('InShopNC') or exit('Access Invalid!');
  6. class Log
  7. {
  8. const open_sql = true;
  9. const SQL = 1;
  10. const INFO = 2;
  11. const DEBUG = 3;
  12. const WARING = 4;
  13. const ERR = 5;
  14. const RUN = 6;
  15. const WAIT_HANDLE = 10;
  16. static $moduls = array("default","session");
  17. const cur_level = self::INFO;
  18. private static $log = array();
  19. private static $stsql_log = array();
  20. public static function start_sql_log()
  21. {
  22. self::$stsql_log = array();
  23. }
  24. public static function sql_log()
  25. {
  26. return self::$stsql_log;
  27. }
  28. private static function add_sql_log($log)
  29. {
  30. if(is_array(self::$stsql_log)) {
  31. array_push(self::$stsql_log,$log);
  32. }
  33. }
  34. //public static function record($message, $lev = self::ERR,$module = 'default')
  35. public static function record($message, $lev = self::ERR)
  36. {
  37. // if(!in_array($lev,self::$moduls) && $lev < self::ERR ) {
  38. // return;
  39. // }
  40. $now = @date('Y-m-d H:i:s', time());
  41. if($lev == self::WAIT_HANDLE) {
  42. $level = 'WAIT_HANDLE';
  43. $log_file = BASE_DATA_PATH.'/log/'.date('Ymd',time()).'-wait.log';
  44. $content = "[{$now}] {$level}: {$message}\r\n";
  45. file_put_contents($log_file,$content, FILE_APPEND);
  46. return;
  47. }
  48. if($lev == self::SQL) {
  49. $level = 'SQL';
  50. $content = "[{$now}] {$level}: {$message}\r\n";
  51. if(self::open_sql) {
  52. $log_file = BASE_DATA_PATH.'/log/'.date('Ymd',time()).'-sql.log';
  53. file_put_contents($log_file,$content, FILE_APPEND);
  54. }
  55. self::add_sql_log($message);
  56. return;
  57. }
  58. if($lev >= self::cur_level && $lev <= self::RUN) {
  59. $level = self::get_level($lev);
  60. $log_file = BASE_DATA_PATH . '/log/' . date('Ymd', time()) . '.log';
  61. $content = "[{$now}] {$level}: {$message}\r\n";
  62. file_put_contents($log_file, $content, FILE_APPEND);
  63. }
  64. if($lev == self::ERR) {
  65. self::msg();
  66. }
  67. // else
  68. // {
  69. // $level = self::get_level($lev);
  70. // $log_file = BASE_DATA_PATH . '/log/' . date('Ymd', TIMESTAMP) . '.log';
  71. // $url = $_SERVER['REQUEST_URI'] ? $_SERVER['REQUEST_URI'] : $_SERVER['PHP_SELF'];
  72. // $url .= " ( act={$_GET['act']}&op={$_GET['op']} ) ";
  73. // $content = "[{$now}] {$url}\r\n{$level}: {$message}\r\n";
  74. // file_put_contents($log_file, $content, FILE_APPEND);
  75. //
  76. // if($lev == self::ERR && is_mobile() != true) {
  77. // self::$log[] = "[{$now}] {$level}: {$message}\r\n";
  78. // }
  79. // }
  80. }
  81. public static function endl($lev = self::ERR)
  82. {
  83. $content = "\r\n";
  84. if($lev == self::SQL && self::open_sql) {
  85. $log_file = BASE_DATA_PATH.'/log/'.date('Ymd',TIMESTAMP).'-sql.log';
  86. file_put_contents($log_file,$content, FILE_APPEND);
  87. return;
  88. }
  89. if($lev >= self::cur_level) {
  90. $log_file = BASE_DATA_PATH . '/log/' . date('Ymd', TIMESTAMP) . '.log';
  91. file_put_contents($log_file, $content, FILE_APPEND);
  92. }
  93. }
  94. public static function msg()
  95. {
  96. $debugInfo = debug_backtrace();
  97. $stack = "[";
  98. foreach($debugInfo as $key => $val){
  99. if(array_key_exists("file", $val)){
  100. $stack .= ",file:" . $val["file"];
  101. }
  102. if(array_key_exists("line", $val)){
  103. $stack .= ",line:" . $val["line"];
  104. }
  105. if(array_key_exists("function", $val)){
  106. $stack .= ",function:" . $val["function"];
  107. }
  108. }
  109. $stack .= "]";
  110. return $stack;
  111. }
  112. private static function get_level($lev)
  113. {
  114. if($lev == self::INFO) return 'INFO';
  115. if($lev == self::DEBUG) return 'DEBUG';
  116. if($lev == self::WARING) return 'WARING';
  117. if($lev == self::ERR) return 'ERR';
  118. if($lev == self::RUN) return 'RUN';
  119. return 'Unknown';
  120. }
  121. public static function read()
  122. {
  123. return self::$log;
  124. }
  125. }