Files
logview/app/common/Service/Logs.php

65 lines
1.6 KiB
PHP
Raw Normal View History

2023-02-14 17:27:01 -03:00
<?php
namespace ProVM\Common\Service;
2023-02-14 22:50:35 -03:00
use DateTimeImmutable;
use SplFileInfo;
2023-02-14 23:18:30 -03:00
use Psr\Log\LoggerInterface;
2023-02-14 17:27:01 -03:00
use ProVM\Logview\Log\File;
class Logs
{
2023-02-14 23:18:30 -03:00
public function __construct(LoggerInterface $logger, string $folder)
2023-02-14 17:27:01 -03:00
{
$this
2023-02-14 23:18:30 -03:00
->setLogger($logger)
2023-02-14 17:27:01 -03:00
->setFolder($folder);
}
2023-02-14 23:18:30 -03:00
protected LoggerInterface $logger;
2023-02-14 17:27:01 -03:00
protected string $folder;
2023-02-14 23:18:30 -03:00
public function getLogger(): LoggerInterface
{
return $this->logger;
}
2023-02-14 17:27:01 -03:00
public function getFolder(): string
{
return $this->folder;
}
2023-02-14 23:18:30 -03:00
public function setLogger(LoggerInterface $logger): Logs
{
$this->logger = $logger;
return $this;
}
2023-02-14 17:27:01 -03:00
public function setFolder(string $folder): Logs
{
$this->folder = $folder;
return $this;
}
public function getFiles(): array
{
$files = new \FilesystemIterator($this->getFolder());
$output = [];
foreach ($files as $file) {
if ($file->isDir()) {
continue;
}
$output []= $file;
}
return $output;
}
public function get(string $log_file): File
{
2023-02-14 22:50:35 -03:00
$filename = implode(DIRECTORY_SEPARATOR, [$this->getFolder(), $log_file]);
$file_info = new SplFileInfo($filename);
$content = \Safe\file_get_contents($filename);
2023-02-14 23:18:30 -03:00
return (new File())
->setLogger($this->getLogger())
->setFilename($log_file)
->setDate((new DateTimeImmutable())->setTimestamp($file_info->getCTime()))
->setContent($content);
2023-02-14 17:27:01 -03:00
}
}