ritey / nginx-log-analyzer
使用php分析/解析Nginx日志
dev-master
2022-08-12 09:46 UTC
Requires
- php: ^7.3|^8.0
Requires (Dev)
- doctrine/coding-standard: ^6.0
- phpstan/extension-installer: ^1.0
- phpstan/phpstan: ^0.11.19
- phpstan/phpstan-phpunit: ^0.11.2
- phpunit/phpunit: ^8.4
- roave/security-advisories: dev-master
- spatie/phpunit-watcher: ^1.21
- vimeo/psalm: ^3.6
This package is not auto-updated.
Last update: 2024-09-21 17:47:23 UTC
README
安装
composer require ritey/nginx-log-analyzer
使用
<?php declare(strict_types=1); use Ritey\NginxLogAnalyzer\Parse; use Ritey\NginxLogAnalyzer\NginxAccessLogFormat; use Ritey\NginxLogAnalyzer\RegexPattern; $file = new SplFileObject('access.log'); $line = $file->fgets(); $parse = new Parse(new NginxAccessLogFormat(), new RegexPattern()); $information = $parse->line($line);
var_dump($information)
class stdClass#16 (8) {
public $remote_addr =>
string(13) "182.76.202.33"
public $remote_user =>
string(1) "-"
public $time_local =>
string(26) "21/Oct/2019:06:52:00 +0000"
public $request =>
string(14) "GET / HTTP/1.1"
public $status =>
string(3) "404"
public $bytes_sent =>
string(3) "580"
public $http_referer =>
string(1) "-"
public $http_user_agent =>
string(110) "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.103 Safari/537.36"
}
配置
基本上,Parse
需要两个实现
Format
接口。有一个getStringRepresentation() : string
方法。Pattern
接口。有build(Format $format) : string
和getIdentifiers() : array
方法。
库已经内置了对默认nginx access.log格式的支持,该格式为
$remote_addr - $remote_user [$time_local] "$request" $status $bytes_sent "$http_referer" "$http_user_agent"
许可证
MIT许可证(MIT)。请参阅许可证文件获取更多信息。