Revert "改名"
This reverts commit fdf58326
This commit is contained in:
+174
-175
@@ -3,11 +3,10 @@
|
|||||||
namespace Kiri\Abstracts;
|
namespace Kiri\Abstracts;
|
||||||
|
|
||||||
use DirectoryIterator;
|
use DirectoryIterator;
|
||||||
use Kiri\Kiri;
|
|
||||||
use Note\Inject;
|
|
||||||
use Exception;
|
use Exception;
|
||||||
use Kiri\Events\EventProvider;
|
use Kiri\Events\EventProvider;
|
||||||
use Kiri\Exception\ConfigException;
|
use Kiri\Exception\ConfigException;
|
||||||
|
use Kiri\Kiri;
|
||||||
use Psr\Log\LoggerInterface;
|
use Psr\Log\LoggerInterface;
|
||||||
use ReflectionException;
|
use ReflectionException;
|
||||||
use Server\Events\OnWorkerStop;
|
use Server\Events\OnWorkerStop;
|
||||||
@@ -19,212 +18,212 @@ use Server\Events\OnWorkerStop;
|
|||||||
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';
|
||||||
|
|
||||||
|
|
||||||
private array $_loggers = [];
|
private array $_loggers = [];
|
||||||
|
|
||||||
|
|
||||||
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];
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return void
|
* @return void
|
||||||
* @throws ReflectionException
|
* @throws ReflectionException
|
||||||
*/
|
*/
|
||||||
public function init()
|
public function init()
|
||||||
{
|
{
|
||||||
Kiri::getDi()->get(EventProvider::class)->on(OnWorkerStop::class, [$this, 'onAfterRequest']);
|
Kiri::getDi()->get(EventProvider::class)->on(OnWorkerStop::class, [$this, 'onAfterRequest']);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param string $message
|
* @param string $message
|
||||||
* @param array $context
|
* @param array $context
|
||||||
* @throws ConfigException
|
* @throws ConfigException
|
||||||
*
|
*
|
||||||
* 紧急情况
|
* 紧急情况
|
||||||
*/
|
*/
|
||||||
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
|
||||||
* @throws ConfigException
|
* @throws ConfigException
|
||||||
*
|
*
|
||||||
* 应该警惕的
|
* 应该警惕的
|
||||||
*/
|
*/
|
||||||
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 $message
|
* @param string $message
|
||||||
* @param array $context
|
* @param array $context
|
||||||
* @throws ConfigException
|
* @throws ConfigException
|
||||||
*
|
*
|
||||||
* 关键性的日志
|
* 关键性的日志
|
||||||
*/
|
*/
|
||||||
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
|
||||||
* @throws ConfigException
|
* @throws ConfigException
|
||||||
*/
|
*/
|
||||||
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
|
||||||
* @throws ConfigException
|
* @throws ConfigException
|
||||||
*/
|
*/
|
||||||
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
|
||||||
* @throws ConfigException
|
* @throws ConfigException
|
||||||
*/
|
*/
|
||||||
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
|
||||||
* @throws ConfigException
|
* @throws ConfigException
|
||||||
*/
|
*/
|
||||||
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
|
||||||
* @throws ConfigException
|
* @throws ConfigException
|
||||||
*/
|
*/
|
||||||
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 ConfigException
|
* @throws ConfigException
|
||||||
*/
|
*/
|
||||||
public function log($level, $message, array $context = [])
|
public function log($level, $message, array $context = [])
|
||||||
{
|
{
|
||||||
// TODO: Implement log() method.
|
// TODO: Implement log() method.
|
||||||
$levels = Config::get('log.level', Logger::LOGGER_LEVELS);
|
$levels = Config::get('log.level', Logger::LOGGER_LEVELS);
|
||||||
if (!in_array($level, $levels)) {
|
if (!in_array($level, $levels) || str_contains($message, 'Event::rshutdown')) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
$_string = '[' . now() . '] production.' . $level . ': ' . $this->_string($message, $context);
|
$_string = '[' . now() . '] production.' . $level . ': ' . $this->_string($message, $context);
|
||||||
|
|
||||||
file_put_contents('php://output', $_string);
|
file_put_contents('php://output', $_string);
|
||||||
|
|
||||||
$this->_loggers[] = $_string;
|
$this->_loggers[] = $_string;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param OnWorkerStop $param
|
* @param OnWorkerStop $param
|
||||||
* @throws Exception
|
* @throws Exception
|
||||||
*/
|
*/
|
||||||
public function onAfterRequest(OnWorkerStop $param)
|
public function onAfterRequest(OnWorkerStop $param)
|
||||||
{
|
{
|
||||||
$loggers = implode(PHP_EOL, $this->_loggers);
|
$loggers = implode(PHP_EOL, $this->_loggers);
|
||||||
$this->_loggers = [];
|
$this->_loggers = [];
|
||||||
if (!empty($loggers)) {
|
if (!empty($loggers)) {
|
||||||
$filename = storage('log-' . date('Y-m-d') . '.log', 'log/');
|
$filename = storage('log-' . date('Y-m-d') . '.log', 'log/');
|
||||||
|
|
||||||
file_put_contents($filename, $loggers);
|
file_put_contents($filename, $loggers);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return void
|
* @return void
|
||||||
* @throws Exception
|
* @throws Exception
|
||||||
*/
|
*/
|
||||||
public function flush()
|
public function flush()
|
||||||
{
|
{
|
||||||
$this->removeFile(storage());
|
$this->removeFile(storage());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param string $dirname
|
* @param string $dirname
|
||||||
* @return void
|
* @return void
|
||||||
*/
|
*/
|
||||||
private function removeFile(string $dirname)
|
private function removeFile(string $dirname)
|
||||||
{
|
{
|
||||||
$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 $message
|
* @param $message
|
||||||
* @param $context
|
* @param $context
|
||||||
* @return string
|
* @return string
|
||||||
*/
|
*/
|
||||||
private function _string($message, $context): string
|
private function _string($message, $context): string
|
||||||
{
|
{
|
||||||
if (!empty($context)) {
|
if (!empty($context)) {
|
||||||
return $message . ' ' . PHP_EOL . print_r($context, TRUE) . PHP_EOL;
|
return $message . ' ' . PHP_EOL . print_r($context, TRUE) . PHP_EOL;
|
||||||
}
|
}
|
||||||
return $message . PHP_EOL;
|
return $message . PHP_EOL;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user