Skip to content

Commit

Permalink
Merge pull request #2 from mcrumm/bug/day-limit
Browse files Browse the repository at this point in the history
Feature:  Allow unlimited days
  • Loading branch information
ddtraceweb committed Aug 18, 2013
2 parents 67a086e + 0edb9bd commit a46634b
Show file tree
Hide file tree
Showing 4 changed files with 117 additions and 22 deletions.
6 changes: 6 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
language: php
php:
- 5.5
- 5.4
- 5.3
before_script: composer install
4 changes: 4 additions & 0 deletions src/Dubture/Monolog/Parser/LineLogParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,10 @@ public function parse($log, $days = 1, $pattern = 'default')
'extra' => json_decode($data['extra'], true)
);

if (0 === $days) {
return $array;
}

if (isset($date) && $date instanceof \DateTime) {
$d2 = new \DateTime('now');

Expand Down
35 changes: 18 additions & 17 deletions tests/Dubture/Monolog/Reader/Test/LogReaderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,28 +20,33 @@
*/
class LogReaderTest extends \PHPUnit_Framework_TestCase
{
public function testReader()
private $reader;

public function setUp()
{
$file = __DIR__ . '/../../../../files/test.log';
$reader = new LogReader($file);
$this->reader = new LogReader($file, 0);
}

$log = $reader[0];
public function testReader()
{
$log = $this->reader[0];

$this->assertInstanceOf('\DateTime', $log['date']);
$this->assertEquals('test', $log['logger']);
$this->assertEquals('INFO', $log['level']);
$this->assertEquals('foobar', $log['message']);
$this->assertArrayHasKey('foo', $log['context']);

$log = $reader[1];
$log = $this->reader[1];

$this->assertInstanceOf('\DateTime', $log['date']);
$this->assertEquals('aha', $log['logger']);
$this->assertEquals('DEBUG', $log['level']);
$this->assertEquals('foobar', $log['message']);
$this->assertArrayNotHasKey('foo', $log['context']);

$log = $reader[2];
$log = $this->reader[2];

$this->assertInstanceOf('\DateTime', $log['date']);
$this->assertEquals('context', $log['logger']);
Expand All @@ -50,7 +55,7 @@ public function testReader()
$this->assertArrayHasKey('foo', $log['context'][0]);
$this->assertArrayHasKey('bat', $log['context'][1]);

$log = $reader[3];
$log = $this->reader[3];

$this->assertInstanceOf('\DateTime', $log['date']);
$this->assertEquals('context', $log['logger']);
Expand All @@ -60,7 +65,7 @@ public function testReader()
$this->assertArrayHasKey('stuff', $log['context'][0]);
$this->assertArrayHasKey('bat', $log['context'][1]);

$log = $reader[4];
$log = $this->reader[4];

$this->assertInstanceOf('\DateTime', $log['date']);
$this->assertEquals('context', $log['logger']);
Expand All @@ -70,7 +75,7 @@ public function testReader()
$this->assertArrayHasKey('stuff', $log['context'][0]);
$this->assertEmpty($log['context'][1]);

$log = $reader[5];
$log = $this->reader[5];

$this->assertInstanceOf('\DateTime', $log['date']);
$this->assertEquals('context', $log['logger']);
Expand All @@ -80,7 +85,7 @@ public function testReader()
$this->assertArrayHasKey('stuff', $log['context'][0]);
$this->assertArrayHasKey('bat', $log['context'][1]);

$log = $reader[6];
$log = $this->reader[6];

$this->assertInstanceOf('\DateTime', $log['date']);
$this->assertEquals('extra', $log['logger']);
Expand All @@ -96,15 +101,13 @@ public function testReader()

public function testIterator()
{
$file = __DIR__ . '/../../../../files/test.log';
$reader = new LogReader($file);
$lines = array();
$keys = array();

$this->assertEquals(7, count($reader));
$this->assertEquals(7, count($this->reader));

foreach ($reader as $i => $log) {
$test = $reader[0];
foreach ($this->reader as $i => $log) {
$test = $this->reader[0];
$lines[] = $log;
$keys[] = $i;
}
Expand All @@ -125,9 +128,7 @@ public function testIterator()
*/
public function testException()
{
$file = __DIR__ . '/../../../../files/test.log';
$reader = new LogReader($file);
$reader[9] = 'foo';
$this->reader[9] = 'foo';

}
}
94 changes: 89 additions & 5 deletions tests/Dubture/Monolog/Reader/Test/ParserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,13 @@
*/
class ParserTest extends \PHPUnit_Framework_TestCase
{
private $parser;

public function setUp()
{
$this->parser = new LineLogParser();
}

public function logLineProvider()
{
return array(
Expand Down Expand Up @@ -80,16 +87,63 @@ public function logLineProvider()
);
}

public function daysLineProvider()
{
return array(
array (
'yesterday',
'[%s] days.DEBUG: %s [] []',
),
array (
'2 days ago',
'[%s] days.DEBUG: %s [] []',
),
array (
'3 days ago',
'[%s] days.DEBUG: %s [] []',
),
array (
'1 week ago',
'[%s] days.DEBUG: %s [] []',
),
array (
'1 month ago',
'[%s] days.DEBUG: %s [] []',
),
array (
'1 year ago',
'[%s] days.DEBUG: %s [] []',
),
);
}

public function datedLineProvider()
{
$dynamic = $this->daysLineProvider();

$array = array(
array (
false,
'[1970-01-01 00:00:00] dates.DEBUG: epoch [] []'
),
array (
false,
'[1955-11-05 19:00:00] dates.DEBUG: flux capacitor [] []'
)
);

return array_merge($dynamic, $array);
}

/**
* @dataProvider logLineProvider
*/
public function testLineFormatter($logger, $level, $message, $context, $extra, $line)
{
$now = new \DateTime();
$parser = new LineLogParser();
$logLine = sprintf($line, $now->format('Y-m-d H:i:s'));

$log = $parser->parse($logLine);
$now = new \DateTime();
$line = sprintf($line, $now->format('Y-m-d H:i:s'));

$log = $this->parser->parse($line);

$this->assertInstanceOf('\DateTime', $log['date']);
$this->assertEquals($logger, $log['logger']);
Expand All @@ -98,4 +152,34 @@ public function testLineFormatter($logger, $level, $message, $context, $extra, $
$this->assertEquals($context, $log['context']);
$this->assertEquals($extra, $log['extra']);
}

/**
* @dataProvider daysLineProvider
*/
public function testDaysFilter($time, $line)
{
$now = new \DateTime();
$date = new \DateTime($time);
$line = sprintf($line, $date->format('Y-m-d H:i:s'), $time);

$days = 1 + $date->diff($now)->days;
$log = $this->parser->parse($line, $days);

$this->assertNotEmpty($log);
}

/**
* @dataProvider datedLineProvider
*/
public function testZeroForDaysReturnsAllLines($time, $line)
{
if (false !== $time) {
$date = new \DateTime($time);
$line = sprintf($line, $date->format('Y-m-d H:i:s'), $time);
}

$log = $this->parser->parse($line, 0);

$this->assertNotEmpty($log);
}
}

0 comments on commit a46634b

Please sign in to comment.