Skip to content

Instantly share code, notes, and snippets.

@JavierCane
Created February 20, 2019 19:09
Show Gist options
  • Save JavierCane/8b3cb966cd65d8e49272771460107087 to your computer and use it in GitHub Desktop.
Save JavierCane/8b3cb966cd65d8e49272771460107087 to your computer and use it in GitHub Desktop.
<?php
namespace App\Command;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use App\Domain\LogEntry;
use App\Application\LogsSummarizer;
final class CountLogTypeOnLevelsCommand extends Command
{
private CONST LOGS_PATH = "var/log/";
private CONST LOGS_EXTENSION = ".log";
private $logEntries = [];
protected static $defaultName = 'count-log-type-on-levels';
protected function configure(): void
{
$this
->setDescription('Print (not dump) a log summary. That is, the number of log entries aggregated by their level for the selected levels on all the log files')
->addArgument('levels', InputArgument::IS_ARRAY, 'Log levels');
}
private function isLogFile(): callable
{
return function(string $fileName): bool {
return substr($fileName, strlen($fileName) - 4, strlen($fileName) - 1) === self::LOGS_EXTENSION;
};
}
protected function execute(InputInterface $input, OutputInterface $output): void
{
$levelsFilter = array_map('strtoupper', $input->getArgument('levels'));
$files = array_filter(scandir(self::LOGS_PATH), $this->isLogFile());
$logsSummarizer = new LogsSummarizer();
foreach ($files as $logFileNameToRead) {
$lines = file(self::LOGS_PATH . $logFileNameToRead);
foreach ($lines as $line) {
$this->logEntries[] = LogEntry::fromJsonString($line);;
}
}
$logSummary = $logsSummarizer($this->logEntries, $levelsFilter);
foreach ($logSummary as $logLevel => $logLevelOccurrences) {
$output->writeln("Hay " . $logLevelOccurrences . " logs del tipo " . $logLevel);
}
}
}
<?php
namespace App\Application;
use App\Domain\LogsSummary;
final class LogsSummarizer
{
private static $holdingCantLeveLog = [];
private CONST ALL_LEVELS = ["WARNING", "INFO", "DEBUG", "ERROR"];
public function withAllLevel():
{
}
public function __invoke(array $logEntries, array $logLevelsFilter): LogsSummary
{
$allLogEntriesSummary = LogsSummary::from(...$logEntries);
return $allLogEntriesSummary->filterBy($logLevelsFilter);
}
}
<?php
declare(strict_types = 1);
namespace App\Domain;
final class LogsSummary
{
private $logEntries = [];
public static function from(LogEntry ...$logEntries): LogsSummary
{
$summary = new self();
array_map($summary->logEntryAdder(), $logEntries);
return $summary;
}
public function logEntryAdder(): callable
{
return function(LogEntry $logEntry): void {
$this->addLogEntry($logEntry);
};
}
public function addLogEntry(LogEntry $logEntry): void
{
$this->logEntries[$logEntry->level()][] = $logEntry;
}
public function errorLevelOccurrences(): int
{
return count($this->logEntries[self::ERROR_LEVEL]);
}
public function filterBy(array $logLevelsFilter): LogsSummary
{
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment