Skip to content

Commit

Permalink
add StdOutLogger
Browse files Browse the repository at this point in the history
  • Loading branch information
TomK committed Nov 19, 2024
1 parent 624912f commit 15ead0b
Show file tree
Hide file tree
Showing 3 changed files with 157 additions and 19 deletions.
20 changes: 1 addition & 19 deletions src/BasicGoogleCloudLogger.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,26 +7,8 @@
/**
* Logger that outputs the messages using error_log()
*/
class BasicGoogleCloudLogger extends ErrorLogLogger
class BasicGoogleCloudLogger extends StdOutLogger
{
private $_handle;

public function __construct($maxLevel = LogLevel::DEBUG)
{
parent::__construct($maxLevel);
$this->_handle = fopen('php://stderr', 'wb');
}

public function setHandle($handle)
{
$this->_handle = $handle;
}

protected function _writeLog($message)
{
fwrite($this->_handle, $message);
}

protected function _formatLog($level, $message, array $context = null)
{
return json_encode(array_filter([
Expand Down
39 changes: 39 additions & 0 deletions src/StdOutLogger.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php
namespace Packaged\Log;

use Psr\Log\AbstractLogger;
use Psr\Log\LogLevel;

/**
* Logger that outputs the messages using error_log()
*/
class StdOutLogger extends ErrorLogLogger
{
private $_handle;

public function __construct($maxLevel = LogLevel::DEBUG)
{
parent::__construct($maxLevel);
$this->setHandle(STDOUT);
}

public function setHandle($handle)
{
$this->_handle = $handle;
}

protected function _writeLog($message)
{
fwrite($this->_handle, $message);
}

protected function _formatLog($level, $message, array $context = null)
{
if(!empty($context))
{
$message .= ' ' . json_encode($context);
}
$ulevel = strtoupper($level);
return "[{$ulevel}] $message" . PHP_EOL;
}
}
117 changes: 117 additions & 0 deletions tests/StdOutLoggerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
<?php
namespace Packaged\Log\Tests;

use Exception;
use Packaged\Log\BasicGoogleCloudLogger;
use Packaged\Log\ErrorLogLogger;
use Packaged\Log\Log;
use Packaged\Log\StdOutLogger;
use PHPUnit\Framework\TestCase;
use Psr\Log\LogLevel;

class StdOutLoggerTest extends TestCase
{
private $_tempFile;
private $_handler;

public function setUp()
{
$this->_tempFile = tempnam(sys_get_temp_dir(), 'packaged-log-');
$this->_handler = fopen($this->_tempFile, 'wb');
}

public function tearDown()
{
fclose($this->_handler);
unlink($this->_tempFile);
}

protected function _getLogContents()
{
return file_get_contents($this->_tempFile);
}

public function assertLastLog($test)
{
self::assertStringEndsWith($test . PHP_EOL, $this->_getLogContents());
}

private function _getTestLogger($maxLevel = LogLevel::DEBUG)
{
$l = new StdOutLogger($maxLevel);
$l->setHandle($this->_handler);
return $l;
}

public function testErrorLogLogger()
{
Log::bind($this->_getTestLogger());

Log::debug('debug: test');
self::assertLastLog('debug: test');

Log::info('info: test');
self::assertLastLog('info: test');

Log::notice('notice: test');
self::assertLastLog('notice: test');

Log::warning('warning: test');
self::assertLastLog('warning: test');

Log::error('error: test');
self::assertLastLog('error: test');

Log::critical('critical: test');
self::assertLastLog('critical: test');

Log::alert('alert: test');
self::assertLastLog('alert: test');

Log::emergency('emergency: test');
self::assertLastLog('emergency: test');
}

public function testLevelLog()
{
Log::bind($this->_getTestLogger(LogLevel::INFO));

Log::info('info: test');
self::assertLastLog('info: test');

Log::debug('debug: test');
self::assertLastLog('info: test');
}

public function testExceptionLog()
{
Log::bind($this->_getTestLogger());

$e = new Exception('exception message', 123);
Log::exception($e);
self::assertContains('[CRITICAL] exception message ', $this->_getLogContents());
self::assertContains('"code":123', $this->_getLogContents());
self::assertContains('"line":90', $this->_getLogContents());
self::assertContains('StdOutLoggerTest.php', $this->_getLogContents());
}

public function testExceptionTraceLog()
{
Log::bind($this->_getTestLogger());

$e = new Exception('exception message', 123);
Log::exceptionWithTrace($e);
self::assertContains('[CRITICAL] exception message ', $this->_getLogContents());
self::assertContains('"code":123', $this->_getLogContents());
self::assertContains('"line":102', $this->_getLogContents());
self::assertContains('StdOutLoggerTest.php', $this->_getLogContents());
self::assertContains('"stack_trace"', $this->_getLogContents());
}

public function testContextLog()
{
Log::bind($this->_getTestLogger());
Log::debug('debug: test', ['test1' => 'value1', 'test2' => 'value2']);
self::assertLastLog('debug: test {"test1":"value1","test2":"value2"}');
}
}

0 comments on commit 15ead0b

Please sign in to comment.