67 lines
1.8 KiB
PHP
67 lines
1.8 KiB
PHP
<?php
|
|
|
|
namespace App\Services;
|
|
|
|
use App\Entities\MyLogEntity as Entity;
|
|
use App\Models\MyLogModel as Model;
|
|
use App\Services\Auth\AuthService;
|
|
use CodeIgniter\HTTP\IncomingRequest;
|
|
|
|
class MyLogService extends CommonService
|
|
{
|
|
private static $_logBuffers = [];
|
|
public function __construct(IncomingRequest $request)
|
|
{
|
|
parent::__construct($request);
|
|
}
|
|
final public function getClassName(): string
|
|
{
|
|
return "MyLog";
|
|
}
|
|
final public function getClassPath(): string
|
|
{
|
|
return $this->getClassName();
|
|
}
|
|
public function getModelClass(): string
|
|
{
|
|
return Model::class;
|
|
}
|
|
public function getEntityClass(): string
|
|
{
|
|
return Entity::class;
|
|
}
|
|
static public function log(string $message, string $level = "info"): void
|
|
{
|
|
self::$_logBuffers[$level] = sprintf("%s[%s]: %s", date("H:i:s"), $level, $message);
|
|
log_message($level, $message);
|
|
}
|
|
public function info(string $message): void
|
|
{
|
|
self::log($message, 'info');
|
|
}
|
|
public function error(string $message): void
|
|
{
|
|
self::log($message, 'error');
|
|
}
|
|
public function warning(string $message): void
|
|
{
|
|
self::log($message, 'warning');
|
|
}
|
|
public function debug(string $message): void
|
|
{
|
|
self::log($message, 'debug');
|
|
}
|
|
public function save($service, string $method, AuthService $myauth, string $title): Entity
|
|
{
|
|
$formDatas = [
|
|
'user_uid' => $myauth->getUIDByAuthInfo(),
|
|
'class_name' => $service->getClassName(),
|
|
'method_name' => $method,
|
|
'title' => $title,
|
|
'content' => implode("\n", self::$_logBuffers),
|
|
];
|
|
self::$_logBuffers = [];
|
|
return $this->getModel()->create($formDatas, new Entity());
|
|
}
|
|
}
|