diff --git a/.gitignore b/.gitignore index 4123962..e1a14bc 100644 --- a/.gitignore +++ b/.gitignore @@ -5,3 +5,4 @@ composer.phar .settings .project bin +/.idea/ diff --git a/README.md b/README.md index 145dcc1..1b59f44 100644 --- a/README.md +++ b/README.md @@ -23,9 +23,6 @@ You can install the library using [composer]('http://getcomposer.org/) by adding echo sprintf("The log entry was written at %s. \n", $log['date']->format('Y-m-d h:i:s')); } - $lastLine = $reader[count($reader)-1]; - echo sprintf("The last log entry was written at %s. \n", $lastLine['date']->format('Y-m-d h:i:s')); - ``` * options unlimited days logs @@ -42,9 +39,6 @@ You can install the library using [composer]('http://getcomposer.org/) by adding echo sprintf("The log entry was written at %s. \n", $log['date']->format('Y-m-d h:i:s')); } - $lastLine = $reader[count($reader)-1]; - echo sprintf("The last log entry was written at %s. \n", $lastLine['date']->format('Y-m-d h:i:s')); - ``` * options 2 days logs @@ -61,12 +55,10 @@ You can install the library using [composer]('http://getcomposer.org/) by adding echo sprintf("The log entry was written at %s. \n", $log['date']->format('Y-m-d h:i:s')); } - $lastLine = $reader[count($reader)-1]; - echo sprintf("The last log entry was written at %s. \n", $lastLine['date']->format('Y-m-d h:i:s')); - ``` * Add custom pattern + ```php require_once 'path/to/vendor/autoload.php'; @@ -84,9 +76,6 @@ You can install the library using [composer]('http://getcomposer.org/) by adding echo sprintf("The log entry was written at %s. \n", $log['date']->format('Y-m-d h:i:s')); } - $lastLine = $reader[count($reader)-1]; - echo sprintf("The last log entry was written at %s. \n", $lastLine['date']->format('Y-m-d h:i:s')); - ``` diff --git a/src/Dubture/Monolog/Parser/LineLogParser.php b/src/Dubture/Monolog/Parser/LineLogParser.php index 2f9bb76..4ea2873 100644 --- a/src/Dubture/Monolog/Parser/LineLogParser.php +++ b/src/Dubture/Monolog/Parser/LineLogParser.php @@ -18,11 +18,11 @@ class LineLogParser implements LogParserInterface { /** - * @var string + * @var array */ protected $pattern = array( - 'default' => '/\[(?P.*)\] (?P\w+).(?P\w+): (?P[^\[\{]+) (?P[\[\{].*[\]\}]) (?P[\[\{].*[\]\}])/', - 'error' => '/\[(?P.*)\] (?P\w+).(?P\w+): (?P(.*)+) (?P[^ ]+) (?P[^ ]+)/' + 'default' => '/\[(?P.*)\]\s(?P[\w-]+)\.(?P\w+):\s(?P[^\[\{]+)\s(?P[\[\{].*[\]\}])\s(?P[\[\{].*[\]\}])/', + 'error' => '/\[(?P.*)\]\s(?P[\w-]+).(?P\w+):\s(?P(.*)+)\s(?P[^\s]+)\s(?P[^\s]+)/' ); @@ -65,10 +65,10 @@ public function parse($log, $days = 1, $pattern = 'default') if ($date->diff($d2)->days < $days) { return $array; - } else { - return array(); } } + + return array(); } /** @@ -85,4 +85,36 @@ public function registerPattern($name, $pattern) throw new \RuntimeException("Pattern $name already exists"); } } + + /** + * @inheritdoc + */ + public function getPattern() + { + return $this->pattern; + } + + /** + * @inheritdoc + */ + public function setPattern($pattern) + { + $this->pattern = $pattern; + } + + /** + * @inheritdoc + */ + public function addPattern($name, $pattern) + { + $this->pattern[$name] = $pattern; + } + + /** + * @inheritdoc + */ + public function removePattern($name) + { + unset($this->pattern[$name]); + } } diff --git a/src/Dubture/Monolog/Parser/LogParserInterface.php b/src/Dubture/Monolog/Parser/LogParserInterface.php index df5689d..861d85b 100644 --- a/src/Dubture/Monolog/Parser/LogParserInterface.php +++ b/src/Dubture/Monolog/Parser/LogParserInterface.php @@ -25,4 +25,31 @@ interface LogParserInterface * @return mixed */ public function parse($log, $days, $pattern); + + /** + * @return array + */ + public function getPattern(); + + /** + * @param array $pattern + * + * @return void + */ + public function setPattern($pattern); + + /** + * @param string $name + * @param string $pattern + * + * @return void + */ + public function addPattern($name, $pattern); + + /** + * @param string $name + * + * @return void + */ + public function removePattern($name); } diff --git a/src/Dubture/Monolog/Reader/LogReader.php b/src/Dubture/Monolog/Reader/LogReader.php index 4b23fee..9bb6148 100644 --- a/src/Dubture/Monolog/Reader/LogReader.php +++ b/src/Dubture/Monolog/Reader/LogReader.php @@ -12,6 +12,7 @@ namespace Dubture\Monolog\Reader; use Dubture\Monolog\Reader\AbstractReader; +use Dubture\Monolog\Parser\LogParserInterface; /** * Class LogReader @@ -30,7 +31,7 @@ class LogReader extends AbstractReader implements \Iterator, \ArrayAccess, \Coun protected $lineCount; /** - * @var \Dubture\Monolog\Parser\LogParserInterface + * @var LogParserInterface */ protected $parser; @@ -46,11 +47,14 @@ class LogReader extends AbstractReader implements \Iterator, \ArrayAccess, \Coun public function __construct($file, $days = 1, $pattern = 'default') { $this->file = new \SplFileObject($file, 'r'); - $i = 0; + $i = 1; while (!$this->file->eof()) { $this->file->current(); $this->file->next(); - $i++; + + if (!empty($this->file->current())) { + $i++; + } } $this->days = $days; @@ -65,8 +69,12 @@ public function __construct($file, $days = 1, $pattern = 'default') */ public function getParser() { - $p = & $this->parser; - return $p; + return $this->parser; + } + + public function setParser(LogParserInterface $parser) + { + $this->parser = $parser; } /** @@ -152,7 +160,7 @@ public function key() */ public function valid() { - return $this->file->valid(); + return $this->file->valid() && !empty($this->file->current()); } /** diff --git a/tests/Dubture/Monolog/Reader/Test/LogReaderTest.php b/tests/Dubture/Monolog/Reader/Test/LogReaderTest.php index 4231c2f..c7ffc63 100644 --- a/tests/Dubture/Monolog/Reader/Test/LogReaderTest.php +++ b/tests/Dubture/Monolog/Reader/Test/LogReaderTest.php @@ -88,7 +88,7 @@ public function testReader() $log = $this->reader[6]; $this->assertInstanceOf('\DateTime', $log['date']); - $this->assertEquals('extra', $log['logger']); + $this->assertEquals('extra-dashed', $log['logger']); $this->assertEquals('INFO', $log['level']); $this->assertEquals('context and extra', $log['message']); $this->assertArrayHasKey('foo', $log['context'][0]); @@ -119,7 +119,7 @@ public function testIterator() $this->assertEquals('context', $lines[3]['logger']); $this->assertEquals('context', $lines[4]['logger']); $this->assertEquals('context', $lines[5]['logger']); - $this->assertEquals('extra', $lines[6]['logger']); + $this->assertEquals('extra-dashed', $lines[6]['logger']); } diff --git a/tests/files/test.log b/tests/files/test.log index 19bb837..404edbe 100755 --- a/tests/files/test.log +++ b/tests/files/test.log @@ -4,4 +4,4 @@ [2013-08-15 14:19:51] context.INFO: multicontext [{"foo":"bar","stuff":"and things"},{"bat":"baz"}] [] [2013-08-15 14:19:51] context.INFO: multicontext with empty [{"foo":"bar","stuff":"and things"},[]] [] [2013-08-15 14:19:51] context.INFO: multicontext with spaces [{"foo":"bar","stuff":"and things"},{"bat":"baz"}] [] -[2013-08-15 14:19:51] extra.INFO: context and extra [{"foo":"bar","stuff":"and things"},{"bat":"baz"}] [{"weebl":"bob"},{"lobob":"lo"}] \ No newline at end of file +[2013-08-15 14:19:51] extra-dashed.INFO: context and extra [{"foo":"bar","stuff":"and things"},{"bat":"baz"}] [{"weebl":"bob"},{"lobob":"lo"}] \ No newline at end of file