|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace SkylerKatz\RackspaceLogParser; |
|
4
|
|
|
|
|
5
|
|
|
class RackspaceLogParser |
|
6
|
|
|
{ |
|
7
|
|
|
/** |
|
8
|
|
|
* Rexeg Pattern to match Rackspace Cloud Files Logs. |
|
9
|
|
|
*/ |
|
10
|
|
|
const PATTERN = '/^([^ ]+) ([^ ]+) ([^ ]+) (\[[^\]]+\]) "(.*) (.*) (.*)" ([0-9\-]+) ([0-9\-]+) "(.*)" "(.*)/m'; |
|
11
|
|
|
|
|
12
|
|
|
/** |
|
13
|
|
|
* Max Buffer size for reading log file. |
|
14
|
|
|
*/ |
|
15
|
|
|
const BUFFER_SIZE = 4096; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* The path to the log file. |
|
19
|
|
|
* |
|
20
|
|
|
* @var string |
|
21
|
|
|
*/ |
|
22
|
|
|
protected $path; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* The log file in memory. |
|
26
|
|
|
* |
|
27
|
|
|
* @var string |
|
28
|
|
|
*/ |
|
29
|
|
|
protected $log; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* Create a new RackspaceLogParser Instance. |
|
33
|
|
|
* |
|
34
|
|
|
* @param string $path |
|
35
|
|
|
*/ |
|
36
|
|
|
public function __construct($path) |
|
37
|
|
|
{ |
|
38
|
|
|
$this->path = $path; |
|
39
|
|
|
$this->processLog(); |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* Parse a log file into an arrray of LogItems. |
|
44
|
|
|
* |
|
45
|
|
|
* @return array |
|
46
|
|
|
*/ |
|
47
|
|
|
public function parse() |
|
48
|
|
|
{ |
|
49
|
|
|
$entries = []; |
|
50
|
|
|
|
|
51
|
|
|
foreach ($this->getLogRows() as $row) { |
|
52
|
|
|
$entry = $this->parseRow($row); |
|
53
|
|
|
$entries[] = $this->createLogItem($entry); |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
return $entries; |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* read a log file into memory. |
|
61
|
|
|
* |
|
62
|
|
|
* @return void |
|
63
|
|
|
*/ |
|
64
|
|
|
private function processLog() |
|
65
|
|
|
{ |
|
66
|
|
|
// Open our file in binary mode |
|
67
|
|
|
$file = gzopen($this->path, 'rb'); |
|
68
|
|
|
|
|
69
|
|
|
// Keep repeating until the end of the input file |
|
70
|
|
|
$log = ''; |
|
71
|
|
|
while (! gzeof($file)) { |
|
72
|
|
|
$log .= gzread($file, self::BUFFER_SIZE); |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
// File is done, close files |
|
76
|
|
|
gzclose($file); |
|
77
|
|
|
|
|
78
|
|
|
$this->log = $log; |
|
79
|
|
|
|
|
80
|
|
|
return $this; |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* Get the rows of a log. |
|
85
|
|
|
* |
|
86
|
|
|
* @return array |
|
87
|
|
|
*/ |
|
88
|
|
|
private function getLogRows() |
|
89
|
|
|
{ |
|
90
|
|
|
return array_filter(explode(PHP_EOL, $this->log)); |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
/** |
|
94
|
|
|
* Parse a row into its component parts. |
|
95
|
|
|
* |
|
96
|
|
|
* @param string $row |
|
97
|
|
|
* @return array |
|
98
|
|
|
*/ |
|
99
|
|
|
private function parseRow($row) |
|
100
|
|
|
{ |
|
101
|
|
|
preg_match(self::PATTERN, $row, $entry); |
|
102
|
|
|
|
|
103
|
|
|
return $entry; |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
/** |
|
107
|
|
|
* Create a Log Item from a parsed log row. |
|
108
|
|
|
* |
|
109
|
|
|
* @param array $entry |
|
110
|
|
|
* @return LogItem |
|
111
|
|
|
*/ |
|
112
|
|
|
private function createLogItem($entry) |
|
113
|
|
|
{ |
|
114
|
|
|
$logItem = new LogItem(); |
|
115
|
|
|
$logItem->raw = $entry[0]; |
|
116
|
|
|
$logItem->clientIp = $entry[1]; |
|
117
|
|
|
$logItem->accessDate = $this->parseDate($entry[4]); |
|
|
|
|
|
|
118
|
|
|
$logItem->method = $entry[5]; |
|
119
|
|
|
$logItem->request = $entry[6]; |
|
120
|
|
|
$logItem->httpVersion = $entry[7]; |
|
121
|
|
|
$logItem->returnCode = intval($entry[8]); |
|
122
|
|
|
$logItem->bytesSent = intval($entry[9]); |
|
123
|
|
|
|
|
124
|
|
|
$logItem->referrer = $entry[10] == '-' ? null : $entry[10]; |
|
125
|
|
|
$logItem->userAgent = $entry[11]; |
|
126
|
|
|
|
|
127
|
|
|
return $logItem; |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
|
/** |
|
131
|
|
|
* Parse a log date. |
|
132
|
|
|
* |
|
133
|
|
|
* @param string $date |
|
134
|
|
|
* @return \Carbon\Carbon |
|
135
|
|
|
*/ |
|
136
|
|
|
private function parseDate($date) |
|
137
|
|
|
{ |
|
138
|
|
|
$accessDate = str_replace('[', '', $date); |
|
139
|
|
|
$accessDate = str_replace(']', '', $accessDate); |
|
140
|
|
|
$accessDate = explode(' ', $accessDate); |
|
141
|
|
|
|
|
142
|
|
|
return \Carbon\Carbon::createFromFormat('d/m/Y:H:i:s', $accessDate[0], new \DateTimeZone('UTC')); |
|
143
|
|
|
} |
|
144
|
|
|
} |
|
145
|
|
|
|
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..