Home  >  Article  >  Backend Development  >  PHP code for setting time zone and recording log files

PHP code for setting time zone and recording log files

WBOY
WBOYOriginal
2016-07-25 09:03:071166browse
  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."rn");
  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." "."rn", 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. ?>
复制代码


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn