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
|
|
|
|
2021-08-03 18:18:09 +08:00
|
|
|
use Annotation\Inject;
|
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\Events\EventProvider;
|
|
|
|
|
use Kiri\Kiri;
|
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
|
|
|
/** @var EventProvider */
|
|
|
|
|
#[Inject(EventProvider::class)]
|
|
|
|
|
public EventProvider $eventProvider;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
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
|
|
|
{
|
2021-08-24 19:11:14 +08:00
|
|
|
return 'Unknown error.';
|
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
|
|
|
|
|
|
|
|
}
|