TelegramErrorLogger.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. <?php
  2. /**
  3. * Telegram Error Logger Class.
  4. *
  5. * @author shakibonline <shakiba_9@yahoo.com>
  6. */
  7. class TelegramErrorLogger
  8. {
  9. private static $self;
  10. /// Log request and response parameters from/to Telegrsm API
  11. /**
  12. * Prints the list of parameters from/to Telegram's API endpoint
  13. * \param $result the Telegram's response as array
  14. * \param $content the request parameters as array.
  15. */
  16. public static function log($result, $content, $use_rt = true)
  17. {
  18. try {
  19. if ($result['ok'] === false) {
  20. self::$self = new self();
  21. $e = new \Exception();
  22. $error = PHP_EOL;
  23. $error .= '==========[Response]==========';
  24. $error .= "\n";
  25. foreach ($result as $key => $value) {
  26. if ($value == false) {
  27. $error .= $key.":\t\t\tFalse\n";
  28. } else {
  29. $error .= $key.":\t\t".$value."\n";
  30. }
  31. }
  32. $array = '=========[Sent Data]==========';
  33. $array .= "\n";
  34. if ($use_rt == true) {
  35. foreach ($content as $item) {
  36. $array .= self::$self->rt($item).PHP_EOL.PHP_EOL;
  37. }
  38. } else {
  39. foreach ($content as $key => $value) {
  40. $array .= $key.":\t\t".$value."\n";
  41. }
  42. }
  43. $backtrace = '============[Trace]===========';
  44. $backtrace .= "\n";
  45. $backtrace .= $e->getTraceAsString();
  46. self::$self->_log_to_file($error.$array.$backtrace);
  47. }
  48. } catch (\Exception $e) {
  49. echo $e->getMessage();
  50. }
  51. }
  52. /// Write a string in the log file adding the current server time
  53. /**
  54. * Write a string in the log file TelegramErrorLogger.txt adding the current server time
  55. * \param $error_text the text to append in the log.
  56. */
  57. private function _log_to_file($error_text)
  58. {
  59. try {
  60. $fileName = __CLASS__.'.txt';
  61. $myFile = fopen($fileName, 'a+');
  62. $date = '============[Date]============';
  63. $date .= "\n";
  64. $date .= '[ '.date('Y-m-d H:i:s e').' ] ';
  65. fwrite($myFile, $date.$error_text."\n\n");
  66. fclose($myFile);
  67. } catch (\Exception $e) {
  68. echo $e->getMessage();
  69. }
  70. }
  71. private function rt($array, $title = null, $head = true)
  72. {
  73. $ref = 'ref';
  74. $text = '';
  75. if ($head) {
  76. $text = "[$ref]";
  77. $text .= "\n";
  78. }
  79. foreach ($array as $key => $value) {
  80. if ($value instanceof CURLFile) {
  81. $text .= $ref.'.'.$key.'= File'.PHP_EOL;
  82. } elseif (is_array($value)) {
  83. if ($title != null) {
  84. $key = $title.'.'.$key;
  85. }
  86. $text .= self::rt($value, $key, false);
  87. } else {
  88. if (is_bool($value)) {
  89. $value = ($value) ? 'true' : 'false';
  90. }
  91. if ($title != '') {
  92. $text .= $ref.'.'.$title.'.'.$key.'= '.$value.PHP_EOL;
  93. } else {
  94. $text .= $ref.'.'.$key.'= '.$value.PHP_EOL;
  95. }
  96. }
  97. }
  98. return $text;
  99. }
  100. }