Skip to content

Commit

Permalink
Merge pull request #1 from packaged/context
Browse files Browse the repository at this point in the history
log context items
  • Loading branch information
TomK authored Nov 15, 2024
2 parents 06a6751 + 00878fc commit 624912f
Show file tree
Hide file tree
Showing 9 changed files with 311 additions and 120 deletions.
6 changes: 3 additions & 3 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,10 @@ jobs:
<<: *defaults
docker:
- image: php:7.3-alpine
build-phpRC:
build-php74:
<<: *defaults
docker:
- image: php:rc-alpine
- image: php:7.4-alpine

workflows:
version: 2
Expand All @@ -46,4 +46,4 @@ workflows:
- build-php71
- build-php72
- build-php73
- build-phpRC
- build-php74
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
}
],
"require": {
"ext-json": "*",
"php": ">=7.1",
"psr/log": "1.1.*"
},
Expand Down
39 changes: 39 additions & 0 deletions src/BasicGoogleCloudLogger.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 BasicGoogleCloudLogger extends ErrorLogLogger
{
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([
'timestamp' => (new \DateTime())->format(DATE_RFC3339_EXTENDED),
'severity' => $level,
'textPayload' => $message,
'jsonPayload' => $context,
])) . PHP_EOL;
}
}
18 changes: 16 additions & 2 deletions src/ErrorLogLogger.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,11 @@ public function __construct($maxLevel = LogLevel::DEBUG)
$this->setMaxLogLevel($maxLevel);
}

protected function _writeLog($message)
{
error_log($message);
}

/**
* @param string $level One of the Psr\Log\LogLevel constants
*/
Expand All @@ -41,12 +46,21 @@ public function setMaxLogLevel($level)
$this->_maxLevel = $this->_levelToNum($level);
}

public function log($level, $message, array $context = [])
public function log($level, $message, array $context = null)
{
if($this->_levelToNum($level) <= $this->_maxLevel)
{
error_log($message);
$this->_writeLog($this->_formatLog($level, $message, $context));
}
}

protected function _formatLog($level, $message, array $context = null)
{
if(!empty($context))
{
$message .= ' ' . json_encode($context);
}
return "[$level] $message";
}

private function _levelToNum($level)
Expand Down
19 changes: 17 additions & 2 deletions src/Log.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,11 +79,26 @@ public static function debug($message, array $context = [])

public static function exception(Throwable $e)
{
static::error("EXCEPTION (" . $e->getCode() . "): " . $e->getMessage());
static::critical(
$e->getMessage(),
[
'code' => $e->getCode(),
'file' => $e->getFile(),
'line' => $e->getLine(),
]
);
}

public static function exceptionWithTrace(Throwable $e)
{
static::error("EXCEPTION (" . $e->getCode() . "): " . $e->getMessage() . "\n" . $e->getTraceAsString());
static::critical(
$e->getMessage(),
[
'code' => $e->getCode(),
'file' => $e->getFile(),
'line' => $e->getLine(),
'stack_trace' => $e->getTraceAsString(),
]
);
}
}
31 changes: 0 additions & 31 deletions tests/AbstractLoggerTestCase.php

This file was deleted.

118 changes: 118 additions & 0 deletions tests/BasicGoogleCloudLoggerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
<?php
namespace Packaged\Log\Tests;

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

class BasicGoogleCloudLoggerTest 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 BasicGoogleCloudLogger($maxLevel);
$l->setHandle($this->_handler);
return $l;
}

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

Log::debug('debug: test');
self::assertLastLog('","severity":"debug","textPayload":"debug: test"}');

Log::info('info: test');
self::assertLastLog('","severity":"info","textPayload":"info: test"}');

Log::notice('notice: test');
self::assertLastLog('","severity":"notice","textPayload":"notice: test"}');

Log::warning('warning: test');
self::assertLastLog('","severity":"warning","textPayload":"warning: test"}');

Log::error('error: test');
self::assertLastLog('","severity":"error","textPayload":"error: test"}');

Log::critical('critical: test');
self::assertLastLog('","severity":"critical","textPayload":"critical: test"}');

Log::alert('alert: test');
self::assertLastLog('","severity":"alert","textPayload":"alert: test"}');

Log::emergency('emergency: test');
self::assertLastLog('","severity":"emergency","textPayload":"emergency: test"}');
}

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

Log::info('info: test');
self::assertLastLog('","severity":"info","textPayload":"info: test"}');

Log::debug('debug: test');
self::assertLastLog('","severity":"info","textPayload":"info: test"}');
}

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

$e = new Exception('exception message', 123);
Log::exception($e);
self::assertContains(',"textPayload":"exception message"', $this->_getLogContents());
self::assertContains(',"severity":"critical"', $this->_getLogContents());
self::assertNotContains('"stace_trace":', $this->_getLogContents());
}

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

$e = new Exception('exception message', 123);
Log::exceptionWithTrace($e);
self::assertContains('"textPayload":"exception message"', $this->_getLogContents());
self::assertContains('"severity":"critical"', $this->_getLogContents());
self::assertContains('"code":123', $this->_getLogContents());
self::assertContains('"line":100', $this->_getLogContents());
self::assertContains('BasicGoogleCloudLoggerTest.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(
'","severity":"debug","textPayload":"debug: test","jsonPayload":{"test1":"value1","test2":"value2"}}'
);
}
}
Loading

0 comments on commit 624912f

Please sign in to comment.