-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #44 from nguyenanhung/v3.x
Add Sentry
- Loading branch information
Showing
6 changed files
with
117 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -20,10 +20,14 @@ | |
|
||
Thư viện có sử dụng các gói sau đây | ||
|
||
- [x] monolog/monolog `^2.3` | ||
- [x] cocur/slugify `^4.0` | ||
- [x] theseer/directoryscanner `^1.3` | ||
- [x] symfony/filesystem `^5.3 || ^4.4` | ||
- [x] monolog/monolog `^2.0 || ^1.26` | ||
- [x] nguyenanhung/benchmark `^2.0 || ^1.0` | ||
- [x] "nguyenanhung/filesystem-helper `^2.0 || ^1.0` | ||
- [x] nguyenanhung/slug-helper `^2.0 || ^1.0` | ||
|
||
Ngoài ra, gói cũng hỗ trợ logging lên service ngoài như Sentry, khi đó cần cài thêm gói `sentry/sdk` như dưới đây | ||
|
||
- [x] sentry/sdk `^3.0 || ^2.0` | ||
|
||
## Usage | ||
|
||
|
@@ -140,7 +144,7 @@ d($utils::slugify($str)); // show "nguyen-an-hung" | |
If any question & request, please contact following information | ||
|
||
| Name | Email | Skype | Facebook | | ||
| ----------- | -------------------- | ---------------- | ------------- | | ||
|-------------|----------------------|------------------|---------------| | ||
| Hung Nguyen | [email protected] | nguyenanhung5891 | @nguyenanhung | | ||
|
||
From Vietnam with Love <3 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
<?php | ||
/** | ||
* Project my-debug | ||
* Created by PhpStorm | ||
* User: 713uk13m <[email protected]> | ||
* Copyright: 713uk13m <[email protected]> | ||
* Date: 08/01/2023 | ||
* Time: 23:44 | ||
*/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -57,6 +57,12 @@ class Logger implements Project | |
/** @var string|null Logger Line Format, VD: "[%datetime%] %channel%.%level_name%: %message% %context% %extra%\n" */ | ||
private $loggerLineFormat; | ||
|
||
/** @var bool Cấu hình logging sử dụng Sentry, TRUE nếu cấu hình Sentry được bật */ | ||
private $useSentry = false; | ||
|
||
/** @var array $sentry Sentry configure */ | ||
private $sentry; | ||
|
||
/** | ||
* Logger constructor. | ||
* | ||
|
@@ -97,6 +103,66 @@ public function setDebugStatus(bool $debug = false): Logger | |
return $this; | ||
} | ||
|
||
/** | ||
* Function setUseSentry | ||
* | ||
* @param bool $useSentry | ||
* | ||
* @return $this | ||
* @author : 713uk13m <[email protected]> | ||
* @copyright: 713uk13m <[email protected]> | ||
* @time : 08/01/2023 54:05 | ||
*/ | ||
public function setUseSentry(bool $useSentry) | ||
{ | ||
$this->useSentry = $useSentry; | ||
|
||
return $this; | ||
} | ||
|
||
/** | ||
* Function getUseSentryStatus | ||
* | ||
* @return bool | ||
* @author : 713uk13m <[email protected]> | ||
* @copyright: 713uk13m <[email protected]> | ||
* @time : 08/01/2023 54:28 | ||
*/ | ||
public function getUseSentryStatus(): bool | ||
{ | ||
return $this->useSentry; | ||
} | ||
|
||
/** | ||
* Function setSentryConfigure | ||
* | ||
* @param $sentry | ||
* | ||
* @return $this | ||
* @author : 713uk13m <[email protected]> | ||
* @copyright: 713uk13m <[email protected]> | ||
* @time : 08/01/2023 55:25 | ||
*/ | ||
public function setSentryConfigure($sentry) | ||
{ | ||
$this->sentry = $sentry; | ||
|
||
return $this; | ||
} | ||
|
||
/** | ||
* Function getSentryConfigure | ||
* | ||
* @return array | ||
* @author : 713uk13m <[email protected]> | ||
* @copyright: 713uk13m <[email protected]> | ||
* @time : 08/01/2023 55:21 | ||
*/ | ||
public function getSentryConfigure() | ||
{ | ||
return $this->sentry; | ||
} | ||
|
||
/** | ||
* Function getGlobalLoggerLevel - Hàm get Level lưu log cho toàn hệ thống | ||
* | ||
|
@@ -342,14 +408,10 @@ public function log(string $level = '', string $name = 'log', string $msg = 'My | |
$this->loggerFilename = 'Log-' . date('Y-m-d') . '.log'; | ||
} | ||
$listLevel = array('debug', 'info', 'notice', 'warning', 'error', 'critical', 'alert', 'emergency'); | ||
if ( | ||
// Tồn tại Global Logger Level | ||
isset($this->globalLoggerLevel) && | ||
// Là 1 string | ||
is_string($this->globalLoggerLevel) && | ||
// Và thuộc list Level được quy định | ||
in_array($this->globalLoggerLevel, $listLevel, true) | ||
) { | ||
if (// Tồn tại Global Logger Level | ||
isset($this->globalLoggerLevel) && // Là 1 string | ||
is_string($this->globalLoggerLevel) && // Và thuộc list Level được quy định | ||
in_array($this->globalLoggerLevel, $listLevel, true)) { | ||
// If valid globalLoggerLevel -> use globalLoggerLevel | ||
$useLevel = strtolower($this->globalLoggerLevel); | ||
} else { | ||
|
@@ -384,17 +446,27 @@ public function log(string $level = '', string $name = 'log', string $msg = 'My | |
default: | ||
$keyLevel = MonoLogger::WARNING; | ||
} | ||
$loggerFilename = $this->loggerPath . DIRECTORY_SEPARATOR . $loggerSubPath . DIRECTORY_SEPARATOR . $this->loggerFilename; | ||
$dateFormat = !empty($this->loggerDateFormat) ? $this->loggerDateFormat : "Y-m-d H:i:s u"; | ||
$output = !empty($this->loggerLineFormat) ? $this->loggerLineFormat : "[%datetime%] %channel%.%level_name%: %message% %context% %extra%\n"; | ||
$formatter = new MonoLineFormatter($output, $dateFormat); | ||
$stream = new MonoStreamHandler($loggerFilename, $keyLevel, self::LOG_BUBBLE, self::FILE_PERMISSION); | ||
$stream->setFormatter($formatter); | ||
$logger = new MonoLogger(ucfirst(trim($name))); | ||
$logger->pushHandler($stream); | ||
$loggerName = ucfirst(trim($name)); | ||
|
||
if ($this->useSentry === true && is_array($this->sentry) && isset($this->sentry['dsn'])) { | ||
$client = \Sentry\ClientBuilder::create(['dsn' => $this->sentry['dsn']])->getClient(); | ||
$handler = new \Sentry\Monolog\Handler(new \Sentry\State\Hub($client)); | ||
$logger = new MonoLogger($loggerName); | ||
$logger->pushHandler($handler); | ||
} else { | ||
$loggerFilename = $this->loggerPath . DIRECTORY_SEPARATOR . $loggerSubPath . DIRECTORY_SEPARATOR . $this->loggerFilename; | ||
$dateFormat = !empty($this->loggerDateFormat) ? $this->loggerDateFormat : "Y-m-d H:i:s u"; | ||
$output = !empty($this->loggerLineFormat) ? $this->loggerLineFormat : "[%datetime%] %channel%.%level_name%: %message% %context% %extra%\n"; | ||
$formatter = new MonoLineFormatter($output, $dateFormat); | ||
$stream = new MonoStreamHandler($loggerFilename, $keyLevel, self::LOG_BUBBLE, self::FILE_PERMISSION); | ||
$stream->setFormatter($formatter); | ||
$logger = new MonoLogger($loggerName); | ||
$logger->pushHandler($stream); | ||
} | ||
if (empty($msg)) { | ||
$msg = 'My Log Message is Empty'; | ||
$msg = 'Input Log Message is Empty'; | ||
} | ||
|
||
if (is_array($context)) { | ||
return $logger->$level($msg, $context); | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters