pozitronik / yii2-exceptionslogger
Exception logger for YII2
Package info
github.com/pozitronik/yii2-exceptionslogger
Type:yii2-extension
pkg:composer/pozitronik/yii2-exceptionslogger
Requires
- php: >=8.0
- kartik-v/yii2-date-range: ^1.7.0
- kartik-v/yii2-grid: ^3.5.0
- pozitronik/yii2-helpers: ^1.0.9
- pozitronik/yii2-traits: ^1.0.5
- vlucas/phpdotenv: 5.5.x-dev
Requires (Dev)
- codeception/codeception: 5.0.x-dev
- codeception/module-asserts: *
- codeception/module-db: dev-master
- codeception/module-phpbrowser: *
- codeception/module-rest: dev-master
- codeception/module-yii2: dev-master
- yiisoft/yii2: dev-master
- yiisoft/yii2-bootstrap4: ~2.0.10
Suggests
- kartik-v/yii2-bootstrap4-dropdown: Needed for dropdowns in default views
Provides
None
Conflicts
None
Replaces
None
README
Exceptions logging extension for YII2
Installation
The preferred way to install this extension is through composer.
Run
php composer.phar require pozitronik/yii2-exceptionslogger "dev-master"
or add
"pozitronik/yii2-exceptionslogger": "dev-master"
to the require section of your composer.json file.
Requirements
Yii2, PHP >= 8.0
Configuration
Run a included migration:
yii migrate/up --migrationPath=vendor/pozitronik/yii2-exceptionslogger/migrations
It creates the sys_exceptions table, which will store exceptions data.
Usage
Direct logger
This extension provides the SysExceptions::log() function, that can accept any Throwable interface as its first parameter. The exception
data will be saved in sys_exceptions table (in case of failure, the data will be written into runtime/exception.log file).
Example:
try { $i = $i/0; } catch (Throwable $t) { SysExceptions::log($t);//just silently log exception SysExceptions::log(new RuntimeException("Someone tried divide to zero"), false, true);//silently log own exception and mark it as known error SysExceptions::log(new RuntimeException("It prohibited by mathematics"), true);//log own exception and throw it }
Default exceptions handler
You can use the pozitronik\sys_exceptions\models\ErrorHandler class as default application error handler to log all exceptions
automatically. Define it in your application config, like:
$config = [ 'components' => [ 'errorHandler' => [ 'class' => pozitronik\sys_exceptions\models\ErrorHandler::class, 'errorAction' => 'site/error' ] ];
How to add custom data to a log record?
It is possible to add custom string data (e.g. some trace identifier) to every logged error. Configure the customDataHandler parameter for
the SysExceptionsModule module:
$config = [ 'modules' => [ 'SysExceptionsModule' => [ 'params' => [ 'customDataHandler' => static function():string { return Yii::$app->request?->csrfToken;//for example } ] ], ] ]
The data will be stored in the custom_data text field.
License
GNU GPL v3.0