PHPackages                             huid/nginx-log-parser - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Logging &amp; Monitoring](/categories/logging)
4. /
5. huid/nginx-log-parser

ActiveLibrary[Logging &amp; Monitoring](/categories/logging)

huid/nginx-log-parser
=====================

nginx log parser by php

v2.0(3y ago)328MITPHPPHP &gt;=8.0

Since Dec 30Pushed 2y ago1 watchersCompare

[ Source](https://github.com/zhanghuid/nginx-log-parser)[ Packagist](https://packagist.org/packages/huid/nginx-log-parser)[ Docs](http://github.com/kassner/log-parser)[ RSS](/packages/huid-nginx-log-parser/feed)WikiDiscussions main Synced 1mo ago

READMEChangelogDependencies (5)Versions (3)Used By (0)

nginx-log-parser
================

[](#nginx-log-parser)

安装
--

[](#安装)

1. php &gt;= 7.1

```
composer require huid/nginx-log-parser:~1.0
```

2. php &gt;= 8.0

```
composer require huid/nginx-log-parser:~2.0
```

使用
--

[](#使用)

1. 使用 nginx.conf 文件

```
$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);
}
```

2. 使用 nginx.conf log\_format 内容

```
$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);
}
```

自定义
---

[](#自定义)

### 多段 log\_format 的情况

[](#多段-log_format-的情况)

1. nginx conf

```
http {

    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent [$request_time]  "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    log_format  err  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent [$request_time]  "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';
		      #'resp_body:"$resp_body" resp_header:"$resp_header"';
}
```

2. 获取指定 log\_format

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

### 使用自定义的字段解析器

[](#使用自定义的字段解析器)

1. 定义工厂类

```
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
    }
}
```

2. 使用自定义的工厂类

```
$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" "-"
');
```

参考
--

[](#参考)

[kassner/log-parser](https://github.com/kassner/log-parser)

###  Health Score

25

—

LowBetter than 37% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity10

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity52

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~0 days

Total

2

Last Release

1227d ago

Major Versions

v1.0 → v2.02022-12-30

PHP version history (2 changes)v1.0PHP &gt;=7.1

v2.0PHP &gt;=8.0

### Community

Maintainers

![](https://www.gravatar.com/avatar/31cd7e8d836d375795c41137e75a773ff2ca63b8c28ebab802860b5bdeb3b966?d=identicon)[huid](/maintainers/huid)

---

Top Contributors

[![zhanghuid](https://avatars.githubusercontent.com/u/12711819?v=4)](https://github.com/zhanghuid "zhanghuid (4 commits)")

---

Tags

logparserformatnginxlog-format

###  Code Quality

TestsPHPUnit

Static AnalysisPsalm

Code StylePHP CS Fixer

Type Coverage Yes

### Embed Badge

![Health badge](/badges/huid-nginx-log-parser/health.svg)

```
[![Health](https://phpackages.com/badges/huid-nginx-log-parser/health.svg)](https://phpackages.com/packages/huid-nginx-log-parser)
```

###  Alternatives

[kassner/log-parser

PHP Log Parser Library

342432.5k6](/packages/kassner-log-parser)[monolog/monolog

Sends your logs to files, sockets, inboxes, databases and various web services

21.4k964.9M7.0k](/packages/monolog-monolog)[psr/log

Common interface for logging libraries

10.4k1.2B9.2k](/packages/psr-log)[symfony/monolog-bundle

Symfony MonologBundle

2.9k249.1M1.6k](/packages/symfony-monolog-bundle)[spatie/laravel-activitylog

A very simple activity logger to monitor the users of your website or application

5.8k45.4M309](/packages/spatie-laravel-activitylog)[sentry/sentry

PHP SDK for Sentry (http://sentry.io)

1.9k227.1M273](/packages/sentry-sentry)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
