Skip to content

Commit

Permalink
Merge pull request #4 from philwc/custom-pattern
Browse files Browse the repository at this point in the history
Add ability to use custom patterns
  • Loading branch information
ddtraceweb committed Nov 22, 2013
2 parents eae8839 + fe77c75 commit 83ab5f5
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 13 deletions.
46 changes: 33 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,22 @@ A simple library for parsing [monolog](https://github.com/Seldaek/monolog) logfi

You can install the library using [composer]('http://getcomposer.org/) by adding `ddtraceweb/monolog-parser` to your `composer.json`.

## Usage
## Usage

* 1 days of logs

```php
require_once 'path/to/vendor/autoload.php';

use Dubture\Monolog\Reader\LogReader;

$logFile = '/path/to/some/monolog.log';
$reader = new LogReader($logFile);

foreach ($reader as $log) {
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 All @@ -32,16 +32,16 @@ You can install the library using [composer]('http://getcomposer.org/) by adding

```php
require_once 'path/to/vendor/autoload.php';

use Dubture\Monolog\Reader\LogReader;

$logFile = '/path/to/some/monolog.log';
$reader = new LogReader($logFile, 0);

foreach ($reader as $log) {
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 All @@ -51,23 +51,43 @@ You can install the library using [composer]('http://getcomposer.org/) by adding

```php
require_once 'path/to/vendor/autoload.php';

use Dubture\Monolog\Reader\LogReader;

$logFile = '/path/to/some/monolog.log';
$reader = new LogReader($logFile, 2);

foreach ($reader as $log) {
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';

use Dubture\Monolog\Reader\LogReader;

$logFile = '/path/to/some/monolog.log';
$reader = new LogReader($logFile);

$pattern = '/\[(?P<date>.*)\] (?P<logger>[\w-\s]+).(?P<level>\w+): (?P<message>[^\[\{]+) (?P<context>[\[\{].*[\]\}]) (?P<extra>[\[\{].*[\]\}])/';
$reader->getParser()->registerPattern('newPatternName', $pattern);
$reader->setPattern('newPatternName');

foreach ($reader as $log) {
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'));

```


[![Bitdeli Badge](https://d2weczhvl823v0.cloudfront.net/ddtraceweb/monolog-parser/trend.png)](https://bitdeli.com/free "Bitdeli Badge")
Expand Down
14 changes: 14 additions & 0 deletions src/Dubture/Monolog/Parser/LineLogParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,4 +70,18 @@ public function parse($log, $days = 1, $pattern = 'default')
}
}
}

/**
* @param string $name
* @param string $pattern
*
* @throws \RuntimeException
*/
public function registerPattern($name, $pattern){
if(!isset($this->pattern[$name])){
$this->pattern[$name] = $pattern;
}else{
throw new \RuntimeException("Pattern $name already exists");
}
}
}
17 changes: 17 additions & 0 deletions src/Dubture/Monolog/Reader/LogReader.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,23 @@ public function __construct($file, $days = 1, $pattern = 'default')
$this->parser = $this->getDefaultParser($days, $pattern);
}

/**
* @return \Dubture\Monolog\Parser\LineLogParser|\Dubture\Monolog\Parser\LogParserInterface
*/
public function getParser()
{
$p = & $this->parser;
return $p;
}

/**
* @param string $pattern
*/
public function setPattern( $pattern = 'default' )
{
$this->pattern = $pattern;
}

/**
* {@inheritdoc}
*/
Expand Down

0 comments on commit 83ab5f5

Please sign in to comment.