├── .gitignore ├── .travis.yml ├── LICENSE ├── README.md ├── composer.json ├── composer.lock ├── phpunit.xml.dist ├── src └── Dubture │ └── Monolog │ ├── Parser │ ├── LineLogParser.php │ └── LogParserInterface.php │ └── Reader │ ├── AbstractReader.php │ └── LogReader.php └── tests ├── Dubture └── Monolog │ └── Reader │ └── Test │ ├── LogReaderTest.php │ └── ParserTest.php ├── bootstrap.php └── files └── test.log /.gitignore: -------------------------------------------------------------------------------- 1 | .DS_Store 2 | vendor 3 | composer.phar 4 | .buildpath 5 | .settings 6 | .project 7 | bin 8 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: php 2 | php: 3 | - 5.5 4 | - 5.4 5 | - 5.3 6 | before_script: composer install -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Copyright (C) 2013 Robert Gruendler 2 | 3 | Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: 4 | 5 | The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. 6 | 7 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | Monolog Parser [![Build Status](https://travis-ci.org/ddtraceweb/monolog-parser.png?branch=master)](https://travis-ci.org/ddtraceweb/monolog-parser) 2 | ============== 3 | 4 | A simple library for parsing [monolog](https://github.com/Seldaek/monolog) logfiles. 5 | 6 | ## Installation 7 | 8 | You can install the library using [composer]('http://getcomposer.org/) by adding `ddtraceweb/monolog-parser` to your `composer.json`. 9 | 10 | ## Usage 11 | 12 | * 1 days of logs 13 | 14 | ```php 15 | require_once 'path/to/vendor/autoload.php'; 16 | 17 | use Dubture\Monolog\Reader\LogReader; 18 | 19 | $logFile = '/path/to/some/monolog.log'; 20 | $reader = new LogReader($logFile); 21 | 22 | foreach ($reader as $log) { 23 | echo sprintf("The log entry was written at %s. \n", $log['date']->format('Y-m-d h:i:s')); 24 | } 25 | 26 | $lastLine = $reader[count($reader)-1]; 27 | echo sprintf("The last log entry was written at %s. \n", $lastLine['date']->format('Y-m-d h:i:s')); 28 | 29 | ``` 30 | 31 | * options unlimited days logs 32 | 33 | ```php 34 | require_once 'path/to/vendor/autoload.php'; 35 | 36 | use Dubture\Monolog\Reader\LogReader; 37 | 38 | $logFile = '/path/to/some/monolog.log'; 39 | $reader = new LogReader($logFile, 0); 40 | 41 | foreach ($reader as $log) { 42 | echo sprintf("The log entry was written at %s. \n", $log['date']->format('Y-m-d h:i:s')); 43 | } 44 | 45 | $lastLine = $reader[count($reader)-1]; 46 | echo sprintf("The last log entry was written at %s. \n", $lastLine['date']->format('Y-m-d h:i:s')); 47 | 48 | ``` 49 | 50 | * options 2 days logs 51 | 52 | ```php 53 | require_once 'path/to/vendor/autoload.php'; 54 | 55 | use Dubture\Monolog\Reader\LogReader; 56 | 57 | $logFile = '/path/to/some/monolog.log'; 58 | $reader = new LogReader($logFile, 2); 59 | 60 | foreach ($reader as $log) { 61 | echo sprintf("The log entry was written at %s. \n", $log['date']->format('Y-m-d h:i:s')); 62 | } 63 | 64 | $lastLine = $reader[count($reader)-1]; 65 | echo sprintf("The last log entry was written at %s. \n", $lastLine['date']->format('Y-m-d h:i:s')); 66 | 67 | ``` 68 | 69 | * Add custom pattern 70 | ```php 71 | 72 | require_once 'path/to/vendor/autoload.php'; 73 | 74 | use Dubture\Monolog\Reader\LogReader; 75 | 76 | $logFile = '/path/to/some/monolog.log'; 77 | $reader = new LogReader($logFile); 78 | 79 | $pattern = '/\[(?P.*)\] (?P[\w-\s]+).(?P\w+): (?P[^\[\{]+) (?P[\[\{].*[\]\}]) (?P[\[\{].*[\]\}])/'; 80 | $reader->getParser()->registerPattern('newPatternName', $pattern); 81 | $reader->setPattern('newPatternName'); 82 | 83 | foreach ($reader as $log) { 84 | echo sprintf("The log entry was written at %s. \n", $log['date']->format('Y-m-d h:i:s')); 85 | } 86 | 87 | $lastLine = $reader[count($reader)-1]; 88 | echo sprintf("The last log entry was written at %s. \n", $lastLine['date']->format('Y-m-d h:i:s')); 89 | 90 | ``` 91 | 92 | 93 | [![Bitdeli Badge](https://d2weczhvl823v0.cloudfront.net/ddtraceweb/monolog-parser/trend.png)](https://bitdeli.com/free "Bitdeli Badge") 94 | 95 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "ddtraceweb/monolog-parser", 3 | "description": "A parser for monolog log entries", 4 | "require": { 5 | "monolog/monolog": "1.*" 6 | }, 7 | "require-dev": { 8 | "phpunit/phpunit": "3.7.*" 9 | }, 10 | "license": "MIT", 11 | "authors": [ 12 | { 13 | "name": "Robert Gruendler", 14 | "email": "r.gruendler@gmail.com" 15 | } 16 | ], 17 | "autoload": { 18 | "psr-0": {"Dubture": "src/"} 19 | }, 20 | "minimum-stability": "dev", 21 | "config": { 22 | "bin-dir": "bin" 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /composer.lock: -------------------------------------------------------------------------------- 1 | { 2 | "hash": "c55c34db6cb410e12fd294fad8e69375", 3 | "packages": [ 4 | { 5 | "name": "monolog/monolog", 6 | "version": "dev-master", 7 | "source": { 8 | "type": "git", 9 | "url": "https://github.com/Seldaek/monolog.git", 10 | "reference": "041aa3930f8d2b466ae897440ec9471d5159c5bf" 11 | }, 12 | "dist": { 13 | "type": "zip", 14 | "url": "https://api.github.com/repos/Seldaek/monolog/zipball/041aa3930f8d2b466ae897440ec9471d5159c5bf", 15 | "reference": "041aa3930f8d2b466ae897440ec9471d5159c5bf", 16 | "shasum": "" 17 | }, 18 | "require": { 19 | "php": ">=5.3.0", 20 | "psr/log": ">=1.0,<2.0" 21 | }, 22 | "require-dev": { 23 | "doctrine/couchdb": "dev-master", 24 | "mlehner/gelf-php": "1.0.*", 25 | "raven/raven": "0.3.*" 26 | }, 27 | "suggest": { 28 | "doctrine/couchdb": "Allow sending log messages to a CouchDB server", 29 | "ext-amqp": "Allow sending log messages to an AMQP server (1.0+ required)", 30 | "ext-mongo": "Allow sending log messages to a MongoDB server", 31 | "mlehner/gelf-php": "Allow sending log messages to a GrayLog2 server", 32 | "raven/raven": "Allow sending log messages to a Sentry server" 33 | }, 34 | "type": "library", 35 | "extra": { 36 | "branch-alias": { 37 | "dev-master": "1.4.x-dev" 38 | } 39 | }, 40 | "autoload": { 41 | "psr-0": { 42 | "Monolog": "src/" 43 | } 44 | }, 45 | "notification-url": "https://packagist.org/downloads/", 46 | "license": [ 47 | "MIT" 48 | ], 49 | "authors": [ 50 | { 51 | "name": "Jordi Boggiano", 52 | "email": "j.boggiano@seld.be", 53 | "homepage": "http://seld.be", 54 | "role": "Developer" 55 | } 56 | ], 57 | "description": "Sends your logs to files, sockets, inboxes, databases and various web services", 58 | "homepage": "http://github.com/Seldaek/monolog", 59 | "keywords": [ 60 | "log", 61 | "logging", 62 | "psr-3" 63 | ], 64 | "time": "2013-02-26 10:18:11" 65 | }, 66 | { 67 | "name": "psr/log", 68 | "version": "1.0.0", 69 | "source": { 70 | "type": "git", 71 | "url": "https://github.com/php-fig/log", 72 | "reference": "1.0.0" 73 | }, 74 | "dist": { 75 | "type": "zip", 76 | "url": "https://github.com/php-fig/log/archive/1.0.0.zip", 77 | "reference": "1.0.0", 78 | "shasum": "" 79 | }, 80 | "type": "library", 81 | "autoload": { 82 | "psr-0": { 83 | "Psr\\Log\\": "" 84 | } 85 | }, 86 | "notification-url": "https://packagist.org/downloads/", 87 | "license": [ 88 | "MIT" 89 | ], 90 | "authors": [ 91 | { 92 | "name": "PHP-FIG", 93 | "homepage": "http://www.php-fig.org/" 94 | } 95 | ], 96 | "description": "Common interface for logging libraries", 97 | "keywords": [ 98 | "log", 99 | "psr", 100 | "psr-3" 101 | ], 102 | "time": "2012-12-21 11:40:51" 103 | } 104 | ], 105 | "packages-dev": [ 106 | { 107 | "name": "phpunit/php-code-coverage", 108 | "version": "1.2.x-dev", 109 | "source": { 110 | "type": "git", 111 | "url": "https://github.com/sebastianbergmann/php-code-coverage.git", 112 | "reference": "1.2.9" 113 | }, 114 | "dist": { 115 | "type": "zip", 116 | "url": "https://api.github.com/repos/sebastianbergmann/php-code-coverage/zipball/1.2.9", 117 | "reference": "1.2.9", 118 | "shasum": "" 119 | }, 120 | "require": { 121 | "php": ">=5.3.3", 122 | "phpunit/php-file-iterator": ">=1.3.0@stable", 123 | "phpunit/php-text-template": ">=1.1.1@stable", 124 | "phpunit/php-token-stream": ">=1.1.3@stable" 125 | }, 126 | "suggest": { 127 | "ext-dom": "*", 128 | "ext-xdebug": ">=2.0.5" 129 | }, 130 | "type": "library", 131 | "autoload": { 132 | "classmap": [ 133 | "PHP/" 134 | ] 135 | }, 136 | "notification-url": "https://packagist.org/downloads/", 137 | "include-path": [ 138 | "" 139 | ], 140 | "license": [ 141 | "BSD-3-Clause" 142 | ], 143 | "authors": [ 144 | { 145 | "name": "Sebastian Bergmann", 146 | "email": "sb@sebastian-bergmann.de", 147 | "role": "lead" 148 | } 149 | ], 150 | "description": "Library that provides collection, processing, and rendering functionality for PHP code coverage information.", 151 | "homepage": "https://github.com/sebastianbergmann/php-code-coverage", 152 | "keywords": [ 153 | "coverage", 154 | "testing", 155 | "xunit" 156 | ], 157 | "time": "2013-02-26 18:55:56" 158 | }, 159 | { 160 | "name": "phpunit/php-file-iterator", 161 | "version": "dev-master", 162 | "source": { 163 | "type": "git", 164 | "url": "https://github.com/sebastianbergmann/php-file-iterator.git", 165 | "reference": "2deb24c65ea78e126daa8d45b2089ddc29ec1d26" 166 | }, 167 | "dist": { 168 | "type": "zip", 169 | "url": "https://api.github.com/repos/sebastianbergmann/php-file-iterator/zipball/2deb24c65ea78e126daa8d45b2089ddc29ec1d26", 170 | "reference": "2deb24c65ea78e126daa8d45b2089ddc29ec1d26", 171 | "shasum": "" 172 | }, 173 | "require": { 174 | "php": ">=5.3.3" 175 | }, 176 | "type": "library", 177 | "autoload": { 178 | "classmap": [ 179 | "File/" 180 | ] 181 | }, 182 | "notification-url": "https://packagist.org/downloads/", 183 | "include-path": [ 184 | "" 185 | ], 186 | "license": [ 187 | "BSD-3-Clause" 188 | ], 189 | "authors": [ 190 | { 191 | "name": "Sebastian Bergmann", 192 | "email": "sb@sebastian-bergmann.de", 193 | "role": "lead" 194 | } 195 | ], 196 | "description": "FilterIterator implementation that filters files based on a list of suffixes.", 197 | "homepage": "https://github.com/sebastianbergmann/php-file-iterator/", 198 | "keywords": [ 199 | "filesystem", 200 | "iterator" 201 | ], 202 | "time": "2013-01-07 10:47:05" 203 | }, 204 | { 205 | "name": "phpunit/php-text-template", 206 | "version": "dev-master", 207 | "source": { 208 | "type": "git", 209 | "url": "https://github.com/sebastianbergmann/php-text-template.git", 210 | "reference": "1eeef106193d2f8c539728e566bb4793071a9e18" 211 | }, 212 | "dist": { 213 | "type": "zip", 214 | "url": "https://api.github.com/repos/sebastianbergmann/php-text-template/zipball/1eeef106193d2f8c539728e566bb4793071a9e18", 215 | "reference": "1eeef106193d2f8c539728e566bb4793071a9e18", 216 | "shasum": "" 217 | }, 218 | "require": { 219 | "php": ">=5.3.3" 220 | }, 221 | "type": "library", 222 | "autoload": { 223 | "classmap": [ 224 | "Text/" 225 | ] 226 | }, 227 | "notification-url": "https://packagist.org/downloads/", 228 | "include-path": [ 229 | "" 230 | ], 231 | "license": [ 232 | "BSD-3-Clause" 233 | ], 234 | "authors": [ 235 | { 236 | "name": "Sebastian Bergmann", 237 | "email": "sb@sebastian-bergmann.de", 238 | "role": "lead" 239 | } 240 | ], 241 | "description": "Simple template engine.", 242 | "homepage": "https://github.com/sebastianbergmann/php-text-template/", 243 | "keywords": [ 244 | "template" 245 | ], 246 | "time": "2013-01-07 10:56:17" 247 | }, 248 | { 249 | "name": "phpunit/php-timer", 250 | "version": "1.0.x-dev", 251 | "source": { 252 | "type": "git", 253 | "url": "https://github.com/sebastianbergmann/php-timer.git", 254 | "reference": "ecf7920b27003a9412b07dad79dbb5ad1249e6c3" 255 | }, 256 | "dist": { 257 | "type": "zip", 258 | "url": "https://api.github.com/repos/sebastianbergmann/php-timer/zipball/ecf7920b27003a9412b07dad79dbb5ad1249e6c3", 259 | "reference": "ecf7920b27003a9412b07dad79dbb5ad1249e6c3", 260 | "shasum": "" 261 | }, 262 | "require": { 263 | "php": ">=5.3.3" 264 | }, 265 | "type": "library", 266 | "autoload": { 267 | "classmap": [ 268 | "PHP/" 269 | ] 270 | }, 271 | "notification-url": "https://packagist.org/downloads/", 272 | "include-path": [ 273 | "" 274 | ], 275 | "license": [ 276 | "BSD-3-Clause" 277 | ], 278 | "authors": [ 279 | { 280 | "name": "Sebastian Bergmann", 281 | "email": "sb@sebastian-bergmann.de", 282 | "role": "lead" 283 | } 284 | ], 285 | "description": "Utility class for timing", 286 | "homepage": "https://github.com/sebastianbergmann/php-timer/", 287 | "keywords": [ 288 | "timer" 289 | ], 290 | "time": "2013-01-30 06:08:51" 291 | }, 292 | { 293 | "name": "phpunit/php-token-stream", 294 | "version": "dev-master", 295 | "source": { 296 | "type": "git", 297 | "url": "https://github.com/sebastianbergmann/php-token-stream.git", 298 | "reference": "c25dd88e1592e66dee2553c99ef244203d5a1b98" 299 | }, 300 | "dist": { 301 | "type": "zip", 302 | "url": "https://api.github.com/repos/sebastianbergmann/php-token-stream/zipball/c25dd88e1592e66dee2553c99ef244203d5a1b98", 303 | "reference": "c25dd88e1592e66dee2553c99ef244203d5a1b98", 304 | "shasum": "" 305 | }, 306 | "require": { 307 | "ext-tokenizer": "*", 308 | "php": ">=5.3.3" 309 | }, 310 | "type": "library", 311 | "autoload": { 312 | "classmap": [ 313 | "PHP/" 314 | ] 315 | }, 316 | "notification-url": "https://packagist.org/downloads/", 317 | "include-path": [ 318 | "" 319 | ], 320 | "license": [ 321 | "BSD-3-Clause" 322 | ], 323 | "authors": [ 324 | { 325 | "name": "Sebastian Bergmann", 326 | "email": "sb@sebastian-bergmann.de", 327 | "role": "lead" 328 | } 329 | ], 330 | "description": "Wrapper around PHP's tokenizer extension.", 331 | "homepage": "https://github.com/sebastianbergmann/php-token-stream/", 332 | "keywords": [ 333 | "tokenizer" 334 | ], 335 | "time": "2013-01-07 10:56:35" 336 | }, 337 | { 338 | "name": "phpunit/phpunit", 339 | "version": "3.7.x-dev", 340 | "source": { 341 | "type": "git", 342 | "url": "https://github.com/sebastianbergmann/phpunit.git", 343 | "reference": "2c67e52445416bb7c14046b432acd7eb79e4e612" 344 | }, 345 | "dist": { 346 | "type": "zip", 347 | "url": "https://api.github.com/repos/sebastianbergmann/phpunit/zipball/2c67e52445416bb7c14046b432acd7eb79e4e612", 348 | "reference": "2c67e52445416bb7c14046b432acd7eb79e4e612", 349 | "shasum": "" 350 | }, 351 | "require": { 352 | "ext-dom": "*", 353 | "ext-pcre": "*", 354 | "ext-reflection": "*", 355 | "ext-spl": "*", 356 | "php": ">=5.3.3", 357 | "phpunit/php-code-coverage": ">=1.2.1,<1.3.0", 358 | "phpunit/php-file-iterator": ">=1.3.1", 359 | "phpunit/php-text-template": ">=1.1.1", 360 | "phpunit/php-timer": ">=1.0.2,<1.1.0", 361 | "phpunit/phpunit-mock-objects": ">=1.2.0,<1.3.0", 362 | "symfony/yaml": ">=2.2.0" 363 | }, 364 | "require-dev": { 365 | "pear-pear/pear": "1.9.4" 366 | }, 367 | "suggest": { 368 | "ext-json": "*", 369 | "ext-simplexml": "*", 370 | "ext-tokenizer": "*", 371 | "phpunit/php-invoker": ">=1.1.0,<1.2.0" 372 | }, 373 | "bin": [ 374 | "composer/bin/phpunit" 375 | ], 376 | "type": "library", 377 | "extra": { 378 | "branch-alias": { 379 | "dev-master": "3.7.x-dev" 380 | } 381 | }, 382 | "autoload": { 383 | "classmap": [ 384 | "PHPUnit/" 385 | ] 386 | }, 387 | "notification-url": "https://packagist.org/downloads/", 388 | "include-path": [ 389 | "", 390 | "../../symfony/yaml/" 391 | ], 392 | "license": [ 393 | "BSD-3-Clause" 394 | ], 395 | "authors": [ 396 | { 397 | "name": "Sebastian Bergmann", 398 | "email": "sebastian@phpunit.de", 399 | "role": "lead" 400 | } 401 | ], 402 | "description": "The PHP Unit Testing framework.", 403 | "homepage": "http://www.phpunit.de/", 404 | "keywords": [ 405 | "phpunit", 406 | "testing", 407 | "xunit" 408 | ], 409 | "time": "2013-03-11 07:06:05" 410 | }, 411 | { 412 | "name": "phpunit/phpunit-mock-objects", 413 | "version": "1.2.x-dev", 414 | "source": { 415 | "type": "git", 416 | "url": "https://github.com/sebastianbergmann/phpunit-mock-objects.git", 417 | "reference": "d49b5683200b5db9b1c64cb06f52f50d147891c4" 418 | }, 419 | "dist": { 420 | "type": "zip", 421 | "url": "https://api.github.com/repos/sebastianbergmann/phpunit-mock-objects/zipball/d49b5683200b5db9b1c64cb06f52f50d147891c4", 422 | "reference": "d49b5683200b5db9b1c64cb06f52f50d147891c4", 423 | "shasum": "" 424 | }, 425 | "require": { 426 | "php": ">=5.3.3", 427 | "phpunit/php-text-template": ">=1.1.1@stable" 428 | }, 429 | "suggest": { 430 | "ext-soap": "*" 431 | }, 432 | "type": "library", 433 | "autoload": { 434 | "classmap": [ 435 | "PHPUnit/" 436 | ] 437 | }, 438 | "notification-url": "https://packagist.org/downloads/", 439 | "include-path": [ 440 | "" 441 | ], 442 | "license": [ 443 | "BSD-3-Clause" 444 | ], 445 | "authors": [ 446 | { 447 | "name": "Sebastian Bergmann", 448 | "email": "sb@sebastian-bergmann.de", 449 | "role": "lead" 450 | } 451 | ], 452 | "description": "Mock Object library for PHPUnit", 453 | "homepage": "https://github.com/sebastianbergmann/phpunit-mock-objects/", 454 | "keywords": [ 455 | "mock", 456 | "xunit" 457 | ], 458 | "time": "2013-02-05 07:46:41" 459 | }, 460 | { 461 | "name": "symfony/yaml", 462 | "version": "dev-master", 463 | "target-dir": "Symfony/Component/Yaml", 464 | "source": { 465 | "type": "git", 466 | "url": "https://github.com/symfony/Yaml.git", 467 | "reference": "f198ac28048eeceae852419c076123aaee59cd1c" 468 | }, 469 | "dist": { 470 | "type": "zip", 471 | "url": "https://api.github.com/repos/symfony/Yaml/zipball/f198ac28048eeceae852419c076123aaee59cd1c", 472 | "reference": "f198ac28048eeceae852419c076123aaee59cd1c", 473 | "shasum": "" 474 | }, 475 | "require": { 476 | "php": ">=5.3.3" 477 | }, 478 | "type": "library", 479 | "extra": { 480 | "branch-alias": { 481 | "dev-master": "2.3-dev" 482 | } 483 | }, 484 | "autoload": { 485 | "psr-0": { 486 | "Symfony\\Component\\Yaml\\": "" 487 | } 488 | }, 489 | "notification-url": "https://packagist.org/downloads/", 490 | "license": [ 491 | "MIT" 492 | ], 493 | "authors": [ 494 | { 495 | "name": "Fabien Potencier", 496 | "email": "fabien@symfony.com" 497 | }, 498 | { 499 | "name": "Symfony Community", 500 | "homepage": "http://symfony.com/contributors" 501 | } 502 | ], 503 | "description": "Symfony Yaml Component", 504 | "homepage": "http://symfony.com", 505 | "time": "2013-01-31 21:39:01" 506 | } 507 | ], 508 | "aliases": [ 509 | 510 | ], 511 | "minimum-stability": "dev", 512 | "stability-flags": { 513 | "monolog/monolog": 20 514 | }, 515 | "platform": [ 516 | 517 | ], 518 | "platform-dev": [ 519 | 520 | ] 521 | } 522 | -------------------------------------------------------------------------------- /phpunit.xml.dist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | tests/Dubture/ 7 | 8 | 9 | 10 | 11 | 12 | src/Dubture/ 13 | 14 | 15 | -------------------------------------------------------------------------------- /src/Dubture/Monolog/Parser/LineLogParser.php: -------------------------------------------------------------------------------- 1 | 7 | * 8 | * For the full copyright and license information, please view the LICENSE 9 | * file that was distributed with this source code. 10 | */ 11 | 12 | namespace Dubture\Monolog\Parser; 13 | 14 | /** 15 | * Class LineLogParser 16 | * @package Dubture\Monolog\Parser 17 | */ 18 | class LineLogParser implements LogParserInterface 19 | { 20 | /** 21 | * @var string 22 | */ 23 | protected $pattern = array( 24 | 'default' => '/\[(?P.*)\] (?P[\w-]+).(?P\w+): (?P[^\[\{]+) (?P[\[\{].*[\]\}]) (?P[\[\{].*[\]\}])/', 25 | 'error' => '/\[(?P.*)\] (?P[\w-]+).(?P\w+): (?P(.*)+) (?P[^ ]+) (?P[^ ]+)/' 26 | ); 27 | 28 | 29 | /** 30 | * @param string $log 31 | * @param int $days 32 | * @param string $pattern 33 | * 34 | * @return array 35 | */ 36 | public function parse($log, $days = 1, $pattern = 'default') 37 | { 38 | if (!is_string($log) || strlen($log) === 0) { 39 | return array(); 40 | } 41 | 42 | preg_match($this->pattern[$pattern], $log, $data); 43 | 44 | if (!isset($data['date'])) { 45 | return array(); 46 | } 47 | 48 | $date = \DateTime::createFromFormat('Y-m-d H:i:s', $data['date']); 49 | 50 | $array = array( 51 | 'date' => $date, 52 | 'logger' => $data['logger'], 53 | 'level' => $data['level'], 54 | 'message' => $data['message'], 55 | 'context' => json_decode($data['context'], true), 56 | 'extra' => json_decode($data['extra'], true) 57 | ); 58 | 59 | if (0 === $days) { 60 | return $array; 61 | } 62 | 63 | if (isset($date) && $date instanceof \DateTime) { 64 | $d2 = new \DateTime('now'); 65 | 66 | if ($date->diff($d2)->days < $days) { 67 | return $array; 68 | } else { 69 | return array(); 70 | } 71 | } 72 | } 73 | 74 | /** 75 | * @param string $name 76 | * @param string $pattern 77 | * 78 | * @throws \RuntimeException 79 | */ 80 | public function registerPattern($name, $pattern) 81 | { 82 | if (!isset($this->pattern[$name])) { 83 | $this->pattern[$name] = $pattern; 84 | } else { 85 | throw new \RuntimeException("Pattern $name already exists"); 86 | } 87 | } 88 | } 89 | -------------------------------------------------------------------------------- /src/Dubture/Monolog/Parser/LogParserInterface.php: -------------------------------------------------------------------------------- 1 | 7 | * 8 | * For the full copyright and license information, please view the LICENSE 9 | * file that was distributed with this source code. 10 | */ 11 | 12 | namespace Dubture\Monolog\Parser; 13 | 14 | /** 15 | * 16 | * @author Robert Gruendler 17 | */ 18 | interface LogParserInterface 19 | { 20 | /** 21 | * @param $log 22 | * @param $days 23 | * @param $pattern 24 | * 25 | * @return mixed 26 | */ 27 | public function parse($log, $days, $pattern); 28 | } 29 | -------------------------------------------------------------------------------- /src/Dubture/Monolog/Reader/AbstractReader.php: -------------------------------------------------------------------------------- 1 | 7 | * 8 | * For the full copyright and license information, please view the LICENSE 9 | * file that was distributed with this source code. 10 | */ 11 | 12 | namespace Dubture\Monolog\Reader; 13 | 14 | use Dubture\Monolog\Parser\LineLogParser; 15 | 16 | /** 17 | * Class AbstractReader 18 | * @package Dubture\Monolog\Reader 19 | */ 20 | class AbstractReader 21 | { 22 | 23 | /** 24 | * @param $days 25 | * @param $pattern 26 | * 27 | * @return LineLogParser 28 | */ 29 | protected function getDefaultParser($days, $pattern) 30 | { 31 | return new LineLogParser($days, $pattern); 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /src/Dubture/Monolog/Reader/LogReader.php: -------------------------------------------------------------------------------- 1 | 7 | * 8 | * For the full copyright and license information, please view the LICENSE 9 | * file that was distributed with this source code. 10 | */ 11 | 12 | namespace Dubture\Monolog\Reader; 13 | 14 | use Dubture\Monolog\Reader\AbstractReader; 15 | 16 | /** 17 | * Class LogReader 18 | * @package Dubture\Monolog\Reader 19 | */ 20 | class LogReader extends AbstractReader implements \Iterator, \ArrayAccess, \Countable 21 | { 22 | /** 23 | * @var \SplFileObject 24 | */ 25 | protected $file; 26 | 27 | /** 28 | * @var integer 29 | */ 30 | protected $lineCount; 31 | 32 | /** 33 | * @var \Dubture\Monolog\Parser\LogParserInterface 34 | */ 35 | protected $parser; 36 | 37 | public $days; 38 | public $pattern; 39 | 40 | 41 | /** 42 | * @param $file 43 | * @param int $days 44 | * @param string $pattern 45 | */ 46 | public function __construct($file, $days = 1, $pattern = 'default') 47 | { 48 | $this->file = new \SplFileObject($file, 'r'); 49 | $i = 0; 50 | while (!$this->file->eof()) { 51 | $this->file->current(); 52 | $this->file->next(); 53 | $i++; 54 | } 55 | 56 | $this->days = $days; 57 | $this->pattern = $pattern; 58 | 59 | $this->lineCount = $i; 60 | $this->parser = $this->getDefaultParser($days, $pattern); 61 | } 62 | 63 | /** 64 | * @return \Dubture\Monolog\Parser\LineLogParser|\Dubture\Monolog\Parser\LogParserInterface 65 | */ 66 | public function getParser() 67 | { 68 | $p = & $this->parser; 69 | return $p; 70 | } 71 | 72 | /** 73 | * @param string $pattern 74 | */ 75 | public function setPattern( $pattern = 'default' ) 76 | { 77 | $this->pattern = $pattern; 78 | } 79 | 80 | /** 81 | * {@inheritdoc} 82 | */ 83 | public function offsetExists($offset) 84 | { 85 | return $this->lineCount < $offset; 86 | } 87 | 88 | /** 89 | * {@inheritdoc} 90 | */ 91 | public function offsetGet($offset) 92 | { 93 | $key = $this->file->key(); 94 | $this->file->seek($offset); 95 | $log = $this->current(); 96 | $this->file->seek($key); 97 | $this->file->current(); 98 | 99 | return $log; 100 | } 101 | 102 | /** 103 | * {@inheritdoc} 104 | */ 105 | public function offsetSet($offset, $value) 106 | { 107 | throw new \RuntimeException("LogReader is read-only."); 108 | } 109 | 110 | /** 111 | * {@inheritdoc} 112 | */ 113 | public function offsetUnset($offset) 114 | { 115 | throw new \RuntimeException("LogReader is read-only."); 116 | } 117 | 118 | /** 119 | * {@inheritdoc} 120 | */ 121 | public function rewind() 122 | { 123 | $this->file->rewind(); 124 | } 125 | 126 | /** 127 | * {@inheritdoc} 128 | */ 129 | public function next() 130 | { 131 | $this->file->next(); 132 | } 133 | 134 | /** 135 | * {@inheritdoc} 136 | */ 137 | public function current() 138 | { 139 | return $this->parser->parse($this->file->current(), $this->days, $this->pattern); 140 | } 141 | 142 | /** 143 | * {@inheritdoc} 144 | */ 145 | public function key() 146 | { 147 | return $this->file->key(); 148 | } 149 | 150 | /** 151 | * {@inheritdoc} 152 | */ 153 | public function valid() 154 | { 155 | return $this->file->valid(); 156 | } 157 | 158 | /** 159 | * {@inheritdoc} 160 | */ 161 | public function count() 162 | { 163 | return $this->lineCount; 164 | } 165 | } 166 | -------------------------------------------------------------------------------- /tests/Dubture/Monolog/Reader/Test/LogReaderTest.php: -------------------------------------------------------------------------------- 1 | 7 | * 8 | * For the full copyright and license information, please view the LICENSE 9 | * file that was distributed with this source code. 10 | */ 11 | 12 | namespace Dubture\Monolog\Reader\Test; 13 | 14 | use Dubture\Monolog\Reader\ReverseLogReader; 15 | use Dubture\Monolog\Reader\LogReader; 16 | 17 | 18 | /** 19 | * @author Robert Gruendler 20 | */ 21 | class LogReaderTest extends \PHPUnit_Framework_TestCase 22 | { 23 | private $reader; 24 | 25 | public function setUp() 26 | { 27 | $file = __DIR__ . '/../../../../files/test.log'; 28 | $this->reader = new LogReader($file, 0); 29 | } 30 | 31 | public function testReader() 32 | { 33 | $log = $this->reader[0]; 34 | 35 | $this->assertInstanceOf('\DateTime', $log['date']); 36 | $this->assertEquals('test', $log['logger']); 37 | $this->assertEquals('INFO', $log['level']); 38 | $this->assertEquals('foobar', $log['message']); 39 | $this->assertArrayHasKey('foo', $log['context']); 40 | 41 | $log = $this->reader[1]; 42 | 43 | $this->assertInstanceOf('\DateTime', $log['date']); 44 | $this->assertEquals('aha', $log['logger']); 45 | $this->assertEquals('DEBUG', $log['level']); 46 | $this->assertEquals('foobar', $log['message']); 47 | $this->assertArrayNotHasKey('foo', $log['context']); 48 | 49 | $log = $this->reader[2]; 50 | 51 | $this->assertInstanceOf('\DateTime', $log['date']); 52 | $this->assertEquals('context', $log['logger']); 53 | $this->assertEquals('INFO', $log['level']); 54 | $this->assertEquals('multicontext', $log['message']); 55 | $this->assertArrayHasKey('foo', $log['context'][0]); 56 | $this->assertArrayHasKey('bat', $log['context'][1]); 57 | 58 | $log = $this->reader[3]; 59 | 60 | $this->assertInstanceOf('\DateTime', $log['date']); 61 | $this->assertEquals('context', $log['logger']); 62 | $this->assertEquals('INFO', $log['level']); 63 | $this->assertEquals('multicontext', $log['message']); 64 | $this->assertArrayHasKey('foo', $log['context'][0]); 65 | $this->assertArrayHasKey('stuff', $log['context'][0]); 66 | $this->assertArrayHasKey('bat', $log['context'][1]); 67 | 68 | $log = $this->reader[4]; 69 | 70 | $this->assertInstanceOf('\DateTime', $log['date']); 71 | $this->assertEquals('context', $log['logger']); 72 | $this->assertEquals('INFO', $log['level']); 73 | $this->assertEquals('multicontext with empty', $log['message']); 74 | $this->assertArrayHasKey('foo', $log['context'][0]); 75 | $this->assertArrayHasKey('stuff', $log['context'][0]); 76 | $this->assertEmpty($log['context'][1]); 77 | 78 | $log = $this->reader[5]; 79 | 80 | $this->assertInstanceOf('\DateTime', $log['date']); 81 | $this->assertEquals('context', $log['logger']); 82 | $this->assertEquals('INFO', $log['level']); 83 | $this->assertEquals('multicontext with spaces', $log['message']); 84 | $this->assertArrayHasKey('foo', $log['context'][0]); 85 | $this->assertArrayHasKey('stuff', $log['context'][0]); 86 | $this->assertArrayHasKey('bat', $log['context'][1]); 87 | 88 | $log = $this->reader[6]; 89 | 90 | $this->assertInstanceOf('\DateTime', $log['date']); 91 | $this->assertEquals('extra', $log['logger']); 92 | $this->assertEquals('INFO', $log['level']); 93 | $this->assertEquals('context and extra', $log['message']); 94 | $this->assertArrayHasKey('foo', $log['context'][0]); 95 | $this->assertArrayHasKey('stuff', $log['context'][0]); 96 | $this->assertArrayHasKey('bat', $log['context'][1]); 97 | $this->assertArrayHasKey('weebl', $log['extra'][0]); 98 | $this->assertArrayHasKey('lobob', $log['extra'][1]); 99 | 100 | } 101 | 102 | public function testIterator() 103 | { 104 | $lines = array(); 105 | $keys = array(); 106 | 107 | $this->assertEquals(7, count($this->reader)); 108 | 109 | foreach ($this->reader as $i => $log) { 110 | $test = $this->reader[0]; 111 | $lines[] = $log; 112 | $keys[] = $i; 113 | } 114 | 115 | $this->assertEquals(array(0, 1, 2, 3, 4, 5, 6), $keys); 116 | $this->assertEquals('test', $lines[0]['logger']); 117 | $this->assertEquals('aha', $lines[1]['logger']); 118 | $this->assertEquals('context', $lines[2]['logger']); 119 | $this->assertEquals('context', $lines[3]['logger']); 120 | $this->assertEquals('context', $lines[4]['logger']); 121 | $this->assertEquals('context', $lines[5]['logger']); 122 | $this->assertEquals('extra', $lines[6]['logger']); 123 | 124 | } 125 | 126 | /** 127 | * @expectedException RuntimeException 128 | */ 129 | public function testException() 130 | { 131 | $this->reader[9] = 'foo'; 132 | 133 | } 134 | } 135 | -------------------------------------------------------------------------------- /tests/Dubture/Monolog/Reader/Test/ParserTest.php: -------------------------------------------------------------------------------- 1 | 7 | * 8 | * For the full copyright and license information, please view the LICENSE 9 | * file that was distributed with this source code. 10 | */ 11 | 12 | namespace Dubture\Monolog\Reader\Test; 13 | 14 | use Dubture\Monolog\Parser\LineLogParser; 15 | 16 | /** 17 | * @author Robert Gruendler 18 | */ 19 | class ParserTest extends \PHPUnit_Framework_TestCase 20 | { 21 | private $parser; 22 | 23 | public function setUp() 24 | { 25 | $this->parser = new LineLogParser(); 26 | } 27 | 28 | public function logLineProvider() 29 | { 30 | return array( 31 | 'simple' => array( 32 | 'aha', 33 | 'DEBUG', 34 | 'foobar', 35 | array(), 36 | array(), 37 | '[%s] aha.DEBUG: foobar [] []' 38 | ), 39 | 'default' => array( 40 | 'test', 41 | 'INFO', 42 | 'foobar', 43 | array('foo' => 'bar'), 44 | array(), 45 | '[%s] test.INFO: foobar {"foo":"bar"} []' 46 | ), 47 | 'multi_context' => array( 48 | 'context', 49 | 'INFO', 50 | 'multicontext', 51 | array(array('foo' => 'bar'), array('bat' => 'baz')), 52 | array(), 53 | '[%s] context.INFO: multicontext [{"foo":"bar"},{"bat":"baz"}] []' 54 | ), 55 | 'multi_context_empty' => array( 56 | 'context', 57 | 'INFO', 58 | 'multicontext', 59 | array(array('foo' => 'bar'), array()), 60 | array(), 61 | '[%s] context.INFO: multicontext [{"foo":"bar"},[]] []' 62 | ), 63 | 'multi_context_spaces' => array( 64 | 'context', 65 | 'INFO', 66 | 'multicontext', 67 | array(array('foo' => 'bar', 'stuff' => 'and things'), array('bat' => 'baz')), 68 | array(), 69 | '[%s] context.INFO: multicontext [{"foo":"bar","stuff":"and things"},{"bat":"baz"}] []' 70 | ), 71 | 'multi_context_message_spaces' => array( 72 | 'context', 73 | 'INFO', 74 | 'multicontext with spaces', 75 | array(array('foo' => 'bar', 'stuff' => 'and things'), array('bat' => 'baz')), 76 | array(), 77 | '[%s] context.INFO: multicontext with spaces [{"foo":"bar","stuff":"and things"},{"bat":"baz"}] []' 78 | ), 79 | 'extra_context' => array( 80 | 'extra', 81 | 'INFO', 82 | 'context and extra', 83 | array(array('foo' => 'bar', 'stuff' => 'and things'), array('bat' => 'baz')), 84 | array(array('weebl' => 'bob'), array('lobob' => 'lo')), 85 | '[%s] extra.INFO: context and extra [{"foo":"bar","stuff":"and things"},{"bat":"baz"}] [{"weebl":"bob"},{"lobob":"lo"}]' 86 | ), 87 | ); 88 | } 89 | 90 | public function daysLineProvider() 91 | { 92 | return array( 93 | array ( 94 | 'yesterday', 95 | '[%s] days.DEBUG: %s [] []', 96 | ), 97 | array ( 98 | '2 days ago', 99 | '[%s] days.DEBUG: %s [] []', 100 | ), 101 | array ( 102 | '3 days ago', 103 | '[%s] days.DEBUG: %s [] []', 104 | ), 105 | array ( 106 | '1 week ago', 107 | '[%s] days.DEBUG: %s [] []', 108 | ), 109 | array ( 110 | '1 month ago', 111 | '[%s] days.DEBUG: %s [] []', 112 | ), 113 | array ( 114 | '1 year ago', 115 | '[%s] days.DEBUG: %s [] []', 116 | ), 117 | ); 118 | } 119 | 120 | public function datedLineProvider() 121 | { 122 | $dynamic = $this->daysLineProvider(); 123 | 124 | $array = array( 125 | array ( 126 | false, 127 | '[1970-01-01 00:00:00] dates.DEBUG: epoch [] []' 128 | ), 129 | array ( 130 | false, 131 | '[1955-11-05 19:00:00] dates.DEBUG: flux capacitor [] []' 132 | ) 133 | ); 134 | 135 | return array_merge($dynamic, $array); 136 | } 137 | 138 | /** 139 | * @dataProvider logLineProvider 140 | */ 141 | public function testLineFormatter($logger, $level, $message, $context, $extra, $line) 142 | { 143 | $now = new \DateTime(); 144 | $line = sprintf($line, $now->format('Y-m-d H:i:s')); 145 | 146 | $log = $this->parser->parse($line); 147 | 148 | $this->assertInstanceOf('\DateTime', $log['date']); 149 | $this->assertEquals($logger, $log['logger']); 150 | $this->assertEquals($level, $log['level']); 151 | $this->assertEquals($message, $log['message']); 152 | $this->assertEquals($context, $log['context']); 153 | $this->assertEquals($extra, $log['extra']); 154 | } 155 | 156 | /** 157 | * @dataProvider daysLineProvider 158 | */ 159 | public function testDaysFilter($time, $line) 160 | { 161 | $now = new \DateTime(); 162 | $date = new \DateTime($time); 163 | $line = sprintf($line, $date->format('Y-m-d H:i:s'), $time); 164 | 165 | $days = 1 + $date->diff($now)->days; 166 | $log = $this->parser->parse($line, $days); 167 | 168 | $this->assertNotEmpty($log); 169 | } 170 | 171 | /** 172 | * @dataProvider datedLineProvider 173 | */ 174 | public function testZeroForDaysReturnsAllLines($time, $line) 175 | { 176 | if (false !== $time) { 177 | $date = new \DateTime($time); 178 | $line = sprintf($line, $date->format('Y-m-d H:i:s'), $time); 179 | } 180 | 181 | $log = $this->parser->parse($line, 0); 182 | 183 | $this->assertNotEmpty($log); 184 | } 185 | } 186 | -------------------------------------------------------------------------------- /tests/bootstrap.php: -------------------------------------------------------------------------------- 1 |