mvar / log-parser
Universal log parser
Installs: 99 463
Dependents: 1
Suggesters: 0
Security: 0
Stars: 17
Watchers: 2
Forks: 8
Open Issues: 1
Requires
- php: >=5.4
Requires (Dev)
- phpunit/phpunit: ~4.8
Suggests
- mvar/apache2-log-parser: Allows to easily parse Apache web server log files
This package is not auto-updated.
Last update: 2024-10-26 19:27:12 UTC
README
This library allows you to easily iterate over your Apache, nginx or any other web server log files.
Main features:
- Log file iterator
- Parser abstraction to help you implement your custom parser
- Low memory footprint even with huge files
Installation
This library can be found on Packagist. The recommended way to install this is through Composer:
composer require mvar/log-parser:^1.0
Basic Usage
Lets say you have log file my.log
with following content:
GET /favicon.ico 200
GET /about 404
All you need to do to iterate over the file is to initialize SimpleParser
with your regular expression and pass it to LogIterator
:
<?php require __DIR__ . '/vendor/autoload.php'; use MVar\LogParser\LogIterator; use MVar\LogParser\SimpleParser; // Pass your regular expression $parser = new SimpleParser('/(?<method>\S+)\s+(?<path>\S+)\s+(?<response_code>\d+)/'); foreach (new LogIterator('my.log', $parser) as $data) { var_export($data); echo "\n"; }
The above example will output:
array ( 'method' => 'GET', 'path' => '/favicon.ico', 'response_code' => '200', ) array ( 'method' => 'GET', 'path' => '/about', 'response_code' => '404', )
It is also possible to parse compressed files by adding stream wrapper before file name:
$logFile = 'compress.zlib://file:///path/to/log.gz';
How To
Implemented Parsers
- mvar/apache2-log-parser - Apache access/error log parser based on this library
License
This package is licensed under the MIT License. See the LICENSE file for details.