diff --git a/COMMANDS.md b/COMMANDS.md index c713334..e56b668 100644 --- a/COMMANDS.md +++ b/COMMANDS.md @@ -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 diff --git a/src/Contracts/Resources/LogsContract.php b/src/Contracts/Resources/LogsContract.php index 656a0f1..0325206 100644 --- a/src/Contracts/Resources/LogsContract.php +++ b/src/Contracts/Resources/LogsContract.php @@ -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; } diff --git a/src/Resources/Logs.php b/src/Resources/Logs.php index 70115e8..4c0228d 100644 --- a/src/Resources/Logs.php +++ b/src/Resources/Logs.php @@ -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); + } } diff --git a/tests/Unit/Resources/LogsTest.php b/tests/Unit/Resources/LogsTest.php index 8cfd489..26a1bcb 100644 --- a/tests/Unit/Resources/LogsTest.php +++ b/tests/Unit/Resources/LogsTest.php @@ -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();