PHP code example of compono-kit / file-error-handler

1. Go to this page and download the library: Download compono-kit/file-error-handler library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

compono-kit / file-error-handler example snippets


use ComponoKit\ErrorHandlers\Files\Configs\ConfiguresFileErrorHandler;

class MyLogConfig implements ConfiguresFileErrorHandler
{
    public function getFilePathName(): string
    {
        return '/var/log/myapp/errors.log';
    }

    public function appendingModeEnabled(): bool
    {
        return true;
    }
}

use ComponoKit\ErrorHandlers\Files\FileErrorHandler;

$handler = new FileErrorHandler(new MyLogConfig());

// Verzeichnis anlegen (einmalig beim App-Start aufrufen)
$handler->install();

// Fehler loggen
$handler->error('Datenbankverbindung fehlgeschlagen');

class OverwriteLogConfig implements ConfiguresFileErrorHandler
{
    public function getFilePathName(): string
    {
        return '/var/log/myapp/latest.log';
    }

    public function appendingModeEnabled(): bool
    {
        return false; // Datei wird bei jedem Schreibvorgang neu erstellt
    }
}

$handler->install();

$handler->error('Benutzer konnte nicht in der Datenbank gespeichert werden');

$handler->warning('Speicherplatz unter 10 % verfügbar');

$handler->critical('Primäre Datenbank nicht erreichbar');

$handler->alert('Zahlungsdienstleister antwortet nicht');

$handler->emergency('System komplett ausgefallen');

try
{
    $orderService->processPayment($orderId);
}
catch (\Throwable $throwable)
{
    $handler->exception($throwable, 'Zahlung konnte nicht verarbeitet werden');
}

use ComponoKit\ErrorHandlers\Files\Configs\ConfiguresFileErrorHandler;
use ComponoKit\ErrorHandlers\Files\FileErrorHandler;

class AppLogConfig implements ConfiguresFileErrorHandler
{
    public function getFilePathName(): string
    {
        return __DIR__ . '/logs/app.log';
    }

    public function appendingModeEnabled(): bool
    {
        return true;
    }
}

$handler = new FileErrorHandler(new AppLogConfig());
$handler->install();

// Im normalen Betrieb
$handler->warning('Cache konnte nicht geladen werden, Fallback aktiv');

// Bei einem aufgefangenen Fehler
try
{
    $userService->deleteUser($userId);
}
catch (\Throwable $throwable)
{
    $handler->exception($throwable, 'Benutzer konnte nicht gelöscht werden');
}