Files
michaelschiemer/src/Application/System/ErrorLogger.php
Michael Schiemer 5050c7d73a docs: consolidate documentation into organized structure
- Move 12 markdown files from root to docs/ subdirectories
- Organize documentation by category:
  • docs/troubleshooting/ (1 file)  - Technical troubleshooting guides
  • docs/deployment/      (4 files) - Deployment and security documentation
  • docs/guides/          (3 files) - Feature-specific guides
  • docs/planning/        (4 files) - Planning and improvement proposals

Root directory cleanup:
- Reduced from 16 to 4 markdown files in root
- Only essential project files remain:
  • CLAUDE.md (AI instructions)
  • README.md (Main project readme)
  • CLEANUP_PLAN.md (Current cleanup plan)
  • SRC_STRUCTURE_IMPROVEMENTS.md (Structure improvements)

This improves:
 Documentation discoverability
 Logical organization by purpose
 Clean root directory
 Better maintainability
2025-10-05 11:05:04 +02:00

61 lines
2.0 KiB
PHP

<?php
declare(strict_types=1);
namespace App\Application\System;
use App\Framework\Core\Events\ErrorOccurred;
use App\Framework\Core\Events\OnEvent;
use App\Framework\Logging\LogContextManager;
use App\Framework\Logging\Logger;
use App\Framework\Logging\ValueObjects\LogContext;
final readonly class ErrorLogger
{
public function __construct(
private ?Logger $logger = null,
private ?LogContextManager $contextManager = null
) {
}
/**
* Logger für aufgetretene Fehler mit strukturiertem Logging
*/
#[OnEvent(priority: 10)]
public function logError(ErrorOccurred $event): void
{
if ($this->logger === null) {
// Fallback für den Fall, dass kein Logger verfügbar ist
$time = $event->occurredAt->format('Y-m-d H:i:s');
$requestId = $event->requestId ? "[Request: {$event->requestId}]" : '';
$message = "[{$time}] ERROR {$requestId} {$event->context}: {$event->error->getMessage()}";
error_log($message);
return;
}
// Strukturiertes Logging mit LogContext
$context = LogContext::withData([
'error_context' => $event->context,
'occurred_at' => $event->occurredAt->format('c'),
'request_id' => $event->requestId,
'error_class' => get_class($event->error),
'error_file' => $event->error->getFile(),
'error_line' => $event->error->getLine(),
])->addTags('error_event', 'application');
// Current context hinzufügen falls verfügbar
if ($this->contextManager !== null) {
$currentContext = $this->contextManager->getCurrentContext();
$mergedContext = $currentContext->merge($context);
} else {
$mergedContext = $context;
}
$this->logger->error(
sprintf('Application error in %s: %s', $event->context, $event->error->getMessage()),
$mergedContext
);
}
}