Skip to content

Commit

Permalink
Logger update
Browse files Browse the repository at this point in the history
  • Loading branch information
xtrime-ru committed Jan 12, 2020
1 parent d76d617 commit a8997ed
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 8 deletions.
1 change: 1 addition & 0 deletions src/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ public function __construct(array $sessions)
$config['connection_settings']['all']['proxy'] = '\Socket';
$config['connection_settings']['all']['proxy_extra'] = [];
}

foreach ($sessions as &$session) {
$session = $config;
}
Expand Down
13 changes: 7 additions & 6 deletions src/EventHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
namespace TelegramApiServer;

use danog\MadelineProto\CombinedEventHandler;
use danog\MadelineProto\Logger;

class EventHandler extends CombinedEventHandler
{
Expand All @@ -12,26 +11,28 @@ class EventHandler extends CombinedEventHandler

public static function addEventListener($clientId, callable $callback)
{
Logger::log("Add event listener. ClientId: {$clientId}");
Logger::getInstance()->notice("Add event listener. ClientId: {$clientId}");
static::$eventListeners[$clientId] = $callback;
}

public static function removeEventListener($clientId): void
{
Logger::log("Removing listener: {$clientId}");
Logger::getInstance()->notice("Removing listener: {$clientId}");
unset(static::$eventListeners[$clientId]);
if (!static::$eventListeners) {
$listenersCount = count(static::$eventListeners);
Logger::getInstance()->notice("Event listeners left: {$listenersCount}");
if ($listenersCount === 0) {
static::$eventListeners = [];
}
}

public function onAny($update, $sessionFile): void
{
$session = Client::getSessionName($sessionFile);
Logger::log("Got update from session: {$session}");
Logger::getInstance()->info("Received update from session: {$session}");

foreach (static::$eventListeners as $clientId => $callback) {
Logger::log("Pass update to callback. ClientId: {$clientId}");
Logger::getInstance()->notice("Pass update to callback. ClientId: {$clientId}");
$callback($update, $session);
}
}
Expand Down
19 changes: 18 additions & 1 deletion src/Logger.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@
*/
class Logger extends AbstractLogger
{
private static ?Logger $instanse = null;

private static array $levels = [
LogLevel::DEBUG => 0,
LogLevel::INFO => 1,
Expand All @@ -54,7 +56,22 @@ class Logger extends AbstractLogger
private int $minLevelIndex;
private array $formatter;

public function __construct(string $minLevel = LogLevel::WARNING, callable $formatter = null)
public static function getInstance(): Logger
{
if (!static::$instanse) {
$settings = Config::getInstance()->get('telegram');
MadelineProto\Logger::$default = null;
MadelineProto\Logger::constructorFromSettings($settings);

$conversionTable = array_flip(static::$madelineLevels);
$loggerLevel = $conversionTable[$settings['logger']['logger_level']];
static::$instanse = new static($loggerLevel);
}

return static::$instanse;
}

protected function __construct(string $minLevel = LogLevel::WARNING, callable $formatter = null)
{
if (null === $minLevel) {
if (isset($_ENV['SHELL_VERBOSITY']) || isset($_SERVER['SHELL_VERBOSITY'])) {
Expand Down
2 changes: 1 addition & 1 deletion src/Server.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public function __construct(Client $client, array $options)
$server = new Amp\Http\Server\Server(
$this->getServerAddresses(static::getConfig($options)),
static::getRouter($client),
new Logger(LogLevel::DEBUG),
Logger::getInstance(),
(new Amp\Http\Server\Options())
->withCompression()
->withBodySizeLimit(30*1024*1024)
Expand Down

0 comments on commit a8997ed

Please sign in to comment.