From e2430437a0f75f32c998506f37fc95dcb19ab05f Mon Sep 17 00:00:00 2001 From: Armando Caprio Date: Wed, 27 Mar 2024 16:38:55 +0100 Subject: [PATCH 1/2] =?UTF-8?q?Implementato=20in=20laravel=20la=20possibil?= =?UTF-8?q?it=C3=A0=20di=20fornire=20i=20dati=20utente?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 4 +++- README.md | 2 ++ src/Laravel/KrakenLogger.php | 22 +++++++++++++++++-- .../ReportBuilder/ExceptionBuilder.php | 2 ++ src/Laravel/ReportBuilder/GenericBuilder.php | 15 +++++++++++++ src/Laravel/ReportBuilder/LogBuilder.php | 2 ++ src/Laravel/config/config.php | 13 +++++++++++ src/Phalcon/Config/Config.php | 4 ++++ 8 files changed, 61 insertions(+), 3 deletions(-) diff --git a/.gitignore b/.gitignore index 9651317..4dc6d0e 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,5 @@ -.idea +.idea composer.phar /vendor/ +.app +.env \ No newline at end of file diff --git a/README.md b/README.md index 8047a50..420f0b6 100644 --- a/README.md +++ b/README.md @@ -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) diff --git a/src/Laravel/KrakenLogger.php b/src/Laravel/KrakenLogger.php index 8d504a0..942acd1 100755 --- a/src/Laravel/KrakenLogger.php +++ b/src/Laravel/KrakenLogger.php @@ -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 ); } @@ -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 ); } diff --git a/src/Laravel/ReportBuilder/ExceptionBuilder.php b/src/Laravel/ReportBuilder/ExceptionBuilder.php index 7ca31e1..4d0ec2a 100755 --- a/src/Laravel/ReportBuilder/ExceptionBuilder.php +++ b/src/Laravel/ReportBuilder/ExceptionBuilder.php @@ -18,6 +18,8 @@ protected function buildReport( Throwable $e ): ReportBuilder { $report->setModule( $this->getModule() ); + $this->addUserInfo( $report ); + return $report; } diff --git a/src/Laravel/ReportBuilder/GenericBuilder.php b/src/Laravel/ReportBuilder/GenericBuilder.php index 15c060f..e9e45e0 100755 --- a/src/Laravel/ReportBuilder/GenericBuilder.php +++ b/src/Laravel/ReportBuilder/GenericBuilder.php @@ -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' ); } diff --git a/src/Laravel/ReportBuilder/LogBuilder.php b/src/Laravel/ReportBuilder/LogBuilder.php index 0d76cf4..48320e2 100755 --- a/src/Laravel/ReportBuilder/LogBuilder.php +++ b/src/Laravel/ReportBuilder/LogBuilder.php @@ -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; } diff --git a/src/Laravel/config/config.php b/src/Laravel/config/config.php index c328519..65fcbe4 100755 --- a/src/Laravel/config/config.php +++ b/src/Laravel/config/config.php @@ -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, ]; diff --git a/src/Phalcon/Config/Config.php b/src/Phalcon/Config/Config.php index 5216edf..9348830 100755 --- a/src/Phalcon/Config/Config.php +++ b/src/Phalcon/Config/Config.php @@ -31,4 +31,8 @@ public static function default(): self { return $config; } + + public function __clone() { + //TODO + } } \ No newline at end of file From 356eb22ed4754d5e3840f87a60b3bb1cd7a4d63c Mon Sep 17 00:00:00 2001 From: Armando Caprio Date: Tue, 2 Apr 2024 16:26:33 +0200 Subject: [PATCH 2/2] =?UTF-8?q?Aggiunta=20la=20possibilit=C3=A0=20di=20imp?= =?UTF-8?q?ostare=20il=20modo=20di=20ottenere=20le=20informazioni=20di=20u?= =?UTF-8?q?n=20utente=20loggato?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 7 +++++++ src/Phalcon/Config/Config.php | 9 ++++++--- src/Phalcon/KrakenLogger.php | 5 +++++ 3 files changed, 18 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 420f0b6..5d42e39 100644 --- a/README.md +++ b/README.md @@ -176,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) @@ -227,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`: @@ -265,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) diff --git a/src/Phalcon/Config/Config.php b/src/Phalcon/Config/Config.php index 9348830..af578fa 100755 --- a/src/Phalcon/Config/Config.php +++ b/src/Phalcon/Config/Config.php @@ -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; @@ -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; @@ -28,11 +30,12 @@ public static function default(): self { $config->exceptionReportType = 'EXCEPTION'; $config->logReportType = 'LOG'; - return $config; } public function __clone() { - //TODO + $this->exceptionBuilder = clone $this->exceptionBuilder; + $this->fatalErrorBuilder = clone $this->fatalErrorBuilder; + $this->logBuilder = clone $this->logBuilder; } } \ No newline at end of file diff --git a/src/Phalcon/KrakenLogger.php b/src/Phalcon/KrakenLogger.php index 7afd7b4..4b497dd 100755 --- a/src/Phalcon/KrakenLogger.php +++ b/src/Phalcon/KrakenLogger.php @@ -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() ); }