Skip to content
Permalink
Branch: master
Find file Copy path
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
100 lines (91 sloc) 2.89 KB

Error

The Error provides more control over displaying and logging PHP errors.

Installation

composer require webiik/error

Example

$error = new \Webiik\Error\Error();

Silent mode

setSilent

setSilent(bool $bool): void

setSilent() activates or deactivates silent mode. When Error is not in silent mode it halts code execution on every error and then it displays error message. In silent mode instead of displaying error message a custom error page is shown and some errors can be completely ignored.

$error->setSilent(true);

setSilentPageContent

setSilentPageContent(string $string): void

setSilentPageContent() sets custom error page to be shown when error occurs in silent mode.

$error->setSilentPageContent('<h1>Ups! Kitten lost!</h1>');

setSilentIgnoreErrors

setSilentIgnoreErrors(array $arr): void

setSilentIgnoreErrors() sets an array of error constants that will be ignored in silent mode - no custom error page will be shown and code execution will not stop.

$error->setSilentIgnoreErrors([
    'E_NOTICE',
    'E_USER_NOTICE',
    'E_DEPRECATED',
    'E_USER_DEPRECATED',
]);

Logging

setErrLogDefLevel

setErrLogDefLevel(string $level): void

setErrLogDefLevel() associates PSR-3 log level to all PHP error constant. The default value is warning.

$error->setErrLogDefLevel('error');

Note: Please keep on mind that PHP error constant (eg. E_NOTICE) and PSR-3 log level (eg. notice) are two different things and there is no connection between them.

setErrLogLevel

setErrLogLevel(array $assocArr): void

setErrLogLevel() associates PSR-3 log level to specific PHP error constant. Default values are shown in the example below.

$error->setErrLogLevel([
    'Exception' => 'error',
    'E_ERROR' => 'error',
]);

setLogService

setLogService(callable $function): void

setLogService() sets custom logger logging errors. function is injected with the following parameters: string $level, string $message, array $data. By default all PHP errors are logged with error_log().

$error->setLogService(function ($level, $message, $data) {
    // $level - PSR-3 log level
    // $message - Re-fromatted error message
    // $data - ['error type' => $errType, 'file' => $file, 'line' => $line, 'error message' => $message, 'trace' => $trace]
    // Your custom logger...
});

Resources

You can’t perform that action at this time.