PHP code example of timefrontiers / php-error-log

1. Go to this page and download the library: Download timefrontiers/php-error-log 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/ */

    

timefrontiers / php-error-log example snippets


use TimeFrontiers\ErrorLog;
use TimeFrontiers\ErrorLogging\LogPolicy;

$log = new ErrorLog(
    base_dir: '/srv/linktude/var',
    process_name: 'billing-operation',
    is_error_log: true,
    policy: new LogPolicy(
        maxErrors: 50,
        maxRecordBytes: 65_536,
        maxFileBytes: 10_485_760,
        retentionSeconds: 2_592_000,
        maxRetentionDeletes: 1_000,
        maxReadBytes: 10_485_760,
    ),
);

$log->setFile('/srv/linktude/var/custom/import.log'); // inside configured root

use TimeFrontiers\ResponseStatus;

$log->write(ResponseStatus::NO_ERROR, 'Import completed'); // 0.0
$log->write(ResponseStatus::NO_TASK, 'Nothing changed');   // 0.1
$log->write(ResponseStatus::NO_DATA, 'No rows found');     // 0.2

$log->write(
    ResponseStatus::PROCESS_ERROR,
    'The import could not be completed.',
    ['The input archive was rejected.'],                   // 3.1
);

use TimeFrontiers\ErrorLogging\LogRecord;

$log->writeRecord(new LogRecord(
    status: ResponseStatus::THIRD_PARTY_ERROR,
    publicMessage: 'The delivery provider rejected the request.',
    safeDetails: ['The operation can be retried.'],
    internalContext: [
        'provider' => 'example',
        'attempt' => 2,
        'token' => $providerToken,       // value becomes [REDACTED]
        'exception' => $exception,       // class only; no message or trace
        'provider_payload' => $payload,  // value becomes [REDACTED]
    ],
));

use TimeFrontiers\ErrorLogging\ReadState;

$result = $log->readResult(page: 1, limit: 25);

if ($result->state === ReadState::OK || $result->state === ReadState::PARTIAL) {
    foreach ($result->entries as $entry) {
        // status, date, subject, content
    }
}