Rename Logger class

parent 6227c407
...@@ -14,7 +14,7 @@ use Longman\TelegramBot\Commands\AdminCommand; ...@@ -14,7 +14,7 @@ use Longman\TelegramBot\Commands\AdminCommand;
use Longman\TelegramBot\DB; use Longman\TelegramBot\DB;
use Longman\TelegramBot\Exception\TelegramException; use Longman\TelegramBot\Exception\TelegramException;
use Longman\TelegramBot\Http\Client; use Longman\TelegramBot\Http\Client;
use Longman\TelegramBot\TelegramLog; use Longman\TelegramBot\Logger;
use PDOException; use PDOException;
/** /**
...@@ -386,7 +386,7 @@ class CleanupCommand extends AdminCommand ...@@ -386,7 +386,7 @@ class CleanupCommand extends AdminCommand
if ($dbq = $pdo->query($query)) { if ($dbq = $pdo->query($query)) {
$rows += $dbq->rowCount(); $rows += $dbq->rowCount();
} else { } else {
TelegramLog::error('Error while executing query: ' . $query); Logger::error('Error while executing query: ' . $query);
} }
} }
......
...@@ -149,7 +149,7 @@ abstract class Command ...@@ -149,7 +149,7 @@ abstract class Command
*/ */
public function preExecute() public function preExecute()
{ {
if ($this->need_mysql && !($this->telegram->isDbEnabled() && DB::isDbConnected())) { if ($this->need_mysql && !(DB::isEnabled() && DB::isDbConnected())) {
return $this->executeNoDb(); return $this->executeNoDb();
} }
......
...@@ -71,7 +71,7 @@ class Kernel ...@@ -71,7 +71,7 @@ class Kernel
} }
if ($this->app->last_update_id !== null) { if ($this->app->last_update_id !== null) {
$offset = $this->app->last_update_id + 1; //As explained in the telegram bot API documentation $offset = $this->app->last_update_id + 1; // As explained in the telegram bot API documentation
} }
$response = Client::getUpdates( $response = Client::getUpdates(
...@@ -91,7 +91,7 @@ class Kernel ...@@ -91,7 +91,7 @@ class Kernel
$this->app->processUpdate($result); $this->app->processUpdate($result);
} }
if (! DB::isDbConnected() && ! $custom_input && $this->app->last_update_id !== null && $offset === 0) { if (! DB::isDbConnected() && $this->app->last_update_id !== null && $offset === 0) {
// Mark update(s) as read after handling // Mark update(s) as read after handling
Client::getUpdates( Client::getUpdates(
[ [
......
...@@ -31,28 +31,35 @@ class DB ...@@ -31,28 +31,35 @@ class DB
* *
* @var array * @var array
*/ */
static protected $mysql_credentials = []; protected static $mysql_credentials = [];
/** /**
* PDO object * PDO object
* *
* @var PDO * @var PDO
*/ */
static protected $pdo; protected static $pdo;
/** /**
* Table prefix * Table prefix
* *
* @var string * @var string
*/ */
static protected $table_prefix; protected static $table_prefix;
/** /**
* Telegram class object * Telegram class object
* *
* @var Telegram * @var Telegram
*/ */
static protected $telegram; protected static $telegram;
/**
* Enabled
*
* @var bool
*/
protected static $enabled = false;
/** /**
* Initialize * Initialize
...@@ -1189,4 +1196,25 @@ class DB ...@@ -1189,4 +1196,25 @@ class DB
throw new TelegramException($e->getMessage()); throw new TelegramException($e->getMessage());
} }
} }
/**
* Return enabled status
*
* @return bool
*/
public static function isEnabled()
{
return self::$enabled;
}
/**
* Set enabled status
*
* @param $enabled bool
* @return void
*/
public static function setEnabled($enabled)
{
self::$enabled = $enabled;
}
} }
...@@ -12,7 +12,7 @@ namespace Longman\TelegramBot\Entities; ...@@ -12,7 +12,7 @@ namespace Longman\TelegramBot\Entities;
use Exception; use Exception;
use Longman\TelegramBot\Entities\InlineQuery\InlineEntity; use Longman\TelegramBot\Entities\InlineQuery\InlineEntity;
use Longman\TelegramBot\TelegramLog; use Longman\TelegramBot\Logger;
/** /**
* Class Entity * Class Entity
......
...@@ -14,7 +14,7 @@ use GuzzleHttp\Client; ...@@ -14,7 +14,7 @@ use GuzzleHttp\Client;
use GuzzleHttp\Exception\RequestException; use GuzzleHttp\Exception\RequestException;
use Longman\TelegramBot\Entities\Update; use Longman\TelegramBot\Entities\Update;
use Longman\TelegramBot\Exception\TelegramException; use Longman\TelegramBot\Exception\TelegramException;
use Longman\TelegramBot\TelegramLog; use Longman\TelegramBot\Logger;
/** /**
* Class Botan * Class Botan
...@@ -167,7 +167,7 @@ class Botan ...@@ -167,7 +167,7 @@ class Botan
} }
if (empty($event_name)) { if (empty($event_name)) {
TelegramLog::error('Botan.io stats report failed, no suitable update object found!'); Logger::error('Botan.io stats report failed, no suitable update object found!');
return false; return false;
} }
...@@ -199,7 +199,7 @@ class Botan ...@@ -199,7 +199,7 @@ class Botan
$responseData = json_decode($result, true); $responseData = json_decode($result, true);
if (! $responseData || $responseData['status'] !== 'accepted') { if (! $responseData || $responseData['status'] !== 'accepted') {
TelegramLog::debug('Botan.io stats report failed: %s', $result ?: 'empty response'); Logger::debug('Botan.io stats report failed: %s', $result ?: 'empty response');
return false; return false;
} }
...@@ -246,7 +246,7 @@ class Botan ...@@ -246,7 +246,7 @@ class Botan
} }
if (filter_var($result, FILTER_VALIDATE_URL) === false) { if (filter_var($result, FILTER_VALIDATE_URL) === false) {
TelegramLog::debug('Botan.io URL shortening failed for "%s": %s', $url, $result ?: 'empty response'); Logger::debug('Botan.io URL shortening failed for "%s": %s', $url, $result ?: 'empty response');
return $url; return $url;
} }
......
...@@ -16,7 +16,7 @@ use Longman\TelegramBot\DB; ...@@ -16,7 +16,7 @@ use Longman\TelegramBot\DB;
use Longman\TelegramBot\Entities\File; use Longman\TelegramBot\Entities\File;
use Longman\TelegramBot\Exception\TelegramException; use Longman\TelegramBot\Exception\TelegramException;
use Longman\TelegramBot\Telegram; use Longman\TelegramBot\Telegram;
use Longman\TelegramBot\TelegramLog; use Longman\TelegramBot\Logger;
/** /**
* Class Client * Class Client
...@@ -380,7 +380,7 @@ class Client ...@@ -380,7 +380,7 @@ class Client
$result = null; $result = null;
$request_params = self::setUpRequestParams($data); $request_params = self::setUpRequestParams($data);
$request_params['debug'] = TelegramLog::getDebugLogTempStream(); $request_params['debug'] = Logger::getDebugLogTempStream();
try { try {
$response = self::$client->post( $response = self::$client->post(
...@@ -391,13 +391,13 @@ class Client ...@@ -391,13 +391,13 @@ class Client
// Logging getUpdates Update // Logging getUpdates Update
if ($action === 'getUpdates') { if ($action === 'getUpdates') {
TelegramLog::update($result); Logger::update($result);
} }
} catch (RequestException $e) { } catch (RequestException $e) {
$result = ($e->getResponse()) ? (string) $e->getResponse()->getBody() : ''; $result = ($e->getResponse()) ? (string) $e->getResponse()->getBody() : '';
} finally { } finally {
// Logging verbose debug output // Logging verbose debug output
TelegramLog::endDebugLogTempStream('Verbose HTTP Request output:' . PHP_EOL . '%s' . PHP_EOL); Logger::endDebugLogTempStream('Verbose HTTP Request output:' . PHP_EOL . '%s' . PHP_EOL);
} }
return $result; return $result;
...@@ -427,7 +427,7 @@ class Client ...@@ -427,7 +427,7 @@ class Client
throw new TelegramException('Directory ' . $file_dir . ' can\'t be created'); throw new TelegramException('Directory ' . $file_dir . ' can\'t be created');
} }
$debug_handle = TelegramLog::getDebugLogTempStream(); $debug_handle = Logger::getDebugLogTempStream();
try { try {
self::$client->get( self::$client->get(
...@@ -440,7 +440,7 @@ class Client ...@@ -440,7 +440,7 @@ class Client
return ($e->getResponse()) ? (string) $e->getResponse()->getBody() : ''; return ($e->getResponse()) ? (string) $e->getResponse()->getBody() : '';
} finally { } finally {
//Logging verbose debug output //Logging verbose debug output
TelegramLog::endDebugLogTempStream('Verbose HTTP File Download Request output:' . PHP_EOL . '%s' . PHP_EOL); Logger::endDebugLogTempStream('Verbose HTTP File Download Request output:' . PHP_EOL . '%s' . PHP_EOL);
} }
} }
......
...@@ -13,9 +13,9 @@ namespace Longman\TelegramBot; ...@@ -13,9 +13,9 @@ namespace Longman\TelegramBot;
use Longman\TelegramBot\Exception\TelegramLogException; use Longman\TelegramBot\Exception\TelegramLogException;
use Monolog\Formatter\LineFormatter; use Monolog\Formatter\LineFormatter;
use Monolog\Handler\StreamHandler; use Monolog\Handler\StreamHandler;
use Monolog\Logger; use Monolog\Logger as MonologLogger;
class TelegramLog class Logger
{ {
/** /**
* Monolog instance * Monolog instance
...@@ -66,7 +66,7 @@ class TelegramLog ...@@ -66,7 +66,7 @@ class TelegramLog
* *
* @return \Monolog\Logger * @return \Monolog\Logger
*/ */
public static function initialize(Logger $external_monolog = null) public static function initialize(MonologLogger $external_monolog = null)
{ {
if (self::$monolog === null) { if (self::$monolog === null) {
if ($external_monolog !== null) { if ($external_monolog !== null) {
...@@ -81,7 +81,7 @@ class TelegramLog ...@@ -81,7 +81,7 @@ class TelegramLog
} }
} }
} else { } else {
self::$monolog = new Logger('bot_log'); self::$monolog = new MonologLogger('bot_log');
} }
} }
...@@ -107,7 +107,7 @@ class TelegramLog ...@@ -107,7 +107,7 @@ class TelegramLog
self::$error_log_path = $path; self::$error_log_path = $path;
return self::$monolog->pushHandler( return self::$monolog->pushHandler(
(new StreamHandler(self::$error_log_path, Logger::ERROR)) (new StreamHandler(self::$error_log_path, MonologLogger::ERROR))
->setFormatter(new LineFormatter(null, null, true)) ->setFormatter(new LineFormatter(null, null, true))
); );
} }
...@@ -131,7 +131,7 @@ class TelegramLog ...@@ -131,7 +131,7 @@ class TelegramLog
self::$debug_log_path = $path; self::$debug_log_path = $path;
return self::$monolog->pushHandler( return self::$monolog->pushHandler(
(new StreamHandler(self::$debug_log_path, Logger::DEBUG)) (new StreamHandler(self::$debug_log_path, MonologLogger::DEBUG))
->setFormatter(new LineFormatter(null, null, true)) ->setFormatter(new LineFormatter(null, null, true))
); );
} }
...@@ -186,10 +186,10 @@ class TelegramLog ...@@ -186,10 +186,10 @@ class TelegramLog
self::$update_log_path = $path; self::$update_log_path = $path;
if (self::$monolog_update === null) { if (self::$monolog_update === null) {
self::$monolog_update = new Logger('bot_update_log'); self::$monolog_update = new MonologLogger('bot_update_log');
self::$monolog_update->pushHandler( self::$monolog_update->pushHandler(
(new StreamHandler(self::$update_log_path, Logger::INFO)) (new StreamHandler(self::$update_log_path, MonologLogger::INFO))
->setFormatter(new LineFormatter('%message%' . PHP_EOL)) ->setFormatter(new LineFormatter('%message%' . PHP_EOL))
); );
} }
......
...@@ -73,27 +73,6 @@ class Telegram ...@@ -73,27 +73,6 @@ class Telegram
*/ */
protected $update; protected $update;
/**
* Upload path
*
* @var string
*/
protected $upload_path;
/**
* Download path
*
* @var string
*/
protected $download_path;
/**
* MySQL integration
*
* @var boolean
*/
protected $mysql_enabled = false;
/** /**
* PDO object * PDO object
* *
...@@ -228,8 +207,8 @@ class Telegram ...@@ -228,8 +207,8 @@ class Telegram
public function enableMySql(array $credential, $table_prefix = null, $encoding = 'utf8mb4') public function enableMySql(array $credential, $table_prefix = null, $encoding = 'utf8mb4')
{ {
$this->pdo = DB::initialize($credential, $this, $table_prefix, $encoding); $this->pdo = DB::initialize($credential, $this, $table_prefix, $encoding);
DB::setEnabled(true);
ConversationDB::initializeConversation(); ConversationDB::initializeConversation();
$this->mysql_enabled = true;
return $this; return $this;
} }
...@@ -246,8 +225,8 @@ class Telegram ...@@ -246,8 +225,8 @@ class Telegram
public function enableExternalMySql($external_pdo_connection, $table_prefix = null) public function enableExternalMySql($external_pdo_connection, $table_prefix = null)
{ {
$this->pdo = DB::externalInitialize($external_pdo_connection, $this, $table_prefix); $this->pdo = DB::externalInitialize($external_pdo_connection, $this, $table_prefix);
DB::setEnabled(true);
ConversationDB::initializeConversation(); ConversationDB::initializeConversation();
$this->mysql_enabled = true;
return $this; return $this;
} }
...@@ -461,7 +440,7 @@ class Telegram ...@@ -461,7 +440,7 @@ class Telegram
// Make sure we don't try to process update that was already processed // Make sure we don't try to process update that was already processed
$last_id = DB::selectTelegramUpdate(1, $update->getUpdateId()); $last_id = DB::selectTelegramUpdate(1, $update->getUpdateId());
if ($last_id && count($last_id) === 1) { if ($last_id && count($last_id) === 1) {
TelegramLog::debug('Duplicate update received, processing aborted!'); Logger::debug('Duplicate update received, processing aborted!');
return new Response(['ok' => true, 'result' => true]); return new Response(['ok' => true, 'result' => true]);
} }
...@@ -596,20 +575,6 @@ class Telegram ...@@ -596,20 +575,6 @@ class Telegram
return ($user_id === null) ? false : in_array($user_id, $admins, true); return ($user_id === null) ? false : in_array($user_id, $admins, true);
} }
/**
* Check if user required the db connection
*
* @return bool
*/
public function isDbEnabled()
{
if ($this->mysql_enabled) {
return true;
} else {
return false;
}
}
/** /**
* Add a single custom commands path * Add a single custom commands path
* *
...@@ -660,7 +625,7 @@ class Telegram ...@@ -660,7 +625,7 @@ class Telegram
*/ */
public function setUploadPath($path) public function setUploadPath($path)
{ {
$this->upload_path = $path; $this->getConfig()->setUploadPath($path);
return $this; return $this;
} }
...@@ -672,7 +637,7 @@ class Telegram ...@@ -672,7 +637,7 @@ class Telegram
*/ */
public function getUploadPath() public function getUploadPath()
{ {
return $this->upload_path; return $this->getConfig()->getUploadPath();
} }
/** /**
...@@ -684,7 +649,7 @@ class Telegram ...@@ -684,7 +649,7 @@ class Telegram
*/ */
public function setDownloadPath($path) public function setDownloadPath($path)
{ {
$this->download_path = $path; $this->getConfig()->setDownloadPath($path);
return $this; return $this;
} }
...@@ -696,7 +661,7 @@ class Telegram ...@@ -696,7 +661,7 @@ class Telegram
*/ */
public function getDownloadPath() public function getDownloadPath()
{ {
return $this->download_path; return $this->getConfig()->getDownloadPath();
} }
/** /**
......
...@@ -10,7 +10,7 @@ ...@@ -10,7 +10,7 @@
namespace Longman\TelegramBot\Tests\Unit; namespace Longman\TelegramBot\Tests\Unit;
use Longman\TelegramBot\TelegramLog; use Longman\TelegramBot\Logger;
use Monolog\Handler\StreamHandler; use Monolog\Handler\StreamHandler;
use Monolog\Logger; use Monolog\Logger;
...@@ -36,7 +36,7 @@ class TelegramLogTest extends TestCase ...@@ -36,7 +36,7 @@ class TelegramLogTest extends TestCase
protected function setUp() protected function setUp()
{ {
// Make sure no monolog instance is set before each test. // Make sure no monolog instance is set before each test.
TestHelpers::setStaticProperty('Longman\TelegramBot\TelegramLog', 'monolog', null); TestHelpers::setStaticProperty('Longman\TelegramBot\Logger', 'monolog', null);
} }
protected function tearDown() protected function tearDown()
...@@ -52,7 +52,7 @@ class TelegramLogTest extends TestCase ...@@ -52,7 +52,7 @@ class TelegramLogTest extends TestCase
*/ */
public function testNewInstanceWithoutErrorPath() public function testNewInstanceWithoutErrorPath()
{ {
TelegramLog::initErrorLog(''); Logger::initErrorLog('');
} }
/** /**
...@@ -60,7 +60,7 @@ class TelegramLogTest extends TestCase ...@@ -60,7 +60,7 @@ class TelegramLogTest extends TestCase
*/ */
public function testNewInstanceWithoutDebugPath() public function testNewInstanceWithoutDebugPath()
{ {
TelegramLog::initDebugLog(''); Logger::initDebugLog('');
} }
/** /**
...@@ -68,17 +68,17 @@ class TelegramLogTest extends TestCase ...@@ -68,17 +68,17 @@ class TelegramLogTest extends TestCase
*/ */
public function testNewInstanceWithoutUpdatePath() public function testNewInstanceWithoutUpdatePath()
{ {
TelegramLog::initUpdateLog(''); Logger::initUpdateLog('');
} }
public function testErrorStream() public function testErrorStream()
{ {
$file = self::$logfiles['error']; $file = self::$logfiles['error'];
$this->assertFileNotExists($file); $this->assertFileNotExists($file);
TelegramLog::initErrorLog($file); Logger::initErrorLog($file);
TelegramLog::error('my error'); Logger::error('my error');
TelegramLog::error('my 50% error'); Logger::error('my 50% error');
TelegramLog::error('my %s error', 'placeholder'); Logger::error('my %s error', 'placeholder');
$this->assertFileExists($file); $this->assertFileExists($file);
$error_log = file_get_contents($file); $error_log = file_get_contents($file);
$this->assertContains('bot_log.ERROR: my error', $error_log); $this->assertContains('bot_log.ERROR: my error', $error_log);
...@@ -90,10 +90,10 @@ class TelegramLogTest extends TestCase ...@@ -90,10 +90,10 @@ class TelegramLogTest extends TestCase
{ {
$file = self::$logfiles['debug']; $file = self::$logfiles['debug'];
$this->assertFileNotExists($file); $this->assertFileNotExists($file);
TelegramLog::initDebugLog($file); Logger::initDebugLog($file);
TelegramLog::debug('my debug'); Logger::debug('my debug');
TelegramLog::debug('my 50% debug'); Logger::debug('my 50% debug');
TelegramLog::debug('my %s debug', 'placeholder'); Logger::debug('my %s debug', 'placeholder');
$this->assertFileExists($file); $this->assertFileExists($file);
$debug_log = file_get_contents($file); $debug_log = file_get_contents($file);
$this->assertContains('bot_log.DEBUG: my debug', $debug_log); $this->assertContains('bot_log.DEBUG: my debug', $debug_log);
...@@ -105,10 +105,10 @@ class TelegramLogTest extends TestCase ...@@ -105,10 +105,10 @@ class TelegramLogTest extends TestCase
{ {
$file = self::$logfiles['update']; $file = self::$logfiles['update'];
$this->assertFileNotExists($file); $this->assertFileNotExists($file);
TelegramLog::initUpdateLog($file); Logger::initUpdateLog($file);
TelegramLog::update('my update'); Logger::update('my update');
TelegramLog::update('my 50% update'); Logger::update('my 50% update');
TelegramLog::update('my %s update', 'placeholder'); Logger::update('my %s update', 'placeholder');
$this->assertFileExists($file); $this->assertFileExists($file);
$debug_log = file_get_contents($file); $debug_log = file_get_contents($file);
$this->assertContains('my update', $debug_log); $this->assertContains('my update', $debug_log);
...@@ -125,13 +125,13 @@ class TelegramLogTest extends TestCase ...@@ -125,13 +125,13 @@ class TelegramLogTest extends TestCase
$external_monolog->pushHandler(new StreamHandler($file, Logger::ERROR)); $external_monolog->pushHandler(new StreamHandler($file, Logger::ERROR));
$external_monolog->pushHandler(new StreamHandler($file, Logger::DEBUG)); $external_monolog->pushHandler(new StreamHandler($file, Logger::DEBUG));
TelegramLog::initialize($external_monolog); Logger::initialize($external_monolog);
TelegramLog::error('my error'); Logger::error('my error');
TelegramLog::error('my 50% error'); Logger::error('my 50% error');
TelegramLog::error('my %s error', 'placeholder'); Logger::error('my %s error', 'placeholder');
TelegramLog::debug('my debug'); Logger::debug('my debug');
TelegramLog::debug('my 50% debug'); Logger::debug('my 50% debug');
TelegramLog::debug('my %s debug', 'placeholder'); Logger::debug('my %s debug', 'placeholder');
$this->assertFileExists($file); $this->assertFileExists($file);
$file_contents = file_get_contents($file); $file_contents = file_get_contents($file);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment