当前位置:Gxlcms > PHP教程 > php设置时区及记录日志文件的代码

php设置时区及记录日志文件的代码

时间:2021-07-01 10:21:17 帮助过:25人阅读

  1. date_default_timezone_set('Asia/Hong_Kong'); //set time zone
  2. set_error_handler("myHandler"); //set error handler
  3. $chinatime = date('Y-m-d H:i:s'); //get current time
  4. $max_size = 500000;

  5. try

  6. {
  7. $content = "Hello WeiXin!";
  8. logger2($content);
  9. //throw new Exception("Value must be 1 or below aaaaaaaaaaaaaaaaaaa");
  10. }
  11. catch(Exception $e)
  12. {
  13. logger2("Exception Message: ".$e->getMessage());
  14. }

  15. //record operation log into .log file

  16. function logger($log_content)
  17. {
  18. print_r(date('H:i:s')." ".$log_content."
    ");
  19. $log_filename = date("Ymd").".log";
  20. $file = fopen($log_filename ,"a+");
  21. fwrite($file, date('H:i:s')." ".$log_content."\r\n");
  22. fclose($file);
  23. }
  24. //record operation log into .log file
  25. function logger2($log_content)
  26. {
  27. Global $max_size;
  28. print_r(date('H:i:s')." ".$log_content." "."
    ");
  29. $log_filename = date("Ymd").".log";
  30. if(file_exists($log_filename) and (abs(filesize($log_filename)) > $max_size)){unlink($log_filename);sleep(1);}
  31. file_put_contents($log_filename, date('H:i:s')." ".$log_content." "."\r\n", FILE_APPEND);
  32. }
  33. //error handler function
  34. function myHandler($level, $message, $file, $line, $context)
  35. {
  36. logger("[ERROR] LEVEL: $level, MESSAGE: $message, FILE: $file, LINE: $line, CONTENT: $context");
  37. die();
  38. }
  39. ?>

人气教程排行