https://github.com/pozitronik/yii2-exceptionslogger
Exceptions logger for Yii2
https://github.com/pozitronik/yii2-exceptionslogger
Last synced: 3 months ago
JSON representation
Exceptions logger for Yii2
- Host: GitHub
- URL: https://github.com/pozitronik/yii2-exceptionslogger
- Owner: pozitronik
- License: gpl-3.0
- Created: 2020-02-18T16:45:10.000Z (over 5 years ago)
- Default Branch: master
- Last Pushed: 2023-04-01T10:29:08.000Z (about 2 years ago)
- Last Synced: 2025-01-22T03:42:21.882Z (4 months ago)
- Language: PHP
- Size: 104 KB
- Stars: 0
- Watchers: 1
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
ExceptionsLogger
==================
Exceptions logging extension for YII2[](https://github.com/pozitronik/yii2-exceptionslogger/actions)
Installation
------------The preferred way to install this extension is through [composer](http://getcomposer.org/download/).
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:
```php
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:```php
$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:```php
$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