首页  >  文章  >  后端开发  >  php设置时区及记录日志文件的代码

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

WBOY
WBOY原创
2016-07-25 09:03:071153浏览
  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. ?>
复制代码


声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn