Update logger dependencies and add Logger class

This commit is contained in:
netkas 2024-11-01 14:01:25 -04:00
parent f8e5b0f632
commit d702ddb120
3 changed files with 33 additions and 4 deletions

3
.idea/php.xml generated
View file

@ -12,9 +12,8 @@
<component name="PhpAnalysisConfiguration" call_tree_analysis_depth="3" />
<component name="PhpIncludePathManager">
<include_path>
<path value="/var/ncc/packages/net.nosial.loglib=1.1.0" />
<path value="/var/ncc/packages/net.nosial.loglib=2.0.2" />
<path value="/usr/share/php" />
<path value="/var/ncc/packages/net.nosial.tempfile=1.1.0" />
</include_path>
</component>
<component name="PhpProjectSharedConfiguration" php_language_level="8.3">

View file

@ -10,9 +10,9 @@
namespace TgBotLib;
use InvalidArgumentException;
use LogLib\Log;
use ReflectionClass;
use TgBotLib\Abstracts\Method;
use TgBotLib\Classes\Logger;
use TgBotLib\Enums\Methods;
use TgBotLib\Enums\Types\ChatActionType;
use TgBotLib\Enums\Types\ParseMode;
@ -50,6 +50,7 @@
/**
* @method Update[] getUpdates(?int $offset=null, ?int $limit=null, ?int $timeout=null, ?string $allowed_updates=null) Use this method to receive incoming updates using long polling (wiki). An Array of Update objects is returned.
* @method bool setWebhook(string $url, ?string $certificate=null, ?string $ip_address=null, ?int $max_connections=null, ?string $allowed_updates=null, ?bool $drop_pending_updates=null, ?string $secret_token=null) Use this method to specify a url and receive incoming updates via an outgoing webhook. Whenever there is an update for the bot, we will send an HTTPS POST request to the specified url, containing a JSON-serialized Update. In case of an unsuccessful request, we will give up after a reasonable amount of attempts. Returns True on success.
* @method User getMe() A simple method for testing your bot's authentication token. Requires no parameters. Returns basic information about the bot in form of a User object.
* @method bool logOut() Use this method to log out from the cloud Bot API server before launching the bot locally. You must log out the bot before running it locally, otherwise there is no guarantee that the bot will receive updates. After a successful call, you can immediately log in on a local server, but will not be able to log in back to the cloud Bot API server for 10 minutes. Returns True on success. Requires no parameters.
* @method bool close() Use this method to close the bot instance before moving it from one local server to another. You need to delete the webhook before calling this method to ensure that the bot isn't launched again after server restart. The method will return error 429 in the first 10 minutes after the bot is launched. Returns True on success. Requires no parameters.
@ -242,6 +243,7 @@
try
{
Logger::getLogger()->debug(sprintf('Executing method %s with parameters %s', $method->value, json_encode($parameters)));
return $method->execute($this, $parameters);
}
catch(TelegramException $e)
@ -257,7 +259,7 @@
$waitTime = 5;
}
Log::warning('net.nosial.tgbotlib', "Too Many Requests error, retrying in {$waitTime} seconds");
Logger::getLogger()->verbose(sprintf('Too many requests, waiting %d seconds before retrying', $waitTime));
sleep($waitTime);
return $method->execute($this, $parameters);

View file

@ -0,0 +1,28 @@
<?php
namespace TgBotLib\Classes;
use LogLib\Log;
class Logger
{
private static ?\LogLib\Logger $logger = null;
/**
* Retrieves the instance of the logger. If the logger instance does not
* exist, it initializes a new logger for the application and registers
* the exception handler.
*
* @return \LogLib\Logger The logger instance.
*/
public static function getLogger(): \LogLib\Logger
{
if(self::$logger === null)
{
self::$logger = new \LogLib\Logger('net.nosial.tgbotlib');
Log::registerExceptionHandler();
}
return self::$logger;
}
}