From 5967e8cdd60b980771b57eab25caaaab42d2a444 Mon Sep 17 00:00:00 2001 From: HungNA - Technical Manager Date: Mon, 1 Apr 2024 14:13:23 +0700 Subject: [PATCH 1/2] Improve code for version 4.x --- .ide.purpose.php | 7 +++ README.md | 103 ++++++++++++++++++++-------------------- src/Logger.php | 19 ++------ src/Utils.php | 24 ++++++++++ test/func.php | 8 ---- test/test_benchmark.php | 12 +---- test/test_file.php | 13 +---- test/test_logger.php | 9 ---- test/test_utils.php | 9 ---- 9 files changed, 89 insertions(+), 115 deletions(-) create mode 100644 .ide.purpose.php diff --git a/.ide.purpose.php b/.ide.purpose.php new file mode 100644 index 0000000..3076150 --- /dev/null +++ b/.ide.purpose.php @@ -0,0 +1,7 @@ + 'Nguyen An Hung', - 'email' => 'dev@nguyenanhung.com' +$name = 'Test'; +$msg = 'Test Log lan 2'; +$context = [ + 'name' => 'Nguyen An Hung', + 'email' => 'dev@nguyenanhung.com', + 'website' => 'https://nguyenanhung.com', ]; // Call -$debug = new Logger(); -$debug->setDebugStatus(TRUE); -$debug->setGlobalLoggerLevel('info'); -$debug->setLoggerPath($logPath); -$debug->setLoggerSubPath($logSubPath); -$debug->setLoggerFilename($logFilename); -$debug->__construct(); +$logger = new Logger(); +$logger->setDebugStatus(true); +$logger->setGlobalLoggerLevel(); +$logger->setLoggerPath($logPath); +$logger->setLoggerSubPath($logSubPath); +$logger->setLoggerFilename($logFilename); +$logger->__construct(); echo "\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n"; -echo "\n getVersion: " . $debug->getVersion() . "\n"; -echo "\n setDebugStatus: " . $debug->getDebugStatus() . "\n"; -echo "\n setLoggerPath: " . $debug->getLoggerPath() . "\n"; -echo "\n setLoggerSubPath: " . $debug->getLoggerSubPath() . "\n"; -echo "\n setLoggerFilename: " . $debug->getLoggerFilename() . "\n"; +echo "\n getVersion: " . $logger->getVersion() . "\n"; +echo "\n setDebugStatus: " . $logger->getDebugStatus() . "\n"; +echo "\n setLoggerPath: " . $logger->getLoggerPath() . "\n"; +echo "\n setLoggerSubPath: " . $logger->getLoggerSubPath() . "\n"; +echo "\n setLoggerFilename: " . $logger->getLoggerFilename() . "\n"; echo "\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n"; -d($debug->debug($name, $msg . ' - DEBUG', $context)); -d($debug->info($name, $msg . ' - INFO', $context)); -d($debug->notice($name, $msg . ' - NOTICE', $context)); -d($debug->warning($name, $msg . ' - WARNING', $context)); -d($debug->error($name, $msg . ' - ERROR', $context)); -d($debug->critical($name, $msg . ' - CRITICAL', $context)); -d($debug->alert($name, $msg . ' - ALERT', $context)); -d($debug->emergency($name, $msg . ' - EMERGENCY', $context)); +__show__($logger->debug($name, $msg . ' - DEBUG', $context)); +__show__($logger->info($name, $msg . ' - INFO', $context)); +__show__($logger->notice($name, $msg . ' - NOTICE', $context)); +__show__($logger->warning($name, $msg . ' - WARNING', $context)); +__show__($logger->error($name, $msg . ' - ERROR', $context)); +__show__($logger->critical($name, $msg . ' - CRITICAL', $context)); +__show__($logger->alert($name, $msg . ' - ALERT', $context)); +__show__($logger->emergency($name, $msg . ' - EMERGENCY', $context)); ``` ### Benchmark @@ -75,27 +77,24 @@ d($debug->emergency($name, $msg . ' - EMERGENCY', $context)); ```php mark('code_start'); -$mathFunctions = array("abs", "acos", "asin", "atan", "floor", "exp", "sin", "tan", "pi", "is_finite", "is_nan", "sqrt"); -$count = 9999; +$mathFunctions = ["abs", "acos", "asin", "atan", "floor"]; +$count = 10; for ($i = 0; $i < $count; $i++) { - foreach ($mathFunctions as $key => $function) { - $function($i); - echo ($key + 1) . " -> " . $function . "\n"; - } + foreach ($mathFunctions as $key => $function) { + $function($i); + echo ($key + 1) . " -> " . $function . "\n"; + } } $benchmark->mark('code_end'); - -d($benchmark->elapsed_time('code_start', 'code_end')); -d($benchmark->memory_usage()); -/***************************** SIMPLE BENCHMARKING BY CI *****************************/ +__show__($benchmark->elapsed_time('code_start', 'code_end')); +__show__($benchmark->memory_usage()); ``` ### Manage File @@ -103,15 +102,18 @@ d($benchmark->memory_usage()); ```php setExclude(['*.zip']); -$file->setInclude(['*.log']); -d($file->getVersion()); -d($file->cleanLog(__DIR__.'/tmp', 7)); +$file->setExclude(array('*.zip')); +$file->setInclude(array('*.log')); + +$path = __DIR__ . '/../tmp'; +$path = realpath($path); + +__show__($file->directoryScanner($path)); ``` ### Utils @@ -119,16 +121,15 @@ d($file->cleanLog(__DIR__.'/tmp', 7)); ```php getVersion()); // show "2.0.5" -d($utils::slugify($str)); // show "nguyen-an-hung" +$str = 'Nguyễn An Hưng'; +__show__($utils->getVersion()); +__show__($utils::slugify($str)); ``` ## Support @@ -139,4 +140,4 @@ If any question & request, please contact following information |-------------|----------------------|------------------|---------------| | Hung Nguyen | dev@nguyenanhung.com | nguyenanhung5891 | @nguyenanhung | -From Vietnam with Love <3 \ No newline at end of file +From Vietnam with Love <3 diff --git a/src/Logger.php b/src/Logger.php index ecdf1d3..acea806 100644 --- a/src/Logger.php +++ b/src/Logger.php @@ -329,11 +329,7 @@ public function log(string $level = '', string $name = 'log', string $msg = 'My $level = mb_strtolower(trim($level)); if ($this->DEBUG === true) { if (!class_exists(MonoLogger::class)) { - if (function_exists('log_message')) { - $errorMsg = 'Không tồn tại class Monolog'; - log_message('error', $errorMsg); - } - + Utils::log_message('error', 'Class Monolog not exists'); return false; } try { @@ -388,20 +384,13 @@ public function log(string $level = '', string $name = 'log', string $msg = 'My $msg = 'My Log Message is Empty'; } - if (is_array($context)) { - $contextData = $context; - } else { - $contextData = [$context]; - } + $contextData = is_array($context) ? $context : [$context]; $logger->$level($msg, $contextData); return true; } catch (Exception $e) { - if (function_exists('log_message')) { - log_message('error', 'Error Message: ' . $e->getMessage()); - log_message('error', 'Error TraceAsString: ' . $e->getTraceAsString()); - } - + Utils::log_message('error', 'Error Message: ' . $e->getMessage()); + Utils::log_message('error', 'Error TraceAsString: ' . $e->getTraceAsString()); return false; } } diff --git a/src/Utils.php b/src/Utils.php index aea9bdd..8dde11c 100644 --- a/src/Utils.php +++ b/src/Utils.php @@ -41,4 +41,28 @@ public static function slugify(string $str = ''): string { return (new SlugUrl())->slugify($str); } + + /** + * Function log_message - Call to function log_message if function exists + * + * @param string $name + * @param mixed $message + * @return void + */ + public static function log_message(string $name = '', mixed $message = ''): void + { + if (empty($name)) { + $name = 'error'; + } + if (function_exists('log_message') && !empty($message)) { + if (!is_string($message)) { + $message = json_encode( + [ + $message + ] + ); + } + log_message($name, $message); + } + } } diff --git a/test/func.php b/test/func.php index 4b72eb0..219b484 100644 --- a/test/func.php +++ b/test/func.php @@ -1,12 +1,4 @@ - * Copyright: 713uk13m - * Date: 14/01/2023 - * Time: 01:29 - */ if (!function_exists('__show__')) { function __show__($s): void { diff --git a/test/test_benchmark.php b/test/test_benchmark.php index 9a0821f..e9a008e 100644 --- a/test/test_benchmark.php +++ b/test/test_benchmark.php @@ -2,18 +2,9 @@ require_once __DIR__ . '/../vendor/autoload.php'; require __DIR__ . '/func.php'; -/** - * Project vn-telco-detect. - * Created by PhpStorm. - * User: 713uk13m - * Date: 9/18/18 - * Time: 17:30 - */ - use nguyenanhung\MyDebug\Benchmark; $benchmark = new Benchmark(); -/***************************** SIMPLE BENCHMARKING BY CI *****************************/ $benchmark->mark('code_start'); $mathFunctions = ["abs", "acos", "asin", "atan", "floor"]; $count = 10; @@ -25,7 +16,6 @@ } $benchmark->mark('code_end'); - __show__($benchmark->elapsed_time('code_start', 'code_end')); __show__($benchmark->memory_usage()); -/***************************** SIMPLE BENCHMARKING BY CI *****************************/ + diff --git a/test/test_file.php b/test/test_file.php index 45e1620..76a19bf 100644 --- a/test/test_file.php +++ b/test/test_file.php @@ -2,14 +2,6 @@ require_once __DIR__ . '/../vendor/autoload.php'; require __DIR__ . '/func.php'; -/** - * Project vn-telco-detect. - * Created by PhpStorm. - * User: 713uk13m - * Date: 9/18/18 - * Time: 17:30 - */ - use nguyenanhung\MyDebug\Manager\File; $file = new File(); @@ -19,7 +11,4 @@ $path = __DIR__ . '/../tmp'; $path = realpath($path); -d($file->directoryScanner($path)); - - - +__show__($file->directoryScanner($path)); diff --git a/test/test_logger.php b/test/test_logger.php index 5c2046e..e8f052e 100644 --- a/test/test_logger.php +++ b/test/test_logger.php @@ -2,14 +2,6 @@ require_once __DIR__ . '/../vendor/autoload.php'; require __DIR__ . '/func.php'; -/** - * Project vn-telco-detect. - * Created by PhpStorm. - * User: 713uk13m - * Date: 9/18/18 - * Time: 17:30 - */ - use nguyenanhung\MyDebug\Logger; // Test Content @@ -48,4 +40,3 @@ __show__($logger->critical($name, $msg . ' - CRITICAL', $context)); __show__($logger->alert($name, $msg . ' - ALERT', $context)); __show__($logger->emergency($name, $msg . ' - EMERGENCY', $context)); - diff --git a/test/test_utils.php b/test/test_utils.php index 2978e03..97b35a0 100644 --- a/test/test_utils.php +++ b/test/test_utils.php @@ -2,14 +2,6 @@ require_once __DIR__ . '/../vendor/autoload.php'; require __DIR__ . '/func.php'; -/** - * Project vn-telco-detect. - * Created by PhpStorm. - * User: 713uk13m - * Date: 9/18/18 - * Time: 17:30 - */ - use nguyenanhung\MyDebug\Utils; $utils = new Utils(); @@ -17,4 +9,3 @@ __show__($utils->getVersion()); __show__($utils::slugify($str)); - From d4ef4e5dd236509908dbd50abb41205e5676186a Mon Sep 17 00:00:00 2001 From: HungNA - Technical Manager Date: Mon, 1 Apr 2024 14:14:46 +0700 Subject: [PATCH 2/2] Upgrade Project Interface --- src/Project.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Project.php b/src/Project.php index d9547df..1ff4374 100644 --- a/src/Project.php +++ b/src/Project.php @@ -18,7 +18,7 @@ */ interface Project { - const VERSION = '4.0.3'; + const VERSION = '4.0.4'; /** * Hàm lấy thông tin phiên bản Packages