Skip to content

Commit

Permalink
Merge 9b42f21 into 368ea83
Browse files Browse the repository at this point in the history
  • Loading branch information
kristuff committed Sep 27, 2020
2 parents 368ea83 + 9b42f21 commit 8d7279e
Show file tree
Hide file tree
Showing 44 changed files with 1,089 additions and 324 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#v 0.1.1
#v 0.2.0
language: php

php:
Expand Down
323 changes: 322 additions & 1 deletion README.md

Large diffs are not rendered by default.

54 changes: 0 additions & 54 deletions src/ApacheAccessLogParser.php

This file was deleted.

58 changes: 0 additions & 58 deletions src/ApacheErrorLogParser.php

This file was deleted.

2 changes: 1 addition & 1 deletion src/Core/LogEntry.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* @version 0.1.1
* @version 0.2.0
* @copyright 2017-2020 Kristuff
*/

Expand Down
2 changes: 1 addition & 1 deletion src/Core/LogEntryFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* @version 0.1.1
* @version 0.2.0
* @copyright 2017-2020 Kristuff
*/

Expand Down
2 changes: 1 addition & 1 deletion src/Core/LogEntryFactoryInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* @version 0.1.1
* @version 0.2.0
* @copyright 2017-2020 Kristuff
*/

Expand Down
2 changes: 1 addition & 1 deletion src/Core/LogEntryInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* @version 0.1.1
* @version 0.2.0
* @copyright 2017-2020 Kristuff
*/

Expand Down
43 changes: 0 additions & 43 deletions src/Fail2BanLogParser.php

This file was deleted.

2 changes: 1 addition & 1 deletion src/FormatException.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* @version 0.1.1
* @version 0.2.0
* @copyright 2017-2020 Kristuff
*/

Expand Down
40 changes: 17 additions & 23 deletions src/LogParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* @version 0.1.1
* @version 0.2.0
* @copyright 2017-2020 Kristuff
*/

Expand All @@ -28,30 +28,29 @@
class LogParser
{
/**
* @access private
* @var string
*/
private $pcreFormat;

/**
* @access private
* @var string
*/
private $logFormat = '';

/**
* @var array
*/
public $patterns = [];

/**
* @var string
*/
public $defaultFormat = '';

/**
* @var LogEntryFactoryInterface
* @access private
* @var LogEntryFactoryInterface
*/
private $factory;

/**
* @access protected
* @var array
*/
protected $patterns = [];

/**
* Constructor
*
Expand All @@ -63,9 +62,7 @@ class LogParser
*/
public function __construct(string $format = null, LogEntryFactoryInterface $factory = null)
{
$this->logFormat = $format ?? $this->defaultFormat;
$this->updateIpPatterns();
$this->setFormat($this->logFormat);
$this->setFormat($format ?? '');
$this->factory = $factory ?: new LogEntryFactory();
}

Expand All @@ -81,7 +78,6 @@ public function __construct(string $format = null, LogEntryFactoryInterface $fac
public function addPattern(string $placeholder, string $pattern): void
{
$this->patterns[$placeholder] = $pattern;
$this->updateIpPatterns();
}

/**
Expand All @@ -106,6 +102,10 @@ public function getFormat(): string
*/
public function setFormat(string $format): void
{
// store log format update IP pattern
$this->logFormat = $format ;
$this->updateIpPatterns();

// strtr won't work for "complex" header patterns
// $this->pcreFormat = strtr("#^{$format}$#", $this->patterns);
$expr = "#^{$format}$#";
Expand All @@ -131,14 +131,8 @@ public function parse(string $line): LogEntryInterface
if (!preg_match($this->getPCRE(), $line, $matches)) {
throw new FormatException($line);
}

return $this->factory->create($matches);
// $parsedLine = [];
// foreach (array_filter(array_keys($matches), 'is_string') as $key) {
// $parsedLine[$key] = trim($matches[$key]);
// }
//
// return $parsedLine;
// return array_filter(array_keys($data), 'is_string') as $key)
}

/**
Expand Down
65 changes: 65 additions & 0 deletions src/LogParserFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
<?php declare(strict_types=1);

/**
* ___ _
* | _ \ __ _ _ _ ___ ___ | | ___ __ _
* | _// _` || '_|(_-</ -_)| |/ _ \/ _` |
* |_| \__,_||_| /__/\___||_|\___/\__, |
* |___/
*
* (c) Kristuff <contact@kristuff.fr>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* @version 0.2.0
* @copyright 2017-2020 Kristuff
*/

namespace Kristuff\Parselog;

use Kristuff\Parselog\Core\LogEntryFactoryInterface;
use Kristuff\Parselog\Software\SoftwareLogParser;

/**
*
*/
class LogParserFactory
{
const TYPE_APACHE_ACCESS = 'apache_access';
const TYPE_APACHE_ERROR = 'apache_error';
const TYPE_SYSLOG = 'syslog';
const TYPE_FAIL2BAN = 'fail2ban';

/**
* Gets a new LogParser instance based on given logType
*
* @access public
* @static
* @param string $logType The internal log type
* @param string $format The log format. Default is null
* @param LogEntryFactoryInterface $factory The custom log entry factory. Default is null (use default factory)
*
* @return \Kristuff\Parselog\LogParser|null
*/
public static function getParser(string $logType, string $format = null, LogEntryFactoryInterface $factory = null): ?SoftwareLogParser
{
switch ($logType){

case self::TYPE_APACHE_ACCESS:
return new \Kristuff\Parselog\Software\ApacheAccessLogParser($format, $factory);

case self::TYPE_APACHE_ERROR:
return new \Kristuff\Parselog\Software\ApacheErrorLogParser($format, $factory);

case self::TYPE_SYSLOG:
return new \Kristuff\Parselog\Software\SyslogParser($format, $factory);

case self::TYPE_FAIL2BAN:
return new \Kristuff\Parselog\Software\Fail2BanLogParser($format, $factory);

}
return null;
}

}

0 comments on commit 8d7279e

Please sign in to comment.