PHP code example of mvar / apache2-log-parser

1. Go to this page and download the library: Download mvar/apache2-log-parser library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

mvar / apache2-log-parser example snippets




Var\Apache2LogParser\AccessLogParser;

// Access log format from your Apache configuration
// It can be any of predefined `AccessLogParser::FORMAT_*` constants or custom string
$parser = new AccessLogParser('%h %l %u %t "%r" %>s %O "%{Referer}i" "%{User-Agent}i"');

// 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));

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)',
  ),
)



Var\Apache2LogParser\AccessLogParser;
use MVar\LogParser\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']);
}
       
$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);