This commit is contained in:
xl
2023-07-06 13:33:50 +08:00
parent c22d7a36a0
commit 95915678c3
2 changed files with 169 additions and 170 deletions
+169 -169
View File
@@ -28,156 +28,156 @@ use Symfony\Component\Console\Output\OutputInterface;
class Logger implements LoggerInterface class Logger implements LoggerInterface
{ {
const EMERGENCY = 'emergency'; const EMERGENCY = 'emergency';
const ALERT = 'alert'; const ALERT = 'alert';
const CRITICAL = 'critical'; const CRITICAL = 'critical';
const ERROR = 'error'; const ERROR = 'error';
const WARNING = 'warning'; const WARNING = 'warning';
const NOTICE = 'notice'; const NOTICE = 'notice';
const INFO = 'info'; const INFO = 'info';
const DEBUG = 'debug'; const DEBUG = 'debug';
const LOGGER_LEVELS = [Logger::EMERGENCY, Logger::ALERT, Logger::CRITICAL, Logger::ERROR, Logger::WARNING, Logger::NOTICE, Logger::INFO, Logger::DEBUG]; const LOGGER_LEVELS = [Logger::EMERGENCY, Logger::ALERT, Logger::CRITICAL, Logger::ERROR, Logger::WARNING, Logger::NOTICE, Logger::INFO, Logger::DEBUG];
private array $levels = []; private array $levels = [];
/** /**
* *
*/ */
public function __construct() public function __construct()
{ {
$this->levels = \config('log.level', Logger::LOGGER_LEVELS); $this->levels = \config('log.level', Logger::LOGGER_LEVELS);
} }
/** /**
* @param string $message * @param string $message
* @param array $context * @param array $context
* *
* 紧急情况 * 紧急情况
*/ */
public function emergency($message, array $context = []) public function emergency($message, array $context = [])
{ {
// TODO: Implement emergency() method. // TODO: Implement emergency() method.
$this->log(Logger::EMERGENCY, $message, $context); $this->log(Logger::EMERGENCY, $message, $context);
} }
/** /**
* @param string $message * @param string $message
* @param array $context * @param array $context
* *
* 应该警惕的 * 应该警惕的
*/ */
public function alert($message, array $context = []) public function alert($message, array $context = [])
{ {
// TODO: Implement alert() method. // TODO: Implement alert() method.
$this->log(Logger::ALERT, $message, $context); $this->log(Logger::ALERT, $message, $context);
} }
/** /**
* @param string $name * @param string $name
* @param array $arguments * @param array $arguments
* @return void * @return void
* @throws ReflectionException * @throws ReflectionException
*/ */
public static function __callStatic(string $name, array $arguments) public static function __callStatic(string $name, array $arguments)
{ {
// TODO: Implement __callStatic() method. // TODO: Implement __callStatic() method.
$name = str_replace('_', '', $name); $name = str_replace('_', '', $name);
Kiri::getLogger()->{$name}(...$arguments); Kiri::getLogger()->{$name}(...$arguments);
} }
/** /**
* @param string $message * @param string $message
* @param array $context * @param array $context
* *
* 关键性的日志 * 关键性的日志
*/ */
public function critical($message, array $context = []) public function critical($message, array $context = [])
{ {
// TODO: Implement critical() method. // TODO: Implement critical() method.
$this->log(Logger::CRITICAL, $message, $context); $this->log(Logger::CRITICAL, $message, $context);
} }
/** /**
* @param string $message * @param string $message
* @param array $context * @param array $context
*/ */
public function error($message, array $context = []) public function error($message, array $context = [])
{ {
// TODO: Implement error() method. // TODO: Implement error() method.
$this->log(Logger::ERROR, $message, $context); $this->log(Logger::ERROR, $message, $context);
} }
/** /**
* @param string $message * @param string $message
* @param array $context * @param array $context
*/ */
public function warning($message, array $context = []) public function warning($message, array $context = [])
{ {
// TODO: Implement warning() method. // TODO: Implement warning() method.
$this->log(Logger::WARNING, $message, $context); $this->log(Logger::WARNING, $message, $context);
} }
/** /**
* @param string $message * @param string $message
* @param array $context * @param array $context
*/ */
public function notice($message, array $context = []) public function notice($message, array $context = [])
{ {
// TODO: Implement notice() method. // TODO: Implement notice() method.
$this->log(Logger::NOTICE, $message, $context); $this->log(Logger::NOTICE, $message, $context);
} }
/** /**
* @param string $message * @param string $message
* @param array $context * @param array $context
*/ */
public function info($message, array $context = []) public function info($message, array $context = [])
{ {
// TODO: Implement info() method. // TODO: Implement info() method.
$this->log(Logger::INFO, $message, $context); $this->log(Logger::INFO, $message, $context);
} }
/** /**
* @param string $message * @param string $message
* @param array $context * @param array $context
*/ */
public function debug($message, array $context = []) public function debug($message, array $context = [])
{ {
// TODO: Implement debug() method. // TODO: Implement debug() method.
$this->log(Logger::DEBUG, $message, $context); $this->log(Logger::DEBUG, $message, $context);
} }
/** /**
* @param mixed $level * @param mixed $level
* @param string $message * @param string $message
* @param array $context * @param array $context
* @throws * @throws
*/ */
public function log($level, $message, array $context = []): void public function log($level, $message, array $context = []): void
{ {
if (!in_array($level, $this->levels)) { if (!in_array($level, $this->levels)) {
return; return;
} }
$_string = "[" . now() . ']' . ucfirst($level) . ": " . $message; $_string = "[" . now() . ']: ' . $message;
if (!empty($context)) { if (!empty($context)) {
$_string .= PHP_EOL . $this->_string($context); $_string .= PHP_EOL . $this->_string($context);
} }
if (str_contains($_string, 'Event::rshutdown')) { if (str_contains($_string, 'Event::rshutdown')) {
return; return;
} }
$container = Kiri::getDi(); $container = Kiri::getDi();
if ($container->has(OutputInterface::class)) { if ($container->has(OutputInterface::class)) {
@@ -187,58 +187,58 @@ class Logger implements LoggerInterface
file_put_contents('php://output', $message . PHP_EOL); file_put_contents('php://output', $message . PHP_EOL);
} }
$filename = storage('log-' . date('Y-m-d') . '.log', 'log/'); $filename = storage('/log-' . date('Y-m-d') . '.log', 'log/' . $level . '/');
file_put_contents($filename, $_string, FILE_APPEND); file_put_contents($filename, $_string, FILE_APPEND);
} }
/** /**
* @return void * @return void
* @throws Exception * @throws Exception
*/ */
public function flush(): void public function flush(): void
{ {
$this->removeFile(storage()); $this->removeFile(storage());
} }
/** /**
* @param string $dirname * @param string $dirname
* @return void * @return void
*/ */
private function removeFile(string $dirname): void private function removeFile(string $dirname): void
{ {
$paths = new DirectoryIterator($dirname); $paths = new DirectoryIterator($dirname);
/** @var DirectoryIterator $path */ /** @var DirectoryIterator $path */
foreach ($paths as $path) { foreach ($paths as $path) {
if ($path->isDot() || str_starts_with($path->getFilename(), '.')) { if ($path->isDot() || str_starts_with($path->getFilename(), '.')) {
continue; continue;
} }
if ($path->isDir()) { if ($path->isDir()) {
$directory = rtrim($path->getRealPath(), '/'); $directory = rtrim($path->getRealPath(), '/');
$this->removeFile($directory); $this->removeFile($directory);
} }
@unlink($path->getRealPath()); @unlink($path->getRealPath());
} }
} }
/** /**
* @param $context * @param $context
* @return string * @return string
*/ */
private function _string($context): string private function _string($context): string
{ {
if ($context instanceof \Throwable) { if ($context instanceof \Throwable) {
$context = 'file -> ' . $context->getFile() . PHP_EOL . 'line -> ' . $context->getLine() . PHP_EOL; $context = 'file -> ' . $context->getFile() . PHP_EOL . 'line -> ' . $context->getLine() . PHP_EOL;
} }
if (is_array($context) && isset($context[0]) && $context[0] instanceof \Throwable) { if (is_array($context) && isset($context[0]) && $context[0] instanceof \Throwable) {
$context = 'file -> ' . $context[0]->getFile() . PHP_EOL . 'line -> ' . $context[0]->getLine() . PHP_EOL; $context = 'file -> ' . $context[0]->getFile() . PHP_EOL . 'line -> ' . $context[0]->getLine() . PHP_EOL;
} }
if (is_string($context)) { if (is_string($context)) {
return $context . PHP_EOL; return $context . PHP_EOL;
} }
return implode(PHP_EOL, $context); return implode(PHP_EOL, $context);
} }
} }
-1
View File
@@ -4,7 +4,6 @@
namespace Kiri\Pool; namespace Kiri\Pool;
use Database\Mysql\PDO;
use Exception; use Exception;
use Kiri\Abstracts\Component; use Kiri\Abstracts\Component;
use Kiri\Exception\ConfigException; use Kiri\Exception\ConfigException;