Skip to content

Commit 4eef9ad

Browse files
committed
support context on exception
1 parent 96d0eb0 commit 4eef9ad

4 files changed

Lines changed: 13 additions & 10 deletions

File tree

src/Log.php

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -77,26 +77,26 @@ public static function debug($message, array $context = [])
7777
self::_getLogger()->debug($message, $context);
7878
}
7979

80-
public static function exception(Throwable $e)
80+
public static function exception(Throwable $e, array $context = [])
8181
{
8282
static::critical(
8383
$e->getMessage(),
84-
[
84+
$context + [
8585
'code' => $e->getCode(),
8686
'file' => $e->getFile(),
8787
'line' => $e->getLine(),
8888
]
8989
);
9090
}
9191

92-
public static function exceptionWithTrace(Throwable $e)
92+
public static function exceptionWithTrace(Throwable $e, array $context = [])
9393
{
9494
static::critical(
9595
$e->getMessage(),
96-
[
97-
'code' => $e->getCode(),
98-
'file' => $e->getFile(),
99-
'line' => $e->getLine(),
96+
$context + [
97+
'code' => $e->getCode(),
98+
'file' => $e->getFile(),
99+
'line' => $e->getLine(),
100100
'stack_trace' => $e->getTraceAsString(),
101101
]
102102
);

tests/BasicGoogleCloudLoggerTest.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,11 +98,12 @@ public function testExceptionTraceLog()
9898
Log::bind($this->_getTestLogger());
9999

100100
$e = new Exception('exception message', 123);
101-
Log::exceptionWithTrace($e);
101+
Log::exceptionWithTrace($e, ['extra' => 'additional']);
102102
self::assertContains('"textPayload":"exception message"', $this->_getLogContents());
103103
self::assertContains('"severity":"critical"', $this->_getLogContents());
104104
self::assertContains('"code":123', $this->_getLogContents());
105105
self::assertContains('"line":100', $this->_getLogContents());
106+
self::assertContains('"extra":"additional"', $this->_getLogContents());
106107
self::assertContains('BasicGoogleCloudLoggerTest.php', $this->_getLogContents());
107108
self::assertContains('"stack_trace"', $this->_getLogContents());
108109
}

tests/ErrorLogLoggerTest.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,10 +100,11 @@ public function testExceptionTraceLog()
100100
Log::bind(new ErrorLogLogger());
101101

102102
$e = new Exception('exception message', 123);
103-
Log::exceptionWithTrace($e);
103+
Log::exceptionWithTrace($e, ['extra' => 'additional']);
104104
self::assertContains('[critical] exception message ', $this->_getLogContents());
105105
self::assertContains('"code":123', $this->_getLogContents());
106106
self::assertContains('"line":102', $this->_getLogContents());
107+
self::assertContains('"extra":"additional"', $this->_getLogContents());
107108
self::assertContains('ErrorLogLoggerTest.php', $this->_getLogContents());
108109
self::assertContains('"stack_trace"', $this->_getLogContents());
109110
}

tests/StdOutLoggerTest.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,10 +100,11 @@ public function testExceptionTraceLog()
100100
Log::bind($this->_getTestLogger());
101101

102102
$e = new Exception('exception message', 123);
103-
Log::exceptionWithTrace($e);
103+
Log::exceptionWithTrace($e, ['extra' => 'additional']);
104104
self::assertContains('[CRITICAL] exception message ', $this->_getLogContents());
105105
self::assertContains('"code":123', $this->_getLogContents());
106106
self::assertContains('"line":102', $this->_getLogContents());
107+
self::assertContains('"extra":"additional"', $this->_getLogContents());
107108
self::assertContains('StdOutLoggerTest.php', $this->_getLogContents());
108109
self::assertContains('"stack_trace"', $this->_getLogContents());
109110
}

0 commit comments

Comments
 (0)