for testing and deploying your application
for finding and fixing issues
for empowering human code reviews
<?php
namespace PiedWeb\LogsAnalyzer;
class LogsAnalyzer
{
protected $parser;
protected $filter;
public $logs;
public function __construct(string $type, ?string $domain = null)
$this->parser = new LogParser();
$this->parser->setFormat(class_exists($type) ? $type::FORMAT : $type);
if ($domain !== null) {
$this->parser->setDomain($domain);
}
$this->parser->setType(class_exists($type) ? $type : LogLine::class);
/**
* The function must return TRUE for line we want to keep / FALSE for line to ignore
*/
public function setFilter($filter)
$this->filter = $filter;
protected function checkFile(string $filename): bool
return !file_exists($filename) || !is_readable($filename);
protected function filter($line)
if ($this->filter) {
return call_user_func($this->filter, $line);
return true;
public function parse(string $filename)
if ($this->checkFile($filename)) {
throw new \Exception('A problem occured with file `'.$filename.'`');
$handle = fopen(('.gz' == substr($filename, -3) ? 'compress.zlib://' : '').$filename, 'r');
if (!$handle) {
$handle
false|resource
false
return [];
$logs = [];
$lineCounter = 1;
while (!feof($handle)) {
$line = fgets($handle);
$line = $line ? $this->parser->parse($line) : null;
if ($line && $this->filter($line)) {
$logs[$lineCounter] = $line;
++$lineCounter;
fclose($handle);
return $logs;