Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions COMMANDS.md
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,11 @@ $teamspeak->logs()->list(lines: 50, reverse: true, instance: true, beginPos: 100
use TeamSpeak\WebQuery\Enums\LogLevel;

$teamspeak->logs()->add(LogLevel::Info, 'custom log message');
// also available shortcuts:
$teamspeak->logs()->error('something failed');
$teamspeak->logs()->warning('something suspicious');
$teamspeak->logs()->debug('debug info');
$teamspeak->logs()->info('server started');
```

## gm
Expand Down
20 changes: 20 additions & 0 deletions src/Contracts/Resources/LogsContract.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,24 @@ public function list(?int $lines = null, bool $reverse = false, bool $instance =
* Writes a custom entry into the server log.
*/
public function add(LogLevel $level, string $message): void;

/**
* Writes an error entry into the server log.
*/
public function error(string $message): void;

/**
* Writes a warning entry into the server log.
*/
public function warning(string $message): void;

/**
* Writes a debug entry into the server log.
*/
public function debug(string $message): void;

/**
* Writes an info entry into the server log.
*/
public function info(string $message): void;
}
32 changes: 32 additions & 0 deletions src/Resources/Logs.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,36 @@ public function add(LogLevel $level, string $message): void

$this->transporter->request($payload);
}

/**
* Writes an error entry into the server log.
*/
public function error(string $message): void
{
$this->add(LogLevel::Error, $message);
}

/**
* Writes a warning entry into the server log.
*/
public function warning(string $message): void
{
$this->add(LogLevel::Warning, $message);
}

/**
* Writes a debug entry into the server log.
*/
public function debug(string $message): void
{
$this->add(LogLevel::Debug, $message);
}

/**
* Writes an info entry into the server log.
*/
public function info(string $message): void
{
$this->add(LogLevel::Info, $message);
}
}
84 changes: 84 additions & 0 deletions tests/Unit/Resources/LogsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -106,3 +106,87 @@

$resource->add(LogLevel::Info, 'test message');
})->doesNotPerformAssertions();

test('error shortcut', function () {
$resource = new Logs($this->http);

$response = new Response(200, ['Content-Type' => 'application/json'], json_encode([
'status' => ['code' => 0, 'message' => 'ok'],
]));

$this->client
->shouldReceive('sendRequest')
->once()
->withArgs(function (Psr7Request $request) {
expect($request->getBody()->getContents())->toBeJson()
->json()
->toBe(['loglevel' => '1', 'logmsg' => 'something failed']);

return true;
})->andReturn($response);

$resource->error('something failed');
})->doesNotPerformAssertions();

test('warning shortcut', function () {
$resource = new Logs($this->http);

$response = new Response(200, ['Content-Type' => 'application/json'], json_encode([
'status' => ['code' => 0, 'message' => 'ok'],
]));

$this->client
->shouldReceive('sendRequest')
->once()
->withArgs(function (Psr7Request $request) {
expect($request->getBody()->getContents())->toBeJson()
->json()
->toBe(['loglevel' => '2', 'logmsg' => 'something suspicious']);

return true;
})->andReturn($response);

$resource->warning('something suspicious');
})->doesNotPerformAssertions();

test('debug shortcut', function () {
$resource = new Logs($this->http);

$response = new Response(200, ['Content-Type' => 'application/json'], json_encode([
'status' => ['code' => 0, 'message' => 'ok'],
]));

$this->client
->shouldReceive('sendRequest')
->once()
->withArgs(function (Psr7Request $request) {
expect($request->getBody()->getContents())->toBeJson()
->json()
->toBe(['loglevel' => '3', 'logmsg' => 'debug info']);

return true;
})->andReturn($response);

$resource->debug('debug info');
})->doesNotPerformAssertions();

test('info shortcut', function () {
$resource = new Logs($this->http);

$response = new Response(200, ['Content-Type' => 'application/json'], json_encode([
'status' => ['code' => 0, 'message' => 'ok'],
]));

$this->client
->shouldReceive('sendRequest')
->once()
->withArgs(function (Psr7Request $request) {
expect($request->getBody()->getContents())->toBeJson()
->json()
->toBe(['loglevel' => '4', 'logmsg' => 'server started']);

return true;
})->andReturn($response);

$resource->info('server started');
})->doesNotPerformAssertions();
Loading