From b1184d862d17fb8e300d57944658a9608742f904 Mon Sep 17 00:00:00 2001 From: MBoretto Date: Sat, 27 Feb 2016 01:43:13 +0100 Subject: [PATCH] fix Exception Log --- README.md | 5 ++- src/Exception/TelegramException.php | 10 ++---- src/Logger.php | 53 +++++++++++++++++++++++++++++ 3 files changed, 60 insertions(+), 8 deletions(-) create mode 100644 src/Logger.php diff --git a/README.md b/README.md index 38f209782..05a136ff8 100644 --- a/README.md +++ b/README.md @@ -427,8 +427,11 @@ $telegram->setUploadPath('yourpath/Upload'); ``` ### Logging +Thrown Exceptions are not stored by default. You can Enable this feature adding this line in your 'webhook.php' or 'getUpdates.php' -Thrown Exceptions are stored in *TelegramException.log* file (in the base directory). +```php + Longman\TelegramBot\Logger::initialize('your_path/TelegramException.log'); +``` Incoming update (json string from webhook and getUpdates) can be logged in a text file. Set those options with the methods: ```php diff --git a/src/Exception/TelegramException.php b/src/Exception/TelegramException.php index a9aef155a..7a600d2a5 100644 --- a/src/Exception/TelegramException.php +++ b/src/Exception/TelegramException.php @@ -10,6 +10,8 @@ namespace Longman\TelegramBot\Exception; +use Longman\TelegramBot\Logger; + /** * Main exception class used for exception handling */ @@ -24,12 +26,6 @@ class TelegramException extends \Exception public function __construct($message, $code = 0) { parent::__construct($message, $code); - - $path = 'TelegramException.log'; - $status = file_put_contents( - $path, - date('Y-m-d H:i:s', time()) . ' ' . self::__toString() . "\n", - FILE_APPEND - ); + Logger::logException(self::__toString()); } } diff --git a/src/Logger.php b/src/Logger.php new file mode 100644 index 000000000..19e1b4380 --- /dev/null +++ b/src/Logger.php @@ -0,0 +1,53 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Longman\TelegramBot; + +/** + * Class Logger. + */ +class Logger +{ + /** + * Exception log path + * + * @var string + */ + static protected $exception_log_path = null; + + /** + * Initialize + * + * @param string $exception_log_path + */ + public static function initialize($exception_log_path) + { + self::$exception_log_path = $exception_log_path; + } + + /** + * Log exception + * + * @param string $text + * + * @return bool + */ + public static function logException($text) + { + if (!is_null(self::$exception_log_path)) { + return file_put_contents( + self::$exception_log_path, + date('Y-m-d H:i:s', time()) . ' ' . $text . "\n", + FILE_APPEND + ); + } + return 0; + } +}