PHP set the time zone and log file code

Source: Internet
Author: User
Tags set time
  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. ?>
Copy Code
  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.