承接 mvar/apache2-log-parser 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

mvar/apache2-log-parser

最新稳定版本:v2.1.0

Composer 安装命令:

composer require mvar/apache2-log-parser

包简介

Apache2 access and error logs parser

README 文档

README

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 Apache access log line

<?php

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

use MVar\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));

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\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']);
}

The example above will output:

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

To get more information about iterator please visit mvar/log-iterator documentation.

Date and Time Formatting

By default date and time is returned as is, raw string. You can change this 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

Feel free to make a Pull Request :)

统计信息

  • 总下载量: 182.59k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 33
  • 点击次数: 1
  • 依赖项目数: 2
  • 推荐数: 1

GitHub 信息

  • Stars: 30
  • Watchers: 3
  • Forks: 9
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2014-01-02