Skip to content

Commit b1184d8

Browse files
committed
fix Exception Log
1 parent f548fa0 commit b1184d8

File tree

3 files changed

+60
-8
lines changed

3 files changed

+60
-8
lines changed

README.md

+4-1
Original file line numberDiff line numberDiff line change
@@ -427,8 +427,11 @@ $telegram->setUploadPath('yourpath/Upload');
427427
```
428428

429429
### Logging
430+
Thrown Exceptions are not stored by default. You can Enable this feature adding this line in your 'webhook.php' or 'getUpdates.php'
430431

431-
Thrown Exceptions are stored in *TelegramException.log* file (in the base directory).
432+
```php
433+
Longman\TelegramBot\Logger::initialize('your_path/TelegramException.log');
434+
```
432435

433436
Incoming update (json string from webhook and getUpdates) can be logged in a text file. Set those options with the methods:
434437
```php

src/Exception/TelegramException.php

+3-7
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010

1111
namespace Longman\TelegramBot\Exception;
1212

13+
use Longman\TelegramBot\Logger;
14+
1315
/**
1416
* Main exception class used for exception handling
1517
*/
@@ -24,12 +26,6 @@ class TelegramException extends \Exception
2426
public function __construct($message, $code = 0)
2527
{
2628
parent::__construct($message, $code);
27-
28-
$path = 'TelegramException.log';
29-
$status = file_put_contents(
30-
$path,
31-
date('Y-m-d H:i:s', time()) . ' ' . self::__toString() . "\n",
32-
FILE_APPEND
33-
);
29+
Logger::logException(self::__toString());
3430
}
3531
}

src/Logger.php

+53
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
<?php
2+
/**
3+
* This file is part of the TelegramBot package.
4+
*
5+
* (c) Avtandil Kikabidze aka LONGMAN <[email protected]>
6+
*
7+
* For the full copyright and license information, please view the LICENSE
8+
* file that was distributed with this source code.
9+
*/
10+
11+
namespace Longman\TelegramBot;
12+
13+
/**
14+
* Class Logger.
15+
*/
16+
class Logger
17+
{
18+
/**
19+
* Exception log path
20+
*
21+
* @var string
22+
*/
23+
static protected $exception_log_path = null;
24+
25+
/**
26+
* Initialize
27+
*
28+
* @param string $exception_log_path
29+
*/
30+
public static function initialize($exception_log_path)
31+
{
32+
self::$exception_log_path = $exception_log_path;
33+
}
34+
35+
/**
36+
* Log exception
37+
*
38+
* @param string $text
39+
*
40+
* @return bool
41+
*/
42+
public static function logException($text)
43+
{
44+
if (!is_null(self::$exception_log_path)) {
45+
return file_put_contents(
46+
self::$exception_log_path,
47+
date('Y-m-d H:i:s', time()) . ' ' . $text . "\n",
48+
FILE_APPEND
49+
);
50+
}
51+
return 0;
52+
}
53+
}

0 commit comments

Comments
 (0)