Rename Logger class

parent 6227c407
......@@ -14,7 +14,7 @@ use Longman\TelegramBot\Commands\AdminCommand;
use Longman\TelegramBot\DB;
use Longman\TelegramBot\Exception\TelegramException;
use Longman\TelegramBot\Http\Client;
use Longman\TelegramBot\TelegramLog;
use Longman\TelegramBot\Logger;
use PDOException;
/**
......@@ -386,7 +386,7 @@ class CleanupCommand extends AdminCommand
if ($dbq = $pdo->query($query)) {
$rows += $dbq->rowCount();
} else {
TelegramLog::error('Error while executing query: ' . $query);
Logger::error('Error while executing query: ' . $query);
}
}
......
......@@ -149,7 +149,7 @@ abstract class Command
*/
public function preExecute()
{
if ($this->need_mysql && !($this->telegram->isDbEnabled() && DB::isDbConnected())) {
if ($this->need_mysql && !(DB::isEnabled() && DB::isDbConnected())) {
return $this->executeNoDb();
}
......
......@@ -71,7 +71,7 @@ class Kernel
}
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(
......@@ -91,7 +91,7 @@ class Kernel
$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
Client::getUpdates(
[
......
......@@ -31,28 +31,35 @@ class DB
*
* @var array
*/
static protected $mysql_credentials = [];
protected static $mysql_credentials = [];
/**
* PDO object
*
* @var PDO
*/
static protected $pdo;
protected static $pdo;
/**
* Table prefix
*
* @var string
*/
static protected $table_prefix;
protected static $table_prefix;
/**
* Telegram class object
*
* @var Telegram
*/
static protected $telegram;
protected static $telegram;
/**
* Enabled
*
* @var bool
*/
protected static $enabled = false;
/**
* Initialize
......@@ -1189,4 +1196,25 @@ class DB
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;
use Exception;
use Longman\TelegramBot\Entities\InlineQuery\InlineEntity;
use Longman\TelegramBot\TelegramLog;
use Longman\TelegramBot\Logger;
/**
* Class Entity
......
......@@ -14,7 +14,7 @@ use GuzzleHttp\Client;
use GuzzleHttp\Exception\RequestException;
use Longman\TelegramBot\Entities\Update;
use Longman\TelegramBot\Exception\TelegramException;
use Longman\TelegramBot\TelegramLog;
use Longman\TelegramBot\Logger;
/**
* Class Botan
......@@ -167,7 +167,7 @@ class Botan
}
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;
}
......@@ -199,7 +199,7 @@ class Botan
$responseData = json_decode($result, true);
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;
}
......@@ -246,7 +246,7 @@ class Botan
}
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;
}
......
......@@ -16,7 +16,7 @@ use Longman\TelegramBot\DB;
use Longman\TelegramBot\Entities\File;
use Longman\TelegramBot\Exception\TelegramException;
use Longman\TelegramBot\Telegram;
use Longman\TelegramBot\TelegramLog;
use Longman\TelegramBot\Logger;
/**
* Class Client
......@@ -380,7 +380,7 @@ class Client
$result = null;
$request_params = self::setUpRequestParams($data);
$request_params['debug'] = TelegramLog::getDebugLogTempStream();
$request_params['debug'] = Logger::getDebugLogTempStream();
try {
$response = self::$client->post(
......@@ -391,13 +391,13 @@ class Client
// Logging getUpdates Update
if ($action === 'getUpdates') {
TelegramLog::update($result);
Logger::update($result);
}
} catch (RequestException $e) {
$result = ($e->getResponse()) ? (string) $e->getResponse()->getBody() : '';
} finally {
// 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;
......@@ -427,7 +427,7 @@ class Client
throw new TelegramException('Directory ' . $file_dir . ' can\'t be created');
}
$debug_handle = TelegramLog::getDebugLogTempStream();
$debug_handle = Logger::getDebugLogTempStream();
try {
self::$client->get(
......@@ -440,7 +440,7 @@ class Client
return ($e->getResponse()) ? (string) $e->getResponse()->getBody() : '';
} finally {
//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;
use Longman\TelegramBot\Exception\TelegramLogException;
use Monolog\Formatter\LineFormatter;
use Monolog\Handler\StreamHandler;
use Monolog\Logger;
use Monolog\Logger as MonologLogger;
class TelegramLog
class Logger
{
/**
* Monolog instance
......@@ -66,7 +66,7 @@ class TelegramLog
*
* @return \Monolog\Logger
*/
public static function initialize(Logger $external_monolog = null)
public static function initialize(MonologLogger $external_monolog = null)
{
if (self::$monolog === null) {
if ($external_monolog !== null) {
......@@ -81,7 +81,7 @@ class TelegramLog
}
}
} else {
self::$monolog = new Logger('bot_log');
self::$monolog = new MonologLogger('bot_log');
}
}
......@@ -107,7 +107,7 @@ class TelegramLog
self::$error_log_path = $path;
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))
);
}
......@@ -131,7 +131,7 @@ class TelegramLog
self::$debug_log_path = $path;
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))
);
}
......@@ -186,10 +186,10 @@ class TelegramLog
self::$update_log_path = $path;
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(
(new StreamHandler(self::$update_log_path, Logger::INFO))
(new StreamHandler(self::$update_log_path, MonologLogger::INFO))
->setFormatter(new LineFormatter('%message%' . PHP_EOL))
);
}
......
......@@ -73,27 +73,6 @@ class Telegram
*/
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
*
......@@ -228,8 +207,8 @@ class Telegram
public function enableMySql(array $credential, $table_prefix = null, $encoding = 'utf8mb4')
{
$this->pdo = DB::initialize($credential, $this, $table_prefix, $encoding);
DB::setEnabled(true);
ConversationDB::initializeConversation();
$this->mysql_enabled = true;
return $this;
}
......@@ -246,8 +225,8 @@ class Telegram
public function enableExternalMySql($external_pdo_connection, $table_prefix = null)
{
$this->pdo = DB::externalInitialize($external_pdo_connection, $this, $table_prefix);
DB::setEnabled(true);
ConversationDB::initializeConversation();
$this->mysql_enabled = true;
return $this;
}
......@@ -461,7 +440,7 @@ class Telegram
// Make sure we don't try to process update that was already processed
$last_id = DB::selectTelegramUpdate(1, $update->getUpdateId());
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]);
}
......@@ -596,20 +575,6 @@ class Telegram
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
*
......@@ -660,7 +625,7 @@ class Telegram
*/
public function setUploadPath($path)
{
$this->upload_path = $path;
$this->getConfig()->setUploadPath($path);
return $this;
}
......@@ -672,7 +637,7 @@ class Telegram
*/
public function getUploadPath()
{
return $this->upload_path;
return $this->getConfig()->getUploadPath();
}
/**
......@@ -684,7 +649,7 @@ class Telegram
*/
public function setDownloadPath($path)
{
$this->download_path = $path;
$this->getConfig()->setDownloadPath($path);
return $this;
}
......@@ -696,7 +661,7 @@ class Telegram
*/
public function getDownloadPath()
{
return $this->download_path;
return $this->getConfig()->getDownloadPath();
}
/**
......
......@@ -10,7 +10,7 @@
namespace Longman\TelegramBot\Tests\Unit;
use Longman\TelegramBot\TelegramLog;
use Longman\TelegramBot\Logger;
use Monolog\Handler\StreamHandler;
use Monolog\Logger;
......@@ -36,7 +36,7 @@ class TelegramLogTest extends TestCase
protected function setUp()
{
// 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()
......@@ -52,7 +52,7 @@ class TelegramLogTest extends TestCase
*/
public function testNewInstanceWithoutErrorPath()
{
TelegramLog::initErrorLog('');
Logger::initErrorLog('');
}
/**
......@@ -60,7 +60,7 @@ class TelegramLogTest extends TestCase
*/
public function testNewInstanceWithoutDebugPath()
{
TelegramLog::initDebugLog('');
Logger::initDebugLog('');
}
/**
......@@ -68,17 +68,17 @@ class TelegramLogTest extends TestCase
*/
public function testNewInstanceWithoutUpdatePath()
{
TelegramLog::initUpdateLog('');
Logger::initUpdateLog('');
}
public function testErrorStream()
{
$file = self::$logfiles['error'];
$this->assertFileNotExists($file);
TelegramLog::initErrorLog($file);
TelegramLog::error('my error');
TelegramLog::error('my 50% error');
TelegramLog::error('my %s error', 'placeholder');
Logger::initErrorLog($file);
Logger::error('my error');
Logger::error('my 50% error');
Logger::error('my %s error', 'placeholder');
$this->assertFileExists($file);
$error_log = file_get_contents($file);
$this->assertContains('bot_log.ERROR: my error', $error_log);
......@@ -90,10 +90,10 @@ class TelegramLogTest extends TestCase
{
$file = self::$logfiles['debug'];
$this->assertFileNotExists($file);
TelegramLog::initDebugLog($file);
TelegramLog::debug('my debug');
TelegramLog::debug('my 50% debug');
TelegramLog::debug('my %s debug', 'placeholder');
Logger::initDebugLog($file);
Logger::debug('my debug');
Logger::debug('my 50% debug');
Logger::debug('my %s debug', 'placeholder');
$this->assertFileExists($file);
$debug_log = file_get_contents($file);
$this->assertContains('bot_log.DEBUG: my debug', $debug_log);
......@@ -105,10 +105,10 @@ class TelegramLogTest extends TestCase
{
$file = self::$logfiles['update'];
$this->assertFileNotExists($file);
TelegramLog::initUpdateLog($file);
TelegramLog::update('my update');
TelegramLog::update('my 50% update');
TelegramLog::update('my %s update', 'placeholder');
Logger::initUpdateLog($file);
Logger::update('my update');
Logger::update('my 50% update');
Logger::update('my %s update', 'placeholder');
$this->assertFileExists($file);
$debug_log = file_get_contents($file);
$this->assertContains('my update', $debug_log);
......@@ -125,13 +125,13 @@ class TelegramLogTest extends TestCase
$external_monolog->pushHandler(new StreamHandler($file, Logger::ERROR));
$external_monolog->pushHandler(new StreamHandler($file, Logger::DEBUG));
TelegramLog::initialize($external_monolog);
TelegramLog::error('my error');
TelegramLog::error('my 50% error');
TelegramLog::error('my %s error', 'placeholder');
TelegramLog::debug('my debug');
TelegramLog::debug('my 50% debug');
TelegramLog::debug('my %s debug', 'placeholder');
Logger::initialize($external_monolog);
Logger::error('my error');
Logger::error('my 50% error');
Logger::error('my %s error', 'placeholder');
Logger::debug('my debug');
Logger::debug('my 50% debug');
Logger::debug('my %s debug', 'placeholder');
$this->assertFileExists($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