summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/Drivers/FileSystemDriver.php1
-rw-r--r--src/Format.php8
-rw-r--r--src/Loggr.php18
-rw-r--r--tests/FileSystemDriverTest.php27
-rw-r--r--tests/FormatTest.php124
-rw-r--r--tests/LoggrTest.php6
-rw-r--r--tests/OutputDriverTest.php24
7 files changed, 131 insertions, 77 deletions
diff --git a/src/Drivers/FileSystemDriver.php b/src/Drivers/FileSystemDriver.php
index 292ba63..7c8feec 100644
--- a/src/Drivers/FileSystemDriver.php
+++ b/src/Drivers/FileSystemDriver.php
@@ -21,7 +21,6 @@ class FileSystemDriver implements Driver
*
* @param string $serializedMessage The message to be logged, serialized as a string.
* @return void
- * @throws \RuntimeException If the log file or directory cannot be created, or if the log file is not writable.
*/
public function log(string $serializedMessage): void
{
diff --git a/src/Format.php b/src/Format.php
index 33e986b..4517e0a 100644
--- a/src/Format.php
+++ b/src/Format.php
@@ -43,7 +43,7 @@ enum Format
/** @var string $trace_line */
$trace_line = $message->trace['line'];
- $json = json_encode([
+ return json_encode([
'date' => $dateTime->format('Y-m-d H:i:s'),
'level' => strtoupper($message->level->value),
'message' => $message->content,
@@ -53,12 +53,6 @@ enum Format
'line' => $trace_line,
],
]);
-
- if ($json === false) {
- return '';
- }
-
- return $json;
}
/**
diff --git a/src/Loggr.php b/src/Loggr.php
index 742195a..78b3504 100644
--- a/src/Loggr.php
+++ b/src/Loggr.php
@@ -47,16 +47,12 @@ class Loggr implements LoggerInterface
return;
}
- try {
- $this->driver->log($this->format->serialize(new Message(
- level: $level,
- trace: $this->trace,
- content: $message,
- context: $context,
- )));
- } catch(\Throwable $e) {
- $this->error = $e->getMessage();
- }
+ $this->driver->log($this->format->serialize(new Message(
+ level: $level,
+ trace: $this->trace,
+ content: $message,
+ context: $context,
+ )));
}
/**
@@ -163,7 +159,7 @@ class Loggr implements LoggerInterface
$this->write(Level::Debug, $message, $context);
}
- public function log($level, \Stringable|string $message, array $context = []): void
+ public function log(mixed $level, \Stringable|string $message, array $context = []): void
{
$this->trace = debug_backtrace()[0];
$this->write(Level::from($level), $message, $context);
diff --git a/tests/FileSystemDriverTest.php b/tests/FileSystemDriverTest.php
index a50adcc..2c98091 100644
--- a/tests/FileSystemDriverTest.php
+++ b/tests/FileSystemDriverTest.php
@@ -10,9 +10,6 @@ use Mockery\Adapter\Phpunit\MockeryTestCase;
class FileSystemDriverTest extends MockeryTestCase
{
- /**
- * @throws \Exception
- */
public function testEmergency(): void
{
$data = '';
@@ -22,9 +19,6 @@ class FileSystemDriverTest extends MockeryTestCase
$this->assertStringContainsString("[{$full_date}] FileSystemDriverTest.EMERGENCY: test", $data);
}
- /**
- * @throws \Exception
- */
public function testAlert(): void
{
$data = '';
@@ -34,9 +28,6 @@ class FileSystemDriverTest extends MockeryTestCase
$this->assertStringContainsString("[{$full_date}] FileSystemDriverTest.ALERT: test", $data);
}
- /**
- * @throws \Exception
- */
public function testCritical(): void
{
$data = '';
@@ -46,9 +37,6 @@ class FileSystemDriverTest extends MockeryTestCase
$this->assertStringContainsString("[{$full_date}] FileSystemDriverTest.CRITICAL: test", $data);
}
- /**
- * @throws \Exception
- */
public function testError(): void
{
$data = '';
@@ -58,9 +46,6 @@ class FileSystemDriverTest extends MockeryTestCase
$this->assertStringContainsString("[{$full_date}] FileSystemDriverTest.ERROR: test", $data);
}
- /**
- * @throws \Exception
- */
public function testWarning(): void
{
$data = '';
@@ -70,9 +55,6 @@ class FileSystemDriverTest extends MockeryTestCase
$this->assertStringContainsString("[{$full_date}] FileSystemDriverTest.WARNING: test", $data);
}
- /**
- * @throws \Exception
- */
public function testNotice(): void
{
$data = '';
@@ -82,9 +64,6 @@ class FileSystemDriverTest extends MockeryTestCase
$this->assertStringContainsString("[{$full_date}] FileSystemDriverTest.NOTICE: test", $data);
}
- /**
- * @throws \Exception
- */
public function testInfo(): void
{
$data = '';
@@ -94,9 +73,6 @@ class FileSystemDriverTest extends MockeryTestCase
$this->assertStringContainsString("[{$full_date}] FileSystemDriverTest.INFO: test", $data);
}
- /**
- * @throws \Exception
- */
public function testDebug(): void
{
$data = '';
@@ -106,9 +82,6 @@ class FileSystemDriverTest extends MockeryTestCase
$this->assertStringContainsString("[{$full_date}] FileSystemDriverTest.DEBUG: test", $data);
}
- /**
- * @throws \Exception
- */
public function testMultipleEntries(): void
{
$data = '';
diff --git a/tests/FormatTest.php b/tests/FormatTest.php
index 16f765d..06f30a3 100644
--- a/tests/FormatTest.php
+++ b/tests/FormatTest.php
@@ -27,6 +27,22 @@ class FormatTest extends MockeryTestCase
$this->assertEquals($expectedJson, $serializedMessage);
}
+ public function testJsonStr(): void
+ {
+ $message = new Message(
+ level: Level::Info,
+ trace: debug_backtrace()[0],
+ context: "test",
+ );
+
+ $format = Format::JSON;
+ $serializedMessage = $format->serialize($message);
+ $full_date = (new DateTime())->format('Y-m-d H:i:s');
+ $expectedJson = '{"date":"' . $full_date . '","level":"INFO","message":"","context":"test","trace":{"file":"TestCase","line":1182}}';
+
+ $this->assertEquals($expectedJson, $serializedMessage);
+ }
+
public function testIntelliJ(): void
{
$message = new Message(
@@ -60,6 +76,40 @@ class FormatTest extends MockeryTestCase
$this->assertEquals($expected, $serializedMessage);
}
+ public function testIntelliJContextStr(): void
+ {
+ $message = new Message(
+ level: Level::Info,
+ trace: debug_backtrace()[0],
+ content: "hello world",
+ context: "hello world",
+ );
+
+ $format = Format::IntelliJ;
+ $serializedMessage = $format->serialize($message);
+ $full_date = (new DateTime())->format('Y-m-d H:i:s');
+ $expected = "{$full_date} [1182] INFO - TestCase - hello world - hello world";
+
+ $this->assertEquals($expected, $serializedMessage);
+ }
+
+ public function testIntelliJContextNumeric(): void
+ {
+ $message = new Message(
+ level: Level::Info,
+ trace: debug_backtrace()[0],
+ content: "hello world",
+ context: 123456789,
+ );
+
+ $format = Format::IntelliJ;
+ $serializedMessage = $format->serialize($message);
+ $full_date = (new DateTime())->format('Y-m-d H:i:s');
+ $expected = "{$full_date} [1182] INFO - TestCase - hello world - 123456789";
+
+ $this->assertEquals($expected, $serializedMessage);
+ }
+
public function testLaravel(): void
{
$message = new Message(
@@ -93,6 +143,40 @@ class FormatTest extends MockeryTestCase
$this->assertEquals($expected, $serializedMessage);
}
+ public function testLaravelContextStr(): void
+ {
+ $message = new Message(
+ level: Level::Info,
+ trace: debug_backtrace()[0],
+ content: "hello world",
+ context: "hello world",
+ );
+
+ $format = Format::Laravel;
+ $serializedMessage = $format->serialize($message);
+ $full_date = (new DateTime())->format('Y-m-d H:i:s');
+ $expected = "[{$full_date}] TestCase.INFO: hello world - hello world";
+
+ $this->assertEquals($expected, $serializedMessage);
+ }
+
+ public function testLaravelContextNumeric(): void
+ {
+ $message = new Message(
+ level: Level::Info,
+ trace: debug_backtrace()[0],
+ content: "hello world",
+ context: 123456789,
+ );
+
+ $format = Format::Laravel;
+ $serializedMessage = $format->serialize($message);
+ $full_date = (new DateTime())->format('Y-m-d H:i:s');
+ $expected = "[{$full_date}] TestCase.INFO: hello world - 123456789";
+
+ $this->assertEquals($expected, $serializedMessage);
+ }
+
public function testSymfony(): void
{
$mock_datetime = Mockery::mock('DateTime');
@@ -120,13 +204,51 @@ class FormatTest extends MockeryTestCase
$message = new Message(
level: Level::Info,
trace: debug_backtrace()[0],
+ content: "hello world",
context: "hello world",
);
$format = Format::Symfony;
$serializedMessage = $format->serialize($message, $mock_datetime);
$full_date = $mock_datetime->format('Y-m-d\TH:i:s.uP');
- $expected = "[$full_date] TestCase.INFO: hello world";
+ $expected = "[$full_date] TestCase.INFO: hello world - hello world";
+
+ $this->assertEquals($expected, $serializedMessage);
+ }
+
+ public function testSymfonyNoContentOrContext(): void
+ {
+ $mock_datetime = Mockery::mock('DateTime');
+ $mock_datetime->shouldReceive('format')->andReturn('2020-01-01T00:00:00.000000P');
+
+ $message = new Message(
+ level: Level::Info,
+ trace: debug_backtrace()[0],
+ context: [],
+ );
+
+ $format = Format::Symfony;
+ $serializedMessage = $format->serialize($message, $mock_datetime);
+ $full_date = $mock_datetime->format('Y-m-d\TH:i:s.uP');
+ $expected = "[$full_date] TestCase.INFO: ";
+
+ $this->assertEquals($expected, $serializedMessage);
+ }
+
+ public function testSymfonyNoContext(): void
+ {
+ $mock_datetime = Mockery::mock('DateTime');
+ $mock_datetime->shouldReceive('format')->andReturn('2020-01-01T00:00:00.000000P');
+
+ $message = new Message(
+ level: Level::Info,
+ trace: debug_backtrace()[0],
+ );
+
+ $format = Format::Symfony;
+ $serializedMessage = $format->serialize($message, $mock_datetime);
+ $full_date = $mock_datetime->format('Y-m-d\TH:i:s.uP');
+ $expected = "[$full_date] TestCase.INFO: ";
$this->assertEquals($expected, $serializedMessage);
}
diff --git a/tests/LoggrTest.php b/tests/LoggrTest.php
index eb2f5ab..a7c3628 100644
--- a/tests/LoggrTest.php
+++ b/tests/LoggrTest.php
@@ -8,9 +8,6 @@ use PHPUnit\Framework\TestCase;
class LoggrTest extends TestCase
{
- /**
- * @throws \Exception
- */
public function testNoDriver(): void
{
$loggr = new Loggr();
@@ -18,9 +15,6 @@ class LoggrTest extends TestCase
$this->assertEquals("Driver or format not set.", $loggr->error);
}
- /**
- * @throws \Exception
- */
public function testNoFormat(): void
{
$loggr = new Loggr(new OutputDriver());
diff --git a/tests/OutputDriverTest.php b/tests/OutputDriverTest.php
index e235cd9..3fa5ae2 100644
--- a/tests/OutputDriverTest.php
+++ b/tests/OutputDriverTest.php
@@ -9,9 +9,6 @@ use PHPUnit\Framework\TestCase;
class OutputDriverTest extends TestCase
{
- /**
- * @throws \Exception
- */
public function testEmergency(): void
{
$date = (new DateTime)->format('Y-m-d H:i:s');
@@ -20,9 +17,6 @@ class OutputDriverTest extends TestCase
$loggr->emergency('test');
}
- /**
- * @throws \Exception
- */
public function testAlert(): void
{
$date = (new DateTime)->format('Y-m-d H:i:s');
@@ -31,9 +25,6 @@ class OutputDriverTest extends TestCase
$loggr->alert('test');
}
- /**
- * @throws \Exception
- */
public function testCritical(): void
{
$date = (new DateTime)->format('Y-m-d H:i:s');
@@ -42,9 +33,6 @@ class OutputDriverTest extends TestCase
$loggr->critical('test');
}
- /**
- * @throws \Exception
- */
public function testError(): void
{
$date = (new DateTime)->format('Y-m-d H:i:s');
@@ -53,9 +41,6 @@ class OutputDriverTest extends TestCase
$loggr->error('test');
}
- /**
- * @throws \Exception
- */
public function testWarning(): void
{
$date = (new DateTime)->format('Y-m-d H:i:s');
@@ -64,9 +49,6 @@ class OutputDriverTest extends TestCase
$loggr->warning('test');
}
- /**
- * @throws \Exception
- */
public function testNotice(): void
{
$date = (new DateTime)->format('Y-m-d H:i:s');
@@ -75,9 +57,6 @@ class OutputDriverTest extends TestCase
$loggr->notice('test');
}
- /**
- * @throws \Exception
- */
public function testInfo(): void
{
$date = (new DateTime)->format('Y-m-d H:i:s');
@@ -86,9 +65,6 @@ class OutputDriverTest extends TestCase
$loggr->info('test');
}
- /**
- * @throws \Exception
- */
public function testDebug(): void
{
$date = (new DateTime)->format('Y-m-d H:i:s');