Skip to content

Commit

Permalink
Filter out token from (most) logs (#84)
Browse files Browse the repository at this point in the history
  • Loading branch information
Exanlv authored Mar 25, 2024
1 parent e903967 commit fd0b84d
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 4 deletions.
2 changes: 1 addition & 1 deletion src/Gateway/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public function __construct(
private LoggerInterface $logger = new NullLogger(),
int $timeout = 10,
) {
$this->websocket = new Websocket($timeout, $logger);
$this->websocket = new Websocket($timeout, $logger, [$this->token => '::token::']);
$this->events = new EventHandler($mapper);

$this->raw = new Eventer();
Expand Down
1 change: 1 addition & 0 deletions src/Parts/Role.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,5 @@ class Role
public bool $managed;
public bool $mentionable;
public ?RoleTags $tags;
public int $flags;
}
11 changes: 8 additions & 3 deletions src/Websocket.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ class Websocket extends EventEmitter

private Bucket $bucket;

public function __construct(private int $timeout, private LoggerInterface $logger)
public function __construct(private int $timeout, private LoggerInterface $logger, private array $sendLoggerBlacklist = [])
{
$this->loop = Loop::get();
$this->socketConnector = new SocketConnector(['timeout' => $timeout]);
Expand Down Expand Up @@ -106,11 +106,11 @@ public function send(string $message, bool $useBucket = true): void

$action = function () use ($message) {
$this->connection->send($message);
$this->logger->debug('Client: New message', [$message]);
$this->logger->debug('Client: New message', [$this->filterSentMessage($message)]);
};

if ($useBucket) {
$this->logger->debug('Client: Queued message', [$message]);
$this->logger->debug('Client: Queued message', [$this->filterSentMessage($message)]);

$this->bucket->run($action);

Expand All @@ -120,6 +120,11 @@ public function send(string $message, bool $useBucket = true): void
$action();
}

private function filterSentMessage(string $message)
{
return str_replace(array_keys($this->sendLoggerBlacklist), array_values($this->sendLoggerBlacklist), $message);
}

/**
* @throws ConnectionNotInitializedException
*/
Expand Down

0 comments on commit fd0b84d

Please sign in to comment.