Skip to content

fix Exception Log #108

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Mar 31, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -432,8 +432,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
Expand Down
10 changes: 3 additions & 7 deletions src/Exception/TelegramException.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@

namespace Longman\TelegramBot\Exception;

use Longman\TelegramBot\Logger;

/**
* Main exception class used for exception handling
*/
Expand All @@ -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());
}
}
53 changes: 53 additions & 0 deletions src/Logger.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?php
/**
* This file is part of the TelegramBot package.
*
* (c) Avtandil Kikabidze aka LONGMAN <[email protected]>
*
* 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;
}
}