1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace PiedWeb\LogsAnalyzer; |
4
|
|
|
|
5
|
|
|
class LogParser extends \Kassner\LogParser\LogParser |
6
|
|
|
{ |
7
|
|
|
protected $domain; |
8
|
|
|
protected $type; |
9
|
|
|
|
10
|
|
|
// To be removed when this pull request is accepted : https://github.com/kassner/log-parser/pull/38 |
11
|
|
|
protected $patterns = array( |
12
|
|
|
'%%' => '(?P<percent>\%)', |
13
|
|
|
'%a' => '(?P<remoteIp>)', |
14
|
|
|
'%A' => '(?P<localIp>)', |
15
|
|
|
'%h' => '(?P<host>[a-zA-Z0-9\-\._:]+)', |
16
|
|
|
'%l' => '(?P<logname>(?:-|[\w-]+))', |
17
|
|
|
'%m' => '(?P<requestMethod>OPTIONS|GET|HEAD|POST|PUT|DELETE|TRACE|CONNECT|PATCH|PROPFIND)', |
18
|
|
|
'%H' => '(?P<requestProtocol>HTTP/(1\.0|1\.1|2\.0))', |
19
|
|
|
'%p' => '(?P<port>\d+)', |
20
|
|
|
'%r' => '(?P<request>(?:(?:[A-Z]+) .+? HTTP/(1\.0|1\.1|2\.0))|-|)', |
21
|
|
|
'%t' => '\[(?P<time>\d{2}/(?:Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)/\d{4}:\d{2}:\d{2}:\d{2} '. |
22
|
|
|
'(?:-|\+)\d{4})\]', |
23
|
|
|
'%u' => '(?P<user>(?:-|[\w\-\.]+))', |
24
|
|
|
'%U' => '(?P<URL>.+?)', |
25
|
|
|
'%v' => '(?P<serverName>([a-zA-Z0-9]+)([a-z0-9.-]*))', |
26
|
|
|
'%V' => '(?P<canonicalServerName>([a-zA-Z0-9]+)([a-z0-9.-]*))', |
27
|
|
|
'%>s' => '(?P<status>\d{3}|-)', |
28
|
|
|
'%b' => '(?P<responseBytes>(\d+|-))', |
29
|
|
|
'%T' => '(?P<requestTime>(\d+\.?\d*))', |
30
|
|
|
'%O' => '(?P<sentBytes>[0-9]+)', |
31
|
|
|
'%I' => '(?P<receivedBytes>[0-9]+)', |
32
|
|
|
'\%\{(?P<name>[a-zA-Z]+)(?P<name2>[-]?)(?P<name3>[a-zA-Z]+)\}i' => '(?P<Header\\1\\3>.*?)', |
33
|
|
|
'%D' => '(?P<timeServeRequest>[0-9]+)', |
34
|
|
|
'%S' => '(?P<scheme>http|https)', |
35
|
|
|
); |
36
|
|
|
|
37
|
|
|
public function setDomain(string $domain) |
38
|
|
|
{ |
39
|
|
|
$this->domain = $domain; |
40
|
|
|
} |
41
|
|
|
|
42
|
3 |
|
public function setType(string $type) |
43
|
|
|
{ |
44
|
3 |
|
$this->type = $type; |
45
|
3 |
|
} |
46
|
|
|
|
47
|
3 |
|
public function parse($line) |
48
|
|
|
{ |
49
|
|
|
//var_dump($this->pcreFormat); die(); |
50
|
3 |
|
if (!preg_match($this->pcreFormat, $line, $matches)) { |
51
|
|
|
throw new \Kassner\LogParser\FormatException($line); |
52
|
|
|
} |
53
|
|
|
|
54
|
3 |
|
$entry = $this->createEntry(); |
55
|
3 |
|
foreach (array_filter(array_keys($matches), 'is_string') as $key) { |
56
|
3 |
|
$setter = 'set'.$key; |
57
|
3 |
|
if (method_exists($entry, $setter)) { |
58
|
3 |
|
$entry->$setter($matches[$key]); |
59
|
|
|
} |
60
|
|
|
} |
61
|
|
|
|
62
|
3 |
|
return $entry; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* @return LogLine |
67
|
|
|
*/ |
68
|
3 |
|
protected function createEntry() |
69
|
|
|
{ |
70
|
3 |
|
return null === $this->type ? new LogLine($this->domain) : new $this->type($this->domain); |
71
|
|
|
} |
72
|
|
|
} |
73
|
|
|
|