Skip to content

Commit

Permalink
Merge pull request #44 from nguyenanhung/v3.x
Browse files Browse the repository at this point in the history
Add Sentry
  • Loading branch information
nguyenanhung authored Jan 8, 2023
2 parents d685156 + 840a7f3 commit 415df6a
Show file tree
Hide file tree
Showing 6 changed files with 117 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ jobs:
key: ${{ runner.os }}-composer-${{ hashFiles('**/composer.lock') }}
restore-keys: ${{ runner.os }}-composer-
- name: Install dependencies
run: composer install --no-progress --prefer-dist --optimize-autoloader
run: composer install --no-progress --prefer-dist --optimize-autoloader --ignore-platforms
- name: Test class Logger
run: php ./test/test_logger.php
- name: Test class Utils
Expand Down
14 changes: 9 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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
11 changes: 8 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,17 +27,22 @@
"monolog/monolog": "^2.0 || ^1.26"
},
"require-dev": {
"kint-php/kint": ">=3.0"
"kint-php/kint": ">=3.0",
"sentry/sdk": "^3.0 || ^2.0"
},
"suggest": {
"ext-json": "Needed to support JSON",
"ext-iconv": "Needed to support ICONV",
"ext-mbstring": "Needed to support MbString",
"ext-openssl": "Needed to support OpenSSL"
"ext-openssl": "Needed to support OpenSSL",
"sentry/sdk": "Need support for Logging with Sentry"
},
"autoload": {
"psr-4": {
"nguyenanhung\\MyDebug\\": "src/"
}
},
"files": [
"helpers/sentry.php"
]
}
}
9 changes: 9 additions & 0 deletions helpers/sentry.php
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
*/
106 changes: 89 additions & 17 deletions src/Logger.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down Expand Up @@ -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
*
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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);
}
Expand Down
2 changes: 1 addition & 1 deletion src/Project.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
*/
interface Project
{
const VERSION = '3.0.7';
const VERSION = '3.0.8';

/**
* Hàm lấy thông tin phiên bản Packages
Expand Down

0 comments on commit 415df6a

Please sign in to comment.