Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ composer.phar
.settings
.project
bin
/.idea/
13 changes: 1 addition & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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';
Expand All @@ -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'));

```


Expand Down
42 changes: 37 additions & 5 deletions src/Dubture/Monolog/Parser/LineLogParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@
class LineLogParser implements LogParserInterface
{
/**
* @var string
* @var array
*/
protected $pattern = array(
'default' => '/\[(?P<date>.*)\] (?P<logger>\w+).(?P<level>\w+): (?P<message>[^\[\{]+) (?P<context>[\[\{].*[\]\}]) (?P<extra>[\[\{].*[\]\}])/',
'error' => '/\[(?P<date>.*)\] (?P<logger>\w+).(?P<level>\w+): (?P<message>(.*)+) (?P<context>[^ ]+) (?P<extra>[^ ]+)/'
'default' => '/\[(?P<date>.*)\]\s(?P<logger>[\w-]+)\.(?P<level>\w+):\s(?P<message>[^\[\{]+)\s(?P<context>[\[\{].*[\]\}])\s(?P<extra>[\[\{].*[\]\}])/',
'error' => '/\[(?P<date>.*)\]\s(?P<logger>[\w-]+).(?P<level>\w+):\s(?P<message>(.*)+)\s(?P<context>[^\s]+)\s(?P<extra>[^\s]+)/'
);


Expand Down Expand Up @@ -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();
}

/**
Expand All @@ -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]);
}
}
27 changes: 27 additions & 0 deletions src/Dubture/Monolog/Parser/LogParserInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
20 changes: 14 additions & 6 deletions src/Dubture/Monolog/Reader/LogReader.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
namespace Dubture\Monolog\Reader;

use Dubture\Monolog\Reader\AbstractReader;
use Dubture\Monolog\Parser\LogParserInterface;

/**
* Class LogReader
Expand All @@ -30,7 +31,7 @@ class LogReader extends AbstractReader implements \Iterator, \ArrayAccess, \Coun
protected $lineCount;

/**
* @var \Dubture\Monolog\Parser\LogParserInterface
* @var LogParserInterface
*/
protected $parser;

Expand All @@ -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;
Expand All @@ -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;
}

/**
Expand Down Expand Up @@ -152,7 +160,7 @@ public function key()
*/
public function valid()
{
return $this->file->valid();
return $this->file->valid() && !empty($this->file->current());
}

/**
Expand Down
4 changes: 2 additions & 2 deletions tests/Dubture/Monolog/Reader/Test/LogReaderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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]);
Expand Down Expand Up @@ -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']);

}

Expand Down
2 changes: 1 addition & 1 deletion tests/files/test.log
Original file line number Diff line number Diff line change
Expand Up @@ -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"}]
[2013-08-15 14:19:51] extra-dashed.INFO: context and extra [{"foo":"bar","stuff":"and things"},{"bat":"baz"}] [{"weebl":"bob"},{"lobob":"lo"}]