Skip to content

Commit

Permalink
Allow get the last written log info
Browse files Browse the repository at this point in the history
  • Loading branch information
natanfelles committed May 24, 2021
1 parent 4fbc7d6 commit b7336c9
Show file tree
Hide file tree
Showing 2 changed files with 108 additions and 24 deletions.
53 changes: 48 additions & 5 deletions src/Logger.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,15 @@ class Logger
* Active log level.
*/
protected int $level = Logger::NOTICE;
protected array $lastLog = [
'filepath' => null,
'date' => null,
'time' => null,
'level' => null,
'id' => null,
'message' => null,
'written' => null,
];

/**
* Logger constructor.
Expand Down Expand Up @@ -84,15 +93,46 @@ public function __construct(string $directory, int $level = self::DEBUG)
public function log(int $level, string $message, array $context = []) : bool
{
$this->validateLevel($level);
$this->resetLastLog();
if ($level < $this->level) {
return true;
}
$time = \date('H:i:s');
$this->lastLog['time'] = $time;
$level = $this->getLevelName($level);
$this->lastLog['level'] = $level;
$id = \bin2hex(\random_bytes(10));
$this->lastLog['id'] = $id;
$message = $this->replaceContext($message, $context);
$message = \date('H:i:s') . ' ' . $this->getLevelName($level) . ' '
. $this->sanitizeMessage($message) . ' ' . \PHP_EOL;
$message = $this->sanitizeMessage($message);
$this->lastLog['message'] = $message;
$message = $time . ' ' . $level . ' ' . $id . ' ' . $message . ' ' . \PHP_EOL;
return $this->write($message);
}

/**
* Get the last written log info.
*
* @return array An array with 7 keys: filepath, date, time, level, id, message and written
*/
public function getLastLog() : array
{
return $this->lastLog;
}

protected function resetLastLog()
{
$this->lastLog = [
'filepath' => null,
'date' => null,
'time' => null,
'level' => null,
'id' => null,
'message' => null,
'written' => null,
];
}

/**
* Detailed debug information.
*
Expand Down Expand Up @@ -270,15 +310,18 @@ protected function sanitizeMessage(string $message) : string

protected function write(string $message) : bool
{
$file = $this->directory . \date('Y-m-d') . '.log';
$date = \date('Y-m-d');
$this->lastLog['date'] = $date;
$file = $this->directory . $date . '.log';
$this->lastLog['filepath'] = $file;
$handle = \fopen($file, 'ab');
if ($handle === false) {
return false;
return $this->lastLog['written'] = false;
}
\flock($handle, \LOCK_EX);
$write = \fwrite($handle, $message);
\flock($handle, \LOCK_UN);
\fclose($handle);
return $write !== false;
return $this->lastLog['written'] = $write !== false;
}
}
79 changes: 60 additions & 19 deletions tests/LoggerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ protected function tearDown() : void

protected function getExpected(string $level)
{
return \date('H:i:s') . ' ' . $level . ' foo ' . \PHP_EOL;
return '#' . \date('H:i:s') . ' ' . $level . ' [a-z0-9]+' . ' foo ' . \PHP_EOL . '#';
}

protected function getContents() : string
Expand Down Expand Up @@ -58,7 +58,7 @@ public function testInvalidName()
public function testLog()
{
$this->assertTrue($this->logger->log($this->logger::DEBUG, 'foo'));
$this->assertEquals(
$this->assertMatchesRegularExpression(
$this->getExpected('DEBUG'),
$this->getContents()
);
Expand All @@ -67,7 +67,7 @@ public function testLog()
public function testEmergency()
{
$this->assertTrue($this->logger->emergency('foo'));
$this->assertEquals(
$this->assertMatchesRegularExpression(
$this->getExpected('EMERGENCY'),
$this->getContents()
);
Expand All @@ -76,7 +76,7 @@ public function testEmergency()
public function testAlert()
{
$this->assertTrue($this->logger->alert('foo'));
$this->assertEquals(
$this->assertMatchesRegularExpression(
$this->getExpected('ALERT'),
$this->getContents()
);
Expand All @@ -85,7 +85,7 @@ public function testAlert()
public function testCritical()
{
$this->assertTrue($this->logger->critical('foo'));
$this->assertEquals(
$this->assertMatchesRegularExpression(
$this->getExpected('CRITICAL'),
$this->getContents()
);
Expand All @@ -94,7 +94,7 @@ public function testCritical()
public function testError()
{
$this->assertTrue($this->logger->error('foo'));
$this->assertEquals(
$this->assertMatchesRegularExpression(
$this->getExpected('ERROR'),
$this->getContents()
);
Expand All @@ -103,7 +103,7 @@ public function testError()
public function testWarning()
{
$this->assertTrue($this->logger->warning('foo'));
$this->assertEquals(
$this->assertMatchesRegularExpression(
$this->getExpected('WARNING'),
$this->getContents()
);
Expand All @@ -112,7 +112,7 @@ public function testWarning()
public function testNotice()
{
$this->assertTrue($this->logger->notice('foo'));
$this->assertEquals(
$this->assertMatchesRegularExpression(
$this->getExpected('NOTICE'),
$this->getContents()
);
Expand All @@ -121,7 +121,7 @@ public function testNotice()
public function testInfo()
{
$this->assertTrue($this->logger->info('foo'));
$this->assertEquals(
$this->assertMatchesRegularExpression(
$this->getExpected('INFO'),
$this->getContents()
);
Expand All @@ -130,7 +130,7 @@ public function testInfo()
public function testDebug()
{
$this->assertTrue($this->logger->debug('foo'));
$this->assertEquals(
$this->assertMatchesRegularExpression(
$this->getExpected('DEBUG'),
$this->getContents()
);
Expand All @@ -141,10 +141,15 @@ public function testMultiLogs()
$this->assertTrue($this->logger->debug('foo'));
$this->assertTrue($this->logger->info('foo'));
$this->assertTrue($this->logger->emergency('foo'));
$this->assertEquals(
$this->getExpected('DEBUG')
. $this->getExpected('INFO')
. $this->getExpected('EMERGENCY'),
$time = \date('H:i:s');
$this->assertMatchesRegularExpression(
<<<EOL
#{$time} DEBUG [a-z0-9]+ foo
{$time} INFO [a-z0-9]+ foo
{$time} EMERGENCY [a-z0-9]+ foo
#
EOL
,
$this->getContents()
);
}
Expand All @@ -155,19 +160,55 @@ public function testMultiLogsOnDiabledLevel()
$this->assertTrue($this->logger->debug('foo'));
$this->assertTrue($this->logger->info('foo'));
$this->assertTrue($this->logger->emergency('foo'));
$this->assertEquals(
$this->getExpected('INFO')
. $this->getExpected('EMERGENCY'),
$time = \date('H:i:s');
$this->assertMatchesRegularExpression(
<<<EOL
#{$time} INFO [a-z0-9]+ foo
{$time} EMERGENCY [a-z0-9]+ foo
#
EOL
,
$this->getContents()
);
}

public function testContext()
{
$this->assertTrue($this->logger->debug('foo {a}bar', ['a' => 'x ']));
$this->assertEquals(
\date('H:i:s') . ' DEBUG foo x bar ' . \PHP_EOL,
$this->assertMatchesRegularExpression(
'#' . \date('H:i:s') . ' DEBUG [a-z0-9]+ foo x bar ' . \PHP_EOL . '#',
$this->getContents()
);
}

public function testLastLog()
{
$this->assertTrue($this->logger->critical('foo'));
$log = [
'filepath' => $this->directory . \date('Y-m-d') . '.log',
'date' => \date('Y-m-d'),
'time' => \date('H:i:s'),
'level' => 'CRITICAL',
'message' => 'foo',
'written' => true,
];
$last_log = $this->logger->getLastLog();
unset($last_log['id']);
$this->assertEquals($log, $last_log);
}

public function testLastLogOnDisabledLevel()
{
$this->logger = new LoggerMock($this->directory, $this->logger::INFO);
$this->logger->debug('foo');
$this->assertEquals([
'filepath' => null,
'date' => null,
'time' => null,
'level' => null,
'id' => null,
'message' => null,
'written' => null,
], $this->logger->getLastLog());
}
}

0 comments on commit b7336c9

Please sign in to comment.