log.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. <?php
  2. /**
  3. * 记录日志
  4. ***/
  5. defined('InShopNC') or exit('Access Invalid!');
  6. class Log
  7. {
  8. const open_sql = false;
  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::ERR;
  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 && self::open_sql) {
  49. $level = 'SQL';
  50. $log_file = BASE_DATA_PATH.'/log/'.date('Ymd',time()).'-sql.log';
  51. $content = "[{$now}] {$level}: {$message}\r\n";
  52. file_put_contents($log_file,$content, FILE_APPEND);
  53. self::add_sql_log($message);
  54. return;
  55. }
  56. if($lev >= self::cur_level && $lev <= self::RUN) {
  57. $level = self::get_level($lev);
  58. $log_file = BASE_DATA_PATH . '/log/' . date('Ymd', time()) . '.log';
  59. $content = "[{$now}] {$level}: {$message}\r\n";
  60. file_put_contents($log_file, $content, FILE_APPEND);
  61. }
  62. if($lev == self::ERR) {
  63. self::msg();
  64. }
  65. // else
  66. // {
  67. // $level = self::get_level($lev);
  68. // $log_file = BASE_DATA_PATH . '/log/' . date('Ymd', TIMESTAMP) . '.log';
  69. // $url = $_SERVER['REQUEST_URI'] ? $_SERVER['REQUEST_URI'] : $_SERVER['PHP_SELF'];
  70. // $url .= " ( act={$_GET['act']}&op={$_GET['op']} ) ";
  71. // $content = "[{$now}] {$url}\r\n{$level}: {$message}\r\n";
  72. // file_put_contents($log_file, $content, FILE_APPEND);
  73. //
  74. // if($lev == self::ERR && is_mobile() != true) {
  75. // self::$log[] = "[{$now}] {$level}: {$message}\r\n";
  76. // }
  77. // }
  78. }
  79. public static function endl($lev = self::ERR)
  80. {
  81. $content = "\r\n";
  82. if($lev == self::SQL && self::open_sql) {
  83. $log_file = BASE_DATA_PATH.'/log/'.date('Ymd',TIMESTAMP).'-sql.log';
  84. file_put_contents($log_file,$content, FILE_APPEND);
  85. return;
  86. }
  87. if($lev >= self::cur_level) {
  88. $log_file = BASE_DATA_PATH . '/log/' . date('Ymd', TIMESTAMP) . '.log';
  89. file_put_contents($log_file, $content, FILE_APPEND);
  90. }
  91. }
  92. public static function msg()
  93. {
  94. $debugInfo = debug_backtrace();
  95. $stack = "[";
  96. foreach($debugInfo as $key => $val){
  97. if(array_key_exists("file", $val)){
  98. $stack .= ",file:" . $val["file"];
  99. }
  100. if(array_key_exists("line", $val)){
  101. $stack .= ",line:" . $val["line"];
  102. }
  103. if(array_key_exists("function", $val)){
  104. $stack .= ",function:" . $val["function"];
  105. }
  106. }
  107. $stack .= "]";
  108. return $stack;
  109. }
  110. private static function get_level($lev)
  111. {
  112. if($lev == self::INFO) return 'INFO';
  113. if($lev == self::DEBUG) return 'DEBUG';
  114. if($lev == self::WARING) return 'WARING';
  115. if($lev == self::ERR) return 'ERR';
  116. if($lev == self::RUN) return 'RUN';
  117. return 'Unknown';
  118. }
  119. public static function read()
  120. {
  121. return self::$log;
  122. }
  123. }