log.php 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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 cur_level = self::INFO;
  16. private static $log = array();
  17. public static function record($message, $lev = self::ERR)
  18. {
  19. $now = @date('Y-m-d H:i:s', time());
  20. if($lev == self::SQL && self::open_sql) {
  21. $level = 'SQL';
  22. $log_file = BASE_DATA_PATH.'/log/'.date('Ymd',TIMESTAMP).'-sql.log';
  23. $content = "[{$now}] {$level}: {$message}\r\n";
  24. file_put_contents($log_file,$content, FILE_APPEND);
  25. return;
  26. }
  27. if($lev >= self::cur_level && $lev <= self::RUN) {
  28. $level = self::get_level($lev);
  29. $log_file = BASE_DATA_PATH . '/log/' . date('Ymd', TIMESTAMP) . '.log';
  30. $content = "[{$now}] {$level}: {$message}\r\n";
  31. file_put_contents($log_file, $content, FILE_APPEND);
  32. }
  33. // else
  34. // {
  35. // $level = self::get_level($lev);
  36. // $log_file = BASE_DATA_PATH . '/log/' . date('Ymd', TIMESTAMP) . '.log';
  37. // $url = $_SERVER['REQUEST_URI'] ? $_SERVER['REQUEST_URI'] : $_SERVER['PHP_SELF'];
  38. // $url .= " ( act={$_GET['act']}&op={$_GET['op']} ) ";
  39. // $content = "[{$now}] {$url}\r\n{$level}: {$message}\r\n";
  40. // file_put_contents($log_file, $content, FILE_APPEND);
  41. //
  42. // if($lev == self::ERR && is_mobile() != true) {
  43. // self::$log[] = "[{$now}] {$level}: {$message}\r\n";
  44. // }
  45. // }
  46. }
  47. public static function endl($lev = self::ERR)
  48. {
  49. $content = "\r\n";
  50. if($lev == self::SQL && self::open_sql) {
  51. $log_file = BASE_DATA_PATH.'/log/'.date('Ymd',TIMESTAMP).'-sql.log';
  52. file_put_contents($log_file,$content, FILE_APPEND);
  53. return;
  54. }
  55. if($lev >= self::cur_level) {
  56. $log_file = BASE_DATA_PATH . '/log/' . date('Ymd', TIMESTAMP) . '.log';
  57. file_put_contents($log_file, $content, FILE_APPEND);
  58. }
  59. }
  60. private static function get_level($lev)
  61. {
  62. if($lev == self::INFO) return 'INFO';
  63. if($lev == self::DEBUG) return 'DEBUG';
  64. if($lev == self::WARING) return 'WARING';
  65. if($lev == self::ERR) return 'ERR';
  66. if($lev == self::RUN) return 'RUN';
  67. return 'Unknown';
  68. }
  69. public static function read()
  70. {
  71. return self::$log;
  72. }
  73. }