From 97692dfd0375668096cd530df6b51f7f179424a0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A6=82=E6=9E=9C=E7=9A=84=E5=A6=82=E6=9E=9C?= Date: Sun, 7 Mar 2021 19:38:40 +0800 Subject: [PATCH] =?UTF-8?q?log=E5=8D=B3=E4=B8=BA=E5=86=99=E6=97=A5?= =?UTF-8?q?=E5=BF=97=EF=BC=8C=E5=B9=B6=E9=80=89=E6=8B=A9=E6=80=A7=E8=BE=93?= =?UTF-8?q?=E5=87=BA=E8=87=B3=E6=8E=A7=E5=88=B6=E5=8F=B0=EF=BC=8Cconsole?= =?UTF-8?q?=E5=8F=AF=E9=80=89=E6=89=A7=E8=A1=8C=E6=80=A7=E6=98=AF=E5=90=A6?= =?UTF-8?q?=E8=BE=93=E5=87=BA=E8=87=B3=E6=8E=A7=E5=88=B6=E5=8F=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/Core.php | 3 +++ src/Logger.php | 49 ++++++++++++++++++++++++---------------- src/Resource/Config._php | 1 + 3 files changed, 33 insertions(+), 20 deletions(-) diff --git a/src/Core.php b/src/Core.php index 4f996bb..3a4bfd6 100644 --- a/src/Core.php +++ b/src/Core.php @@ -186,6 +186,9 @@ private function registerErrorHandler() $ignoreCategory = Config::getInstance()->getConf('LOG.ignoreCategory'); Logger::getInstance()->ignoreCategory($ignoreCategory); + $displayConsole = Config::getInstance()->getConf('LOG.displayConsole'); + Logger::getInstance()->displayConsole($displayConsole); + //初始化追追踪器 $trigger = Di::getInstance()->get(SysConst::TRIGGER_HANDLER); if (!$trigger instanceof TriggerInterface) { diff --git a/src/Logger.php b/src/Logger.php index cbb2211..84a2e2a 100644 --- a/src/Logger.php +++ b/src/Logger.php @@ -21,6 +21,8 @@ class Logger private $logConsole = true; + private $displayConsole = true; + private $ignoreCategory = []; private $logLevel = LoggerInterface::LOG_LEVEL_INFO; @@ -33,6 +35,11 @@ function __construct(LoggerInterface $logger) $this->callback = new Event(); } + public function onLog(): Event + { + return $this->callback; + } + public function logLevel(?int $level = null) { if ($level !== null) { @@ -42,6 +49,16 @@ public function logLevel(?int $level = null) return $this->logLevel; } + public function displayConsole(?bool $is = null) + { + if($is === null){ + return $this->displayConsole; + }else{ + $this->displayConsole = $is; + return $this; + } + } + public function logConsole(?bool $is = null) { if ($is === null) { @@ -72,6 +89,10 @@ public function log(?string $msg, int $logLevel = LoggerInterface::LOG_LEVEL_DEB return; } + if ($this->logConsole) { + $this->console($msg, $logLevel, $category); + } + $this->logger->log($msg, $logLevel, $category); $calls = $this->callback->all(); foreach ($calls as $call) { @@ -81,47 +102,35 @@ public function log(?string $msg, int $logLevel = LoggerInterface::LOG_LEVEL_DEB public function console(?string $msg, int $logLevel = LoggerInterface::LOG_LEVEL_DEBUG, string $category = 'debug') { - if ($logLevel < $this->logLevel) { - return; - } - - if (in_array($category, $this->ignoreCategory)) { - return; - } - - $this->logger->console($msg, $logLevel, $category); - if ($this->logConsole) { - $this->log($msg, $logLevel, $category); + if($this->displayConsole){ + $this->logger->console($msg, $logLevel, $category); } } public function debug(?string $msg, string $category = 'debug') { - $this->console($msg, LoggerInterface::LOG_LEVEL_DEBUG, $category); + $this->log($msg, LoggerInterface::LOG_LEVEL_DEBUG, $category); } public function info(?string $msg, string $category = 'info') { - $this->console($msg, LoggerInterface::LOG_LEVEL_INFO, $category); + $this->log($msg, LoggerInterface::LOG_LEVEL_INFO, $category); } public function notice(?string $msg, string $category = 'notice') { - $this->console($msg, LoggerInterface::LOG_LEVEL_NOTICE, $category); + $this->log($msg, LoggerInterface::LOG_LEVEL_NOTICE, $category); } public function waring(?string $msg, string $category = 'waring') { - $this->console($msg, LoggerInterface::LOG_LEVEL_WARNING, $category); + $this->log($msg, LoggerInterface::LOG_LEVEL_WARNING, $category); } public function error(?string $msg, string $category = 'error') { - $this->console($msg, LoggerInterface::LOG_LEVEL_ERROR, $category); + $this->log($msg, LoggerInterface::LOG_LEVEL_ERROR, $category); } - public function onLog(): Event - { - return $this->callback; - } + } diff --git a/src/Resource/Config._php b/src/Resource/Config._php index a69fc3f..69fccc2 100644 --- a/src/Resource/Config._php +++ b/src/Resource/Config._php @@ -26,6 +26,7 @@ return [ 'level' => LoggerInterface::LOG_LEVEL_DEBUG, 'handler' => null, 'logConsole' => true, + 'displayConsole'=>true, 'ignoreCategory' => [] ], 'TEMP_DIR' => null