PHP code example of huid / nginx-log-parser

1. Go to this page and download the library: Download huid/nginx-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/ */

    

huid / nginx-log-parser example snippets



$parser = \Huid\NginxLogParser\LogParser::createFromFilepath('your-nginx-conf-path/nginx.conf');
$lines = file('/var/log/nginx/access.log', FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
foreach ($lines as $line) {
    $data = $parser->parse($line);
}

$parser = \Huid\NginxLogParser\LogParser::createFromContent('
$remote_addr - $remote_user [$time_local] "$request"
                      $status $body_bytes_sent [$request_time]  "$http_referer"
                      "$http_user_agent" "$http_x_forwarded_for";
');
$lines = file('/var/log/nginx/access.log', FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
foreach ($lines as $line) {
    $data = $parser->parse($line);
}

$parser = \Huid\NginxLogParser\LogParser::createFromFilepath('your-nginx-conf-path/nginx.conf');
$parser->setName('err');

class MyEntry implements Huid\NginxLogParser\LogEntryInterface
{
}

class MyEntryFactory implements \Huid\NginxLogParser\LogEntryFactoryInterface
{
    public function create(array $data): \Huid\NginxLogParser\LogEntryInterface
    {
        // @TODO implement your code here to return a instance of MyEntry
    }
}

$factory = new MyEntryFactory();
$parser = Huid\NginxLogParser\LogParser::createFromFilepath('your-nginx-conf-path/nginx.conf');
$parser->setFactory($factory);
$entry = $parser->parse('172.16.16.50 - - [02/Feb/2020:17:10:04 +0800] "GET /xxx/xxxx/xxxxx?xxx=day HTTP/1.1" 200 12416 [0.347]  "http://www.baidu.com/" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.130 Safari/537.36" "-"
');