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

118 lines
2.0 KiB
PHP
Raw Normal View History

2020-08-31 01:27:08 +08:00
<?php
/**
* Created by PhpStorm.
* User: admin
* Date: 2019-03-22
* Time: 14:36
*/
2020-10-29 18:17:25 +08:00
declare(strict_types=1);
2020-08-31 01:27:08 +08:00
2021-08-11 01:04:57 +08:00
namespace Kiri\Error;
2020-08-31 01:27:08 +08:00
use Exception;
2021-08-11 01:04:57 +08:00
use Kiri\Abstracts\Component;
use Kiri\Core\Json;
use Kiri\Kiri;
2022-01-08 18:49:08 +08:00
use Kiri\Annotation\Inject;
2021-08-24 19:11:14 +08:00
use Psr\Log\LoggerInterface;
2020-10-30 01:25:38 +08:00
use Throwable;
2020-08-31 01:27:08 +08:00
/**
* Class Logger
2021-08-11 01:04:57 +08:00
* @package Kiri\Kiri\Error
2021-08-24 19:11:14 +08:00
* @mixin \Kiri\Abstracts\Logger
2020-08-31 01:27:08 +08:00
*/
2020-11-18 15:11:59 +08:00
class Logger extends Component
2020-08-31 01:27:08 +08:00
{
2021-07-12 11:24:33 +08:00
private array $logs = [];
2021-08-03 18:18:09 +08:00
/**
2021-08-24 19:11:14 +08:00
* inject logger
2021-08-03 18:18:09 +08:00
*
2021-08-24 19:11:14 +08:00
* @var LoggerInterface
2021-08-03 18:18:09 +08:00
*/
2021-08-24 19:11:14 +08:00
#[Inject(LoggerInterface::class)]
public LoggerInterface $logger;
2021-07-12 11:24:33 +08:00
2021-08-24 19:11:14 +08:00
private array $sources = [];
2021-07-12 11:24:33 +08:00
/**
* @param string $application
2021-08-24 19:11:14 +08:00
* @return string
2021-07-12 11:24:33 +08:00
*/
2021-08-24 19:11:14 +08:00
public function getLastError(string $application = 'app'): string
2021-07-12 11:24:33 +08:00
{
2022-01-07 14:38:36 +08:00
return $this->logs[$application] ?? 'Unknown error.';
2021-07-12 11:24:33 +08:00
}
2022-01-07 14:38:36 +08:00
/**
* @param $message
* @param $method
* @return void
*/
public function fail($message, $method)
{
$this->logs[$method] = $message;
}
2021-07-12 11:24:33 +08:00
/**
* @param string $messages
* @param string $method
* @throws Exception
*/
public function write(string $messages, string $method = 'app')
{
if (empty($messages)) {
return;
}
$to_day = date('Y-m-d');
2021-07-12 11:42:33 +08:00
$fileName = storage('server-' . $to_day . '.log', $dirName = 'log/' . ($method ?? 'app'));
2021-08-02 10:30:13 +08:00
file_put_contents($fileName, '[' . date('Y-m-d H:i:s') . ']:' . PHP_EOL . $messages . PHP_EOL);
2021-07-12 11:24:33 +08:00
}
/**
* @param Throwable $exception
* @return mixed
* @throws Exception
*/
public function exception(Throwable $exception): mixed
{
$code = $exception->getCode() == 0 ? 500 : $exception->getCode();
2021-08-11 01:04:57 +08:00
$logger = Kiri::app()->getLogger();
2021-07-12 11:24:33 +08:00
$logger->write(jTraceEx($exception), 'exception');
2021-07-23 10:38:03 +08:00
return Json::to($code, $exception->getMessage(), [
2021-07-12 11:24:33 +08:00
'file' => $exception->getFile(),
'line' => $exception->getLine()
]);
}
2021-08-24 19:12:59 +08:00
/**
* @param string $name
* @param array $arguments
* @return mixed
*/
public function __call(string $name, array $arguments): mixed
{
if (!method_exists($this, $name)) {
return $this->logger->{$name}(...$arguments);
} else {
return $this->{$name}(...$arguments);
}
}
2020-08-31 01:27:08 +08:00
}