-
Notifications
You must be signed in to change notification settings - Fork 0
/
lib-53.php
151 lines (134 loc) · 4.78 KB
/
lib-53.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
<?php
class PhpLogParser53
{
public function __construct(
$logPath,
$temporaryDir,
\Closure $deliverErrorsCallback,
$pollDelay = 2,
$keepLogDir = null
) {
$this->logPath = $logPath;
$this->temporaryDir = $temporaryDir;
$this->pollDelay = $pollDelay * 1000000; //to milliseconds
$this->deliverErrorsCallback = $deliverErrorsCallback;
$this->keepLogDir = $keepLogDir;
}
public function start() {
try {
$this->processOldLogFiles();
$this->loopOnNewFile();
} catch (\Exception $e) {
$this->log('ERROR ' . $e->getMessage());
};
}
protected function processOldLogFiles()
{
self::log('process old log files');
$this->prepareDir($this->temporaryDir);
$it = new FilesystemIterator($this->temporaryDir);
/** @var SplFileInfo $fileInfo */
foreach ($it as $fileInfo) {
$filepath = $fileInfo->getPathname();
$content = $this->readFile($filepath);
$errors = $this->parseLog($content);
$this->sendErrors($errors);
$this->unlinkProcessedLog($fileInfo->getFilename());
}
}
protected function loopOnNewFile()
{
self::log('loop on new file');
while (true) {
if (is_file($this->logPath)) {
if (!is_readable($this->logPath)) {
throw new \Exception('log file is not readable:' . $this->logPath);
}
if (filesize($this->logPath)) {
$newName = date('Y-m-d_H-i-s');
$newPath = $this->temporaryDir . '/' . $newName;
$this->rename($this->logPath, $this->temporaryDir, $newName);
$content = $this->readFile($newPath);
$errors = $this->parseLog($content);
unset($content);
$this->sendErrors($errors);
unset($errors);
$this->unlinkProcessedLog($newName);
self::log('memory usage ' . (memory_get_usage(true)/1024) . 'K');
}
}
usleep($this->pollDelay);
}
}
protected function prepareDir($dir)
{
if (is_dir($dir)) {
if (!is_readable($dir)) {
throw new \Exception('Directory does not exists or not readable:' . $dir);
}
} elseif (!mkdir($dir)) {
throw new \Exception('failed to create directory:' . $dir);
}
}
protected function readFile($filename)
{
if (!is_file($filename) || !is_readable($filename)) {
throw new \Exception('file does not exists or is not readable:' . $filename);
}
self::log('read file ' . $filename);
$result = file_get_contents($filename);
if (false === $result) {
throw new \Exception('file_get_contents failed:' . $filename);
}
return $result;
}
protected function unlinkProcessedLog($filename)
{
if ($this->keepLogDir) {
$this->prepareDir($this->keepLogDir);
$this->rename($this->temporaryDir . '/' . $filename, $this->keepLogDir, $filename);
} else {
if (@unlink($this->temporaryDir . '/' . $filename)) {
self::log('file deleted successfully: ' . $filename);
} else {
throw new \Exception('unlink failed: ' . $filename);
}
}
}
protected function rename($pathFrom, $dirTo, $nameTo)
{
$this->prepareDir($dirTo);
$to = $dirTo . '/' . $nameTo;
if (@rename($pathFrom, $to)) {
self::log("rename success: {$pathFrom} -> {$to}");
} else {
throw new \Exception("rename failed: {$pathFrom} -> {$to}");
}
}
protected function parseLog($content)
{
$pattern = '/\[(\d\d-\w{3}-\d{4}\s+\d\d:\d\d:\d\d\ [\w\/]*?)] /';
$flags = PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY;
$matches = preg_split($pattern, $content, -1, $flags);
$errorsOutput = array();
while (($msg = array_pop($matches)) && ($time = array_pop($matches))) {
$time = strtotime($time);
$errorsOutput[] = array($time, $msg);
}
self::log('log parsed, errors found: ' . count($errorsOutput));
return $errorsOutput;
}
protected function sendErrors(array $errors)
{
$callback = $this->deliverErrorsCallback;
if ($callback($errors)) {
self::log('errors inserted successfully');
} else {
throw new \Exception('deliver errors failed');
}
}
public static function log($message)
{
echo date('[Y-m-d H:i:s]') . ' ' . $message . PHP_EOL;
}
}