errorfunc.php 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. <?php
  2. namespace Safe;
  3. use Safe\Exceptions\ErrorfuncException;
  4. /**
  5. * Sends an error message to the web server's error log or to a file.
  6. *
  7. * @param string $message The error message that should be logged.
  8. * @param int $message_type Says where the error should go. The possible message types are as
  9. * follows:
  10. *
  11. *
  12. * error_log log types
  13. *
  14. *
  15. *
  16. * 0
  17. *
  18. * message is sent to PHP's system logger, using
  19. * the Operating System's system logging mechanism or a file, depending
  20. * on what the error_log
  21. * configuration directive is set to. This is the default option.
  22. *
  23. *
  24. *
  25. * 1
  26. *
  27. * message is sent by email to the address in
  28. * the destination parameter. This is the only
  29. * message type where the fourth parameter,
  30. * extra_headers is used.
  31. *
  32. *
  33. *
  34. * 2
  35. *
  36. * No longer an option.
  37. *
  38. *
  39. *
  40. * 3
  41. *
  42. * message is appended to the file
  43. * destination. A newline is not automatically
  44. * added to the end of the message string.
  45. *
  46. *
  47. *
  48. * 4
  49. *
  50. * message is sent directly to the SAPI logging
  51. * handler.
  52. *
  53. *
  54. *
  55. *
  56. *
  57. * @param string $destination The destination. Its meaning depends on the
  58. * message_type parameter as described above.
  59. * @param string $extra_headers The extra headers. It's used when the message_type
  60. * parameter is set to 1.
  61. * This message type uses the same internal function as
  62. * mail does.
  63. * @throws ErrorfuncException
  64. *
  65. */
  66. function error_log(string $message, int $message_type = 0, string $destination = null, string $extra_headers = null): void
  67. {
  68. error_clear_last();
  69. if ($extra_headers !== null) {
  70. $result = \error_log($message, $message_type, $destination, $extra_headers);
  71. } elseif ($destination !== null) {
  72. $result = \error_log($message, $message_type, $destination);
  73. } else {
  74. $result = \error_log($message, $message_type);
  75. }
  76. if ($result === false) {
  77. throw ErrorfuncException::createFromPhpError();
  78. }
  79. }