Package installation in laravel
Download Package
Download the package for Laravel from the
This link,
then extract it in the project root folder.
The new folders should be like this: packages > itpath > lighthouse.
Update composer.json file of the project
Add the namespace of our package in “autoload > psr-4”:
"autoload": {
"psr-4": {
...
...
"Itpath\\Lighthouse\\": "packages/itpath/lighthouse/src/",
},
}
Now, since we’ve edited our project’s composer.json file, we should reload it by running the following command in the project root directory:
composer dump-autoload
Add variable in .env
If you don't have project id or deliverable id then you can get these keys after creating the new project and deliverable.
LIGHTHOUSE_PROJECT_ID = {project-id}
LIGHTHOUSE_DELIVERABLE_ID = {deliverable-id}
Update config/app.php file
'LIGHTHOUSE_PROJECT_ID' => env('LIGHTHOUSE_PROJECT_ID', ''),
'LIGHTHOUSE_DELIVERABLE_ID' => env('LIGHTHOUSE_DELIVERABLE_ID', ''),
Run "composer dump-autoload" again in the root folder.
Use In Laravel
Use In try-catch
This example shows how to use the package inside a try-catch block:
use Itpath\Lighthouse\Lighthouse;
...
public function testError()
{
try {
echo 100 / 0;
} catch (Throwable $e) {
$newLighthouse = new Lighthouse();
$newLighthouse->add($e);
}
}
If you want to apply this globally at the project level, update the
register()
method inside the
app\Exceptions\Handler.php file:
use Itpath\Lighthouse\Lighthouse;
...
public function register()
{
$this->reportable(function (Throwable $e) {
$newLighthouse = new Lighthouse();
$newLighthouse->add($e);
});
}
Methods
add()
The add()
method can accept 3 arguments:
The first should be an instance of the Exception or a string.(required)
$newLighthouse->add($e);
You can pass the second argument as a log type (optional - default error type will be used). The valid log type is debug, warning, notice, info and error
$newLighthouse->add($e, 'error');
The third is an array (optional). You can pass the array as additional data.
$additional_data = [
'user_data' => [
'name' => 'John',
'id' => 1
],
'order_data' => [
'order_id' => '001'
]
];
$newLighthouse = new Lighthouse();
$newLighthouse->add('User Order Data', 'debug', $additional_data);
addError(), addWarning(), addInfo(), addDebug(), addNotice()
These are used to add log with specific type. They accept 2 arguments:
- The first should be an instance of the Exception or string. (required)
- The third is an array (optional) for additional data
Example: addWarning()
will add the new log with warning type.