log.php 3.7 KB

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