Files
kiri-core/kiri-engine/Abstracts/Logger.php
T

236 lines
5.5 KiB
PHP
Raw Normal View History

2022-01-09 03:50:38 +08:00
<?php
namespace Kiri\Abstracts;
use DirectoryIterator;
use Exception;
use Kiri\Events\EventProvider;
use Kiri\Exception\ConfigException;
2022-01-12 14:10:33 +08:00
use Kiri;
2022-01-11 15:53:31 +08:00
use Kiri\Server\Events\OnWorkerStop;
2022-01-09 03:50:38 +08:00
use Psr\Log\LoggerInterface;
use ReflectionException;
2023-04-19 00:03:18 +08:00
use Symfony\Component\Console\Output\OutputInterface;
2022-01-09 03:50:38 +08:00
/**
2023-04-19 15:35:57 +08:00
* @method static _emergency(string $message, array $context = [])
* @method static _alert(string $message, array $context = [])
* @method static _critical(string $message, array $context = [])
* @method static _error(string $message, array $context = [])
2023-04-22 02:28:26 +08:00
* @method static _warning(string $message, array $context = [])
2023-04-19 15:35:57 +08:00
* @method static _notice(string $message, array $context = [])
* @method static _info(string $message, array $context = [])
* @method static _debug(string $message, array $context = [])
* @method static _log(mixed $level, string $message, array $context = [])
* @method static _flush()
2022-01-09 03:50:38 +08:00
*/
class Logger implements LoggerInterface
{
2023-04-18 23:47:31 +08:00
2022-01-09 03:50:38 +08:00
const EMERGENCY = 'emergency';
const ALERT = 'alert';
const CRITICAL = 'critical';
const ERROR = 'error';
const WARNING = 'warning';
const NOTICE = 'notice';
const INFO = 'info';
const DEBUG = 'debug';
2023-04-18 23:47:31 +08:00
2022-01-09 03:50:38 +08:00
const LOGGER_LEVELS = [Logger::EMERGENCY, Logger::ALERT, Logger::CRITICAL, Logger::ERROR, Logger::WARNING, Logger::NOTICE, Logger::INFO, Logger::DEBUG];
2023-04-18 23:47:31 +08:00
2023-04-19 10:32:55 +08:00
private array $levels = [];
public function __construct()
{
$this->levels = Config::get('log.level', Logger::LOGGER_LEVELS);
}
2022-01-09 03:50:38 +08:00
/**
* @param string $message
* @param array $context
*
* 紧急情况
*/
public function emergency($message, array $context = [])
{
// TODO: Implement emergency() method.
$this->log(Logger::EMERGENCY, $message, $context);
}
2023-04-18 23:47:31 +08:00
2022-01-09 03:50:38 +08:00
/**
* @param string $message
* @param array $context
*
* 应该警惕的
*/
public function alert($message, array $context = [])
{
// TODO: Implement alert() method.
$this->log(Logger::ALERT, $message, $context);
}
2023-04-18 23:47:31 +08:00
2023-04-19 13:15:21 +08:00
/**
* @param string $name
* @param array $arguments
* @return void
* @throws ReflectionException
*/
public static function __callStatic(string $name, array $arguments)
{
// TODO: Implement __callStatic() method.
2023-04-19 15:38:08 +08:00
$name = str_replace('_', '', $name);
Kiri::getLogger()->{$name}(...$arguments);
2023-04-19 13:15:21 +08:00
}
2022-01-09 03:50:38 +08:00
/**
* @param string $message
* @param array $context
*
* 关键性的日志
*/
public function critical($message, array $context = [])
{
// TODO: Implement critical() method.
$this->log(Logger::CRITICAL, $message, $context);
}
2023-04-18 23:47:31 +08:00
2022-01-09 03:50:38 +08:00
/**
* @param string $message
* @param array $context
*/
public function error($message, array $context = [])
{
// TODO: Implement error() method.
$this->log(Logger::ERROR, $message, $context);
}
2023-04-18 23:47:31 +08:00
2022-01-09 03:50:38 +08:00
/**
* @param string $message
* @param array $context
*/
public function warning($message, array $context = [])
{
// TODO: Implement warning() method.
$this->log(Logger::WARNING, $message, $context);
}
2023-04-18 23:47:31 +08:00
2022-01-09 03:50:38 +08:00
/**
* @param string $message
* @param array $context
*/
public function notice($message, array $context = [])
{
// TODO: Implement notice() method.
$this->log(Logger::NOTICE, $message, $context);
}
2023-04-18 23:47:31 +08:00
2022-01-09 03:50:38 +08:00
/**
* @param string $message
* @param array $context
*/
public function info($message, array $context = [])
{
// TODO: Implement info() method.
$this->log(Logger::INFO, $message, $context);
}
2023-04-18 23:47:31 +08:00
2022-01-09 03:50:38 +08:00
/**
* @param string $message
* @param array $context
*/
public function debug($message, array $context = [])
{
// TODO: Implement debug() method.
$this->log(Logger::DEBUG, $message, $context);
}
2023-04-18 23:47:31 +08:00
2022-01-09 03:50:38 +08:00
/**
* @param mixed $level
* @param string $message
* @param array $context
2023-04-17 10:19:00 +08:00
* @throws
2022-01-09 03:50:38 +08:00
*/
2023-04-19 10:32:55 +08:00
public function log($level, $message, array $context = []): void
2022-01-09 03:50:38 +08:00
{
2023-04-19 10:32:55 +08:00
if (!in_array($level, $this->levels)) {
2023-04-19 00:03:18 +08:00
return;
2022-01-09 03:50:38 +08:00
}
2023-04-19 12:41:41 +08:00
$_string = "[" . now() . ']' . ucfirst($level) . ": " . $message;
if (!empty($context)) {
$_string .= PHP_EOL . $this->_string($context);
}
2023-04-19 00:03:18 +08:00
if (str_contains($_string, 'Event::rshutdown')) {
return;
}
$console = Kiri::getDi()->get(OutputInterface::class);
$console->writeln($_string);
$filename = storage('log-' . date('Y-m-d') . '.log', 'log/');
file_put_contents($filename, $_string, FILE_APPEND);
2022-01-09 03:50:38 +08:00
}
2023-04-18 23:47:31 +08:00
2022-01-09 03:50:38 +08:00
/**
* @return void
* @throws Exception
*/
2022-06-16 17:38:23 +08:00
public function flush(): void
2022-01-09 03:50:38 +08:00
{
$this->removeFile(storage());
}
2023-04-18 23:47:31 +08:00
2022-01-09 03:50:38 +08:00
/**
* @param string $dirname
* @return void
*/
2022-06-16 17:38:23 +08:00
private function removeFile(string $dirname): void
2022-01-09 03:50:38 +08:00
{
$paths = new DirectoryIterator($dirname);
/** @var DirectoryIterator $path */
foreach ($paths as $path) {
if ($path->isDot() || str_starts_with($path->getFilename(), '.')) {
continue;
}
if ($path->isDir()) {
$directory = rtrim($path->getRealPath(), '/');
$this->removeFile($directory);
}
@unlink($path->getRealPath());
}
}
2023-04-18 23:47:31 +08:00
2022-01-09 03:50:38 +08:00
/**
* @param $context
* @return string
*/
2022-06-22 16:29:42 +08:00
private function _string($context): string
2022-01-09 03:50:38 +08:00
{
2022-06-16 17:38:23 +08:00
if ($context instanceof \Throwable) {
2023-04-18 23:47:31 +08:00
$context = 'file -> ' . $context->getFile() . PHP_EOL . 'line -> ' . $context->getLine() . PHP_EOL;
2022-06-16 17:38:23 +08:00
}
2022-06-22 19:05:08 +08:00
if (is_array($context) && isset($context[0]) && $context[0] instanceof \Throwable) {
2023-04-18 23:47:31 +08:00
$context = 'file -> ' . $context[0]->getFile() . PHP_EOL . 'line -> ' . $context[0]->getLine() . PHP_EOL;
}
if (is_string($context)) {
return $context . PHP_EOL;
2022-01-09 03:50:38 +08:00
}
2023-04-19 12:41:41 +08:00
return implode(PHP_EOL, $context);
2022-01-09 03:50:38 +08:00
}
}