Skip to content

skynet/apache2-log-parser

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Apache2 access and error logs parser

Latest Stable Version Build Status Code Coverage Scrutinizer Quality Score

Installation

This library can be found on Packagist. The recommended way to install this is through Composer:

composer require mvar/apache2-log-parser:dev-master

Features

  • Apache2 log lines parsing
    • Access log
    • Error log (currently, for Apache 2.2 and older)
  • Log files iterator
  • Low memory footprint even with huge files

Usage

Parsing single Apache2 access log line

<?php

require __DIR__ . '/vendor/autoload.php';

use MVar\Apache2LogParser\AccessLogParser;

// Format can be any of predefined `AccessLogParser::FORMAT_*` constants or custom string
$parser = new AccessLogParser(AccessLogParser::FORMAT_COMBINED);

// String which you want to parse
$line = '66.249.78.230 - - [29/Dec/2013:16:07:58 +0200] "GET /my-page/ HTTP/1.1" 200 2490 "-" ' .
    '"Mozilla/5.0 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)"';

var_export($parser->parseLine($line));

The above example will output:

array (
  'remote_host' => '66.249.78.230',
  'identity' => '-',
  'remote_user' => '-',
  'time' => '29/Dec/2013:16:07:58 +0200',
  'request_line' => 'GET /my-page/ HTTP/1.1',
  'response_code' => '200',
  'bytes_sent' => '2490',
  'request' =>
  array (
    'method' => 'GET',
    'path' => '/my-page/',
    'protocol' => 'HTTP/1.1',
  ),
  'request_headers' =>
  array (
    'Referer' => '-',
    'User-Agent' => 'Mozilla/5.0 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)',
  ),
)

Iterate through Apache log file

Log iterator reads log file line by line. This means that it is possible to parse huge files with low memory usage.

Let's say we have Apache log file access.log with following content:

192.168.25.1 - - [25/Jun/2012:14:26:05 -0700] "GET /favicon.ico HTTP/1.1" 404 498
192.168.25.1 - - [25/Jun/2012:14:26:05 -0700] "GET /icons/blank.gif HTTP/1.1" 200 438

To parse whole log file line by line it needs only to create new iterator with file name and parser arguments:

<?php

require __DIR__ . '/vendor/autoload.php';

use MVar\Apache2LogParser\AccessLogParser;
use MVar\Apache2LogParser\LogIterator;

$parser = new AccessLogParser(AccessLogParser::FORMAT_COMMON);

foreach (new LogIterator('access.log', $parser) as $line => $data) {
    printf("%s %s\n", $data['request']['method'], $data['request']['path']);
}

The above example will output:

GET /favicon.ico
GET /icons/blank.gif

It is also possible to parse compressed files by adding stream wrapper before file name:

$logFile = 'compress.zlib://file:///path/to/log.gz';

Date and Time Formatting

By default date and time is returned as is, raw string. You can change that behaviour in two ways. First, set custom format string and formatted date string will be returned. Second, set time format to true and you will get \DateTime object.

$parser = new AccessLogParser(AccessLogParser::FORMAT_COMMON);

// Set custom date and time format accepted by date()
$parser->setTimeFormat('Y-m-d H:i:s');

// Set TRUE and you will get \DateTime object
$parser->setTimeFormat(true);

TODO for future releases

  • Modifiers support
  • Custom time format support
  • PHP stack trace collector (few error log lines can be aggregated as single PHP error)

Feel free to make a Pull Request :)

About

Apache2 access and error logs parser

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • PHP 100.0%