-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
145 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Gotphoto\Logging; | ||
|
||
use Monolog\Formatter\NormalizerFormatter; | ||
|
||
/** | ||
* @internal | ||
*/ | ||
final class OtelFormatter extends NormalizerFormatter | ||
{ | ||
/** | ||
* @param array<string, array<callable>> $exceptionContextProviderMap | ||
*/ | ||
public function __construct(private readonly array $exceptionContextProviderMap = []) | ||
{ | ||
parent::__construct(); | ||
} | ||
|
||
/** | ||
* @return array | ||
*/ | ||
protected function normalizeException(\Throwable $e, int $depth = 0) | ||
{ | ||
/** @var array{message: string, context?: array<string, mixed>} $data */ | ||
$data = parent::normalizeException($e, $depth); | ||
|
||
$exceptionProviders = $this->getExceptionContexts($e); | ||
|
||
foreach ($exceptionProviders as $exceptionProvider) { | ||
/** | ||
* @var array<string, mixed> $additionalContext | ||
*/ | ||
$additionalContext = $exceptionProvider($e); | ||
if (!empty($additionalContext)) { | ||
$data['context'] = ($data['context'] ?? []) + $additionalContext; | ||
} | ||
} | ||
|
||
return $data; | ||
} | ||
|
||
/** | ||
* @return array<callable> | ||
*/ | ||
protected function getExceptionContexts(\Throwable $e): array | ||
{ | ||
if (isset($this->exceptionContextProviderMap[\get_class($e)])) { | ||
return $this->exceptionContextProviderMap[\get_class($e)]; | ||
} | ||
$exceptionContexts = []; | ||
foreach (array_keys($this->exceptionContextProviderMap) as $className) { | ||
if ($e instanceof $className) { | ||
$exceptionContexts = array_merge($exceptionContexts + $this->exceptionContextProviderMap[$className]); | ||
} | ||
} | ||
|
||
return $exceptionContexts; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters