Skip to content

Commit

Permalink
Merge branch 'feature/t_5' into 'main'
Browse files Browse the repository at this point in the history
Sistema per inviare l'utente loggato closes #5

Closes #5

See merge request shellrent-library/kraken-client!1
  • Loading branch information
Armando Caprio committed Apr 2, 2024
2 parents 17cb4a2 + 356eb22 commit 685daa9
Show file tree
Hide file tree
Showing 9 changed files with 78 additions and 5 deletions.
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
.idea
.idea
composer.phar
/vendor/
.app
.env
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,8 @@ $app->singleton(

It is possible to decide on which environments to activate the sending of reports by modifying `enabled_envs` in the config file; by default only the **production** environment is enabled

By default the package does not send the information of the logged in user, they can be added by setting `user_report_builder` in the configuration file, with a callable that returns this information

For more details see [laravel customization](#customization-laravel)

### Logger Usage (Laravel)
Expand Down Expand Up @@ -174,6 +176,7 @@ From the file created in `config/kraken.php` you can edit:
- The environments that trigger the ExceptionHandler
- The type code and builder class of an exception report
- The type code and builder class of an log report
- Signed in user information

For more details see the [configuration file](/src/Laravel/config/config.php)

Expand Down Expand Up @@ -225,6 +228,11 @@ $krakenHandler->addHideDataKey( getenv( 'DATABASE_PASSWORD' ) );
$krakenHandler->addHideDataKey( 'KEY_CLI_ACCESS' );
```

By default the package does not send the information of the logged in user, they can be added by setting `userDataGetter` property in the configuration object, with a callable that returns this information

For more details see [phalcon customization](#customization-phalcon)


### Logger Usage (Phalcon)

It is possible to send single reports and logs via KrakenLogger using `KrakenService`:
Expand Down Expand Up @@ -263,5 +271,6 @@ The config object can be customized for change:
- The type code and builder class of an exception report
- The builder class of an php error report
- The type code and builder class of an log report
- Signed in user information

For more details see the [configuration class](/src/Phalcon/Config/Config.php)
22 changes: 20 additions & 2 deletions src/Laravel/KrakenLogger.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,16 @@ private function dispatch( ReportBuilder $report ): void {

public function log( string $level, string $message ): void {
$builder = config('kraken.log_report_builder' );
$report = (new $builder)( $message, $level );

if( class_exists( $builder ) ) {
$builder = new $builder();
}

if( !is_callable( $builder ) ) {
throw new \Exception( 'kraken.log_report_builder must me a callable entity' );
}

$report = call_user_func( $builder, $message, $level );

$this->dispatch( $report );
}
Expand All @@ -44,7 +53,16 @@ public function onQueue( ?string $queue ): self {

public function exception( Throwable $exception ): void {
$builder = config('kraken.exception_report_builder' );
$report = (new $builder)( $exception );

if( class_exists( $builder ) ) {
$builder = new $builder();
}

if( !is_callable( $builder ) ) {
throw new \Exception( 'kraken.log_report_builder must me a callable entity' );
}

$report = call_user_func( $builder, $exception );

$this->dispatch( $report );
}
Expand Down
2 changes: 2 additions & 0 deletions src/Laravel/ReportBuilder/ExceptionBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ protected function buildReport( Throwable $e ): ReportBuilder {

$report->setModule( $this->getModule() );

$this->addUserInfo( $report );

return $report;
}

Expand Down
15 changes: 15 additions & 0 deletions src/Laravel/ReportBuilder/GenericBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,21 @@ protected function addCliData( ReportBuilder $report ): void {
]);
}

protected function addUserInfo( ReportBuilder $report ): void {
$userBuilder = config('kraken.user_report_builder' );

if( class_exists( $userBuilder ) ) {
$userBuilder = new $userBuilder();
}

if( !$userBuilder or !is_callable( $userBuilder ) ) {
return;
}

$data = call_user_func( $userBuilder );
$report->addExtraInfo( 'user', $data );
}

protected function getModule(): ?string {
return App::runningInConsole() ? config('kraken.cli_module_code' ) : config('kraken.app_module_code' );
}
Expand Down
2 changes: 2 additions & 0 deletions src/Laravel/ReportBuilder/LogBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ protected function buildReport( string $message, string $level ): ReportBuilder
$report->setModule( $this->getModule() );
$report->addExtraInfo( 'level', $level );

$this->addUserInfo( $report );

return $report;
}

Expand Down
13 changes: 13 additions & 0 deletions src/Laravel/config/config.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,4 +91,17 @@
'log_report_type'=> 'LOG',

'log_report_builder' => \Shellrent\KrakenClient\Laravel\ReportBuilder\LogBuilder::class,

/*
|--------------------------------------------------------------------------
| Log report configuration
|--------------------------------------------------------------------------
|
| This value represents the system for obtaining the information
| of the logged in user at the time of generating the report
| Must be a callable element. If null no information is added
|
*/

'user_report_builder' => null,
];
11 changes: 9 additions & 2 deletions src/Phalcon/Config/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Shellrent\KrakenClient\Phalcon\Config;

use Closure;
use Shellrent\KrakenClient\Phalcon\ReportBuilder\ExceptionBuilder;
use Shellrent\KrakenClient\Phalcon\ReportBuilder\FatalErrorBuilder;
use Shellrent\KrakenClient\Phalcon\ReportBuilder\LogBuilder;
Expand All @@ -15,10 +16,11 @@ class Config {
public LogBuilder $logBuilder;
public string $exceptionReportType;
public string $logReportType;
public ?Closure $userDataGetter = null;


public static function default(): self {
$config = new self();

$config->apiEndpoint = getenv( 'KRAKEN_API_ENDPOINT' ) ?? 'localhost';
$config->apiToken = getenv( 'KRAKEN_API_TOKEN' ) ?? 'token';
$config->verifySsl = true;
Expand All @@ -28,7 +30,12 @@ public static function default(): self {
$config->exceptionReportType = 'EXCEPTION';
$config->logReportType = 'LOG';


return $config;
}

public function __clone() {
$this->exceptionBuilder = clone $this->exceptionBuilder;
$this->fatalErrorBuilder = clone $this->fatalErrorBuilder;
$this->logBuilder = clone $this->logBuilder;
}
}
5 changes: 5 additions & 0 deletions src/Phalcon/KrakenLogger.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@ public function __construct( Config $config ) {
}

protected function dispatch( ReportBuilder $report ): void {
if( $this->config->userDataGetter ) {
$userData = call_user_func( $this->config->userDataGetter );
$report->addExtraInfo( 'user', $userData );
}

$this->client->sendReport( $report->getData() );
}

Expand Down

0 comments on commit 685daa9

Please sign in to comment.