|
|
@@ -62,24 +62,22 @@ use function strtr; |
|
|
|
* MonoLog is an example implementing this interface. |
|
|
|
*/ |
|
|
|
class Log implements ILogger, IDataLogger { |
|
|
|
private IWriter $logger; |
|
|
|
private ?SystemConfig $config; |
|
|
|
private ?bool $logConditionSatisfied = null; |
|
|
|
private ?Normalizer $normalizer; |
|
|
|
private ?IRegistry $crashReporters; |
|
|
|
private ?IEventDispatcher $eventDispatcher; |
|
|
|
|
|
|
|
/** |
|
|
|
* @param IWriter $logger The logger that should be used |
|
|
|
* @param SystemConfig $config the system config object |
|
|
|
* @param SystemConfig|null $config the system config object |
|
|
|
* @param Normalizer|null $normalizer |
|
|
|
* @param IRegistry|null $registry |
|
|
|
* @param IRegistry|null $crashReporters |
|
|
|
*/ |
|
|
|
public function __construct( |
|
|
|
IWriter $logger, |
|
|
|
private IWriter $logger, |
|
|
|
SystemConfig $config = null, |
|
|
|
Normalizer $normalizer = null, |
|
|
|
IRegistry $registry = null |
|
|
|
private ?IRegistry $crashReporters = null |
|
|
|
) { |
|
|
|
// FIXME: Add this for backwards compatibility, should be fixed at some point probably |
|
|
|
if ($config === null) { |
|
|
@@ -87,13 +85,11 @@ class Log implements ILogger, IDataLogger { |
|
|
|
} |
|
|
|
|
|
|
|
$this->config = $config; |
|
|
|
$this->logger = $logger; |
|
|
|
if ($normalizer === null) { |
|
|
|
$this->normalizer = new Normalizer(); |
|
|
|
} else { |
|
|
|
$this->normalizer = $normalizer; |
|
|
|
} |
|
|
|
$this->crashReporters = $registry; |
|
|
|
$this->eventDispatcher = null; |
|
|
|
} |
|
|
|
|
|
|
@@ -211,15 +207,18 @@ class Log implements ILogger, IDataLogger { |
|
|
|
*/ |
|
|
|
public function log(int $level, string $message, array $context = []) { |
|
|
|
$minLevel = $this->getLogLevel($context); |
|
|
|
if ($level < $minLevel |
|
|
|
&& (($this->crashReporters?->hasReporters() ?? false) === false) |
|
|
|
&& (($this->eventDispatcher?->hasListeners(BeforeMessageLoggedEvent::class) ?? false) === false)) { |
|
|
|
return; // no crash reporter, no listeners, we can stop for lower log level |
|
|
|
} |
|
|
|
|
|
|
|
array_walk($context, [$this->normalizer, 'format']); |
|
|
|
|
|
|
|
$app = $context['app'] ?? 'no app in context'; |
|
|
|
$entry = $this->interpolateMessage($context, $message); |
|
|
|
|
|
|
|
if ($this->eventDispatcher) { |
|
|
|
$this->eventDispatcher->dispatchTyped(new BeforeMessageLoggedEvent($app, $level, $entry)); |
|
|
|
} |
|
|
|
$this->eventDispatcher?->dispatchTyped(new BeforeMessageLoggedEvent($app, $level, $entry)); |
|
|
|
|
|
|
|
$hasBacktrace = isset($entry['exception']); |
|
|
|
$logBacktrace = $this->config->getValue('log.backtrace', false); |
|
|
@@ -241,9 +240,7 @@ class Log implements ILogger, IDataLogger { |
|
|
|
$this->crashReporters->delegateMessage($entry['message'], $messageContext); |
|
|
|
} |
|
|
|
} else { |
|
|
|
if ($this->crashReporters !== null) { |
|
|
|
$this->crashReporters->delegateBreadcrumb($entry['message'], 'log', $context); |
|
|
|
} |
|
|
|
$this->crashReporters?->delegateBreadcrumb($entry['message'], 'log', $context); |
|
|
|
} |
|
|
|
} catch (Throwable $e) { |
|
|
|
// make sure we dont hard crash if logging fails |
|
|
@@ -329,8 +326,10 @@ class Log implements ILogger, IDataLogger { |
|
|
|
$level = $context['level'] ?? ILogger::ERROR; |
|
|
|
|
|
|
|
$minLevel = $this->getLogLevel($context); |
|
|
|
if ($level < $minLevel && ($this->crashReporters === null || !$this->crashReporters->hasReporters())) { |
|
|
|
return; |
|
|
|
if ($level < $minLevel |
|
|
|
&& (($this->crashReporters?->hasReporters() ?? false) === false) |
|
|
|
&& (($this->eventDispatcher?->hasListeners(BeforeMessageLoggedEvent::class) ?? false) === false)) { |
|
|
|
return; // no crash reporter, no listeners, we can stop for lower log level |
|
|
|
} |
|
|
|
|
|
|
|
// if an error is raised before the autoloader is properly setup, we can't serialize exceptions |
|
|
@@ -346,12 +345,9 @@ class Log implements ILogger, IDataLogger { |
|
|
|
$data = array_merge($serializer->serializeException($exception), $data); |
|
|
|
$data = $this->interpolateMessage($data, isset($context['message']) && $context['message'] !== '' ? $context['message'] : ('Exception thrown: ' . get_class($exception)), 'CustomMessage'); |
|
|
|
|
|
|
|
|
|
|
|
array_walk($context, [$this->normalizer, 'format']); |
|
|
|
|
|
|
|
if ($this->eventDispatcher) { |
|
|
|
$this->eventDispatcher->dispatchTyped(new BeforeMessageLoggedEvent($app, $level, $data)); |
|
|
|
} |
|
|
|
$this->eventDispatcher?->dispatchTyped(new BeforeMessageLoggedEvent($app, $level, $data)); |
|
|
|
|
|
|
|
try { |
|
|
|
if ($level >= $minLevel) { |