[英]possible for PHP to read/parse the current vhosts' Apache VirtualHost config block, to retrieve ErrorLog and CustomLog settings?
[英]Regex to parse CustomLog format (PHP)
我正在嘗試以這種格式解析 CustomLog 格式:
LogFormat "%v %{X-Forwarded-For}i %h %l %u %t \"%r\" %>s %b" MyCustomLog
這是條目的外觀 - 請注意,有一個逗號分隔在 X-Forwarded-For 標頭中傳遞的 IP。
my.server.com 24.24.24.3, 1.2.3.4 1.2.3.5 - - [18/May/2016:02:57:25 -0400] "GET /veer/eye?params=1&are=2&right=3&here=4 HTTP/1.1" 200 146351
我想捕獲以下字段:
我對正則表達式有點生疏 - 至少在我認為我需要使用的負面前瞻的意義上?
幫助表示贊賞!
這是一個更完整的模式,應該適合你。 我更完整地將所有內容分解為一個組的一部分,甚至為這些組添加了名稱。 它與您的問題中的示例和評論中的示例相匹配。
演示: https : //3v4l.org/jMKFL
<?php
$pattern = '/(?P<hostname>[\w\.]+) '
. '(?P<forwardedFor>(?:[\d\.]+, )*(?:[\d\.]+)|-) '
. '(?P<remoteHostname>[\d\.]+) '
. '(?P<remoteLogname>[^\s]+) '
. '(?P<remoteUsername>[^\s]+) '
. '\['
. '(?P<requestDate>[^\]]+)'
. '\] '
. '"'
. '(?P<method>\w+) '
. '(?P<uri>[^\s]+) '
. '(?<httpVersion>[^\"]+)'
. '" '
. '(?P<responseStatus>\d+) '
. '(?P<responseSize>\d+)/';
$test = 'my.server.com 24.24.24.3, 1.2.3.4 1.2.3.5 - - [18/May/2016:02:57:25 -0400] "GET /veer/eye?params=1&are=2&right=3&here=4 HTTP/1.1" 200 146351';
$test2 = 'qa-test.test.com - 80.82.65.120 - - [18/May/2016:00:30:20 -0400] "GET // HTTP/1.1" 404 198';
preg_match($pattern, $test, $matches);
print_r($matches);
preg_match($pattern, $test2, $matches);
print_r($matches);
輸出:
Array
(
[0] => my.server.com 24.24.24.3, 1.2.3.4 1.2.3.5 - - [18/May/2016:02:57:25 -0400] "GET /veer/eye?params=1&are=2&right=3&here=4 HTTP/1.1" 200 146351
[hostname] => my.server.com
[1] => my.server.com
[forwardedFor] => 24.24.24.3, 1.2.3.4
[2] => 24.24.24.3, 1.2.3.4
[remoteHostname] => 1.2.3.5
[3] => 1.2.3.5
[remoteLogname] => -
[4] => -
[remoteUsername] => -
[5] => -
[requestDate] => 18/May/2016:02:57:25 -0400
[6] => 18/May/2016:02:57:25 -0400
[method] => GET
[7] => GET
[uri] => /veer/eye?params=1&are=2&right=3&here=4
[8] => /veer/eye?params=1&are=2&right=3&here=4
[httpVersion] => HTTP/1.1
[9] => HTTP/1.1
[responseStatus] => 200
[10] => 200
[responseSize] => 146351
[11] => 146351
)
Array
(
[0] => test.test.com - 80.82.65.120 - - [18/May/2016:00:30:20 -0400] "GET // HTTP/1.1" 404 198
[hostname] => test.test.com
[1] => test.test.com
[forwardedFor] => -
[2] => -
[remoteHostname] => 80.82.65.120
[3] => 80.82.65.120
[remoteLogname] => -
[4] => -
[remoteUsername] => -
[5] => -
[requestDate] => 18/May/2016:00:30:20 -0400
[6] => 18/May/2016:00:30:20 -0400
[method] => GET
[7] => GET
[uri] => //
[8] => //
[httpVersion] => HTTP/1.1
[9] => HTTP/1.1
[responseStatus] => 404
[10] => 404
[responseSize] => 198
[11] => 198
)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.