Skip to content

Commit

Permalink
support context on exception
Browse files Browse the repository at this point in the history
  • Loading branch information
TomK committed Nov 20, 2024
1 parent 96d0eb0 commit 4eef9ad
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 10 deletions.
14 changes: 7 additions & 7 deletions src/Log.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,26 +77,26 @@ public static function debug($message, array $context = [])
self::_getLogger()->debug($message, $context);
}

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

public static function exceptionWithTrace(Throwable $e)
public static function exceptionWithTrace(Throwable $e, array $context = [])
{
static::critical(
$e->getMessage(),
[
'code' => $e->getCode(),
'file' => $e->getFile(),
'line' => $e->getLine(),
$context + [
'code' => $e->getCode(),
'file' => $e->getFile(),
'line' => $e->getLine(),
'stack_trace' => $e->getTraceAsString(),
]
);
Expand Down
3 changes: 2 additions & 1 deletion tests/BasicGoogleCloudLoggerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -98,11 +98,12 @@ public function testExceptionTraceLog()
Log::bind($this->_getTestLogger());

$e = new Exception('exception message', 123);
Log::exceptionWithTrace($e);
Log::exceptionWithTrace($e, ['extra' => 'additional']);
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('"extra":"additional"', $this->_getLogContents());
self::assertContains('BasicGoogleCloudLoggerTest.php', $this->_getLogContents());
self::assertContains('"stack_trace"', $this->_getLogContents());
}
Expand Down
3 changes: 2 additions & 1 deletion tests/ErrorLogLoggerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -100,10 +100,11 @@ public function testExceptionTraceLog()
Log::bind(new ErrorLogLogger());

$e = new Exception('exception message', 123);
Log::exceptionWithTrace($e);
Log::exceptionWithTrace($e, ['extra' => 'additional']);
self::assertContains('[critical] exception message ', $this->_getLogContents());
self::assertContains('"code":123', $this->_getLogContents());
self::assertContains('"line":102', $this->_getLogContents());
self::assertContains('"extra":"additional"', $this->_getLogContents());
self::assertContains('ErrorLogLoggerTest.php', $this->_getLogContents());
self::assertContains('"stack_trace"', $this->_getLogContents());
}
Expand Down
3 changes: 2 additions & 1 deletion tests/StdOutLoggerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -100,10 +100,11 @@ public function testExceptionTraceLog()
Log::bind($this->_getTestLogger());

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

0 comments on commit 4eef9ad

Please sign in to comment.