欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  后端开发

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

程序员文章站 2022-04-02 12:00:44
...
  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. ?>
复制代码