Skip to content

Commit

Permalink
[#10] Added Message::jsonSerialize() and updated tests to reflect enc…
Browse files Browse the repository at this point in the history
…oding (#11)
  • Loading branch information
AndyM84 authored Apr 25, 2020
1 parent 7c17fb8 commit ab98435
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 14 deletions.
22 changes: 16 additions & 6 deletions Log/Message.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
* @package Stoic\Log
* @version 1.0.0
*/
class Message {
class Message implements \JsonSerializable {
/**
* String value of message level.
*
Expand Down Expand Up @@ -102,11 +102,21 @@ public function __toArray() {
* @return string
*/
public function __toJson() {
return sprintf("{ \"level\": \"%s\", \"message\": \"%s\", \"timestamp\": \"%s\" }",
strtoupper($this->level),
$this->message,
$this->timestamp->format('Y-m-d G:i:s.u')
);
return json_encode($this);
}

/**
* Returns an array of the message data ready to run through
* json_encode().
*
* @return string[]
*/
public function jsonSerialize() {
return [
'level' => strtoupper($this->level),
'message' => $this->message,
'timestamp' => $this->timestamp->format('Y-m-d G:i:s.u')
];
}

/**
Expand Down
16 changes: 8 additions & 8 deletions Tests/Log/LoggerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public function test_LogLevels() {
self::assertEquals('Testing', $app->messages[0]->message);

$ts = $app->messages[0]->getTimestamp()->format('Y-m-d G:i:s.u');
self::assertEquals("{ \"level\": \"ALERT\", \"message\": \"Testing\", \"timestamp\": \"{$ts}\" }", $app->messages[0]->__toJson());
self::assertEquals("{\"level\":\"ALERT\",\"message\":\"Testing\",\"timestamp\":\"{$ts}\"}", $app->messages[0]->__toJson());
self::assertEquals(sprintf("%s %' -9s %s", $ts, 'ALERT', 'Testing'), $app->messages[0]->__toString());

$app = new MemoryAppender();
Expand All @@ -73,7 +73,7 @@ public function test_LogLevels() {
self::assertEquals('Testing', $app->messages[0]->message);

$ts = $app->messages[0]->getTimestamp()->format('Y-m-d G:i:s.u');
self::assertEquals("{ \"level\": \"CRITICAL\", \"message\": \"Testing\", \"timestamp\": \"{$ts}\" }", $app->messages[0]->__toJson());
self::assertEquals("{\"level\":\"CRITICAL\",\"message\":\"Testing\",\"timestamp\":\"{$ts}\"}", $app->messages[0]->__toJson());
self::assertEquals(sprintf("%s %' -9s %s", $ts, 'CRITICAL', 'Testing'), $app->messages[0]->__toString());

$app = new MemoryAppender();
Expand All @@ -87,7 +87,7 @@ public function test_LogLevels() {
self::assertEquals('Testing', $app->messages[0]->message);

$ts = $app->messages[0]->getTimestamp()->format('Y-m-d G:i:s.u');
self::assertEquals("{ \"level\": \"DEBUG\", \"message\": \"Testing\", \"timestamp\": \"{$ts}\" }", $app->messages[0]->__toJson());
self::assertEquals("{\"level\":\"DEBUG\",\"message\":\"Testing\",\"timestamp\":\"{$ts}\"}", $app->messages[0]->__toJson());
self::assertEquals(sprintf("%s %' -9s %s", $ts, 'DEBUG', 'Testing'), $app->messages[0]->__toString());

$app = new MemoryAppender();
Expand All @@ -102,7 +102,7 @@ public function test_LogLevels() {

$ts = $app->messages[0]->getTimestamp()->format('Y-m-d G:i:s.u');
self::assertEquals(3, count($app->messages[0]->__toArray()));
self::assertEquals("{ \"level\": \"EMERGENCY\", \"message\": \"Testing\", \"timestamp\": \"{$ts}\" }", $app->messages[0]->__toJson());
self::assertEquals("{\"level\":\"EMERGENCY\",\"message\":\"Testing\",\"timestamp\":\"{$ts}\"}", $app->messages[0]->__toJson());
self::assertEquals(sprintf("%s %' -9s %s", $ts, 'EMERGENCY', 'Testing'), $app->messages[0]->__toString());

$app = new MemoryAppender();
Expand All @@ -116,7 +116,7 @@ public function test_LogLevels() {
self::assertEquals('Testing', $app->messages[0]->message);

$ts = $app->messages[0]->getTimestamp()->format('Y-m-d G:i:s.u');
self::assertEquals("{ \"level\": \"ERROR\", \"message\": \"Testing\", \"timestamp\": \"{$ts}\" }", $app->messages[0]->__toJson());
self::assertEquals("{\"level\":\"ERROR\",\"message\":\"Testing\",\"timestamp\":\"{$ts}\"}", $app->messages[0]->__toJson());
self::assertEquals(sprintf("%s %' -9s %s", $ts, 'ERROR', 'Testing'), $app->messages[0]->__toString());

$app = new MemoryAppender();
Expand All @@ -130,7 +130,7 @@ public function test_LogLevels() {
self::assertEquals('Testing', $app->messages[0]->message);

$ts = $app->messages[0]->getTimestamp()->format('Y-m-d G:i:s.u');
self::assertEquals("{ \"level\": \"INFO\", \"message\": \"Testing\", \"timestamp\": \"{$ts}\" }", $app->messages[0]->__toJson());
self::assertEquals("{\"level\":\"INFO\",\"message\":\"Testing\",\"timestamp\":\"{$ts}\"}", $app->messages[0]->__toJson());
self::assertEquals(sprintf("%s %' -9s %s", $ts, 'INFO', 'Testing'), $app->messages[0]->__toString());

$app = new MemoryAppender();
Expand All @@ -144,7 +144,7 @@ public function test_LogLevels() {
self::assertEquals('Testing', $app->messages[0]->message);

$ts = $app->messages[0]->getTimestamp()->format('Y-m-d G:i:s.u');
self::assertEquals("{ \"level\": \"NOTICE\", \"message\": \"Testing\", \"timestamp\": \"{$ts}\" }", $app->messages[0]->__toJson());
self::assertEquals("{\"level\":\"NOTICE\",\"message\":\"Testing\",\"timestamp\":\"{$ts}\"}", $app->messages[0]->__toJson());
self::assertEquals(sprintf("%s %' -9s %s", $ts, 'NOTICE', 'Testing'), $app->messages[0]->__toString());

$app = new MemoryAppender();
Expand All @@ -158,7 +158,7 @@ public function test_LogLevels() {
self::assertEquals('Testing', $app->messages[0]->message);

$ts = $app->messages[0]->getTimestamp()->format('Y-m-d G:i:s.u');
self::assertEquals("{ \"level\": \"WARNING\", \"message\": \"Testing\", \"timestamp\": \"{$ts}\" }", $app->messages[0]->__toJson());
self::assertEquals("{\"level\":\"WARNING\",\"message\":\"Testing\",\"timestamp\":\"{$ts}\"}", $app->messages[0]->__toJson());
self::assertEquals(sprintf("%s %' -9s %s", $ts, 'WARNING', 'Testing'), $app->messages[0]->__toString());

return;
Expand Down

0 comments on commit ab98435

Please sign in to comment.