forked from peoplepath/homework-modern-php
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnewReader.php
More file actions
72 lines (59 loc) · 1.81 KB
/
newReader.php
File metadata and controls
72 lines (59 loc) · 1.81 KB
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
<?php
include_once "LogCounter.php";
include_once "UserFilter.php";
const TIME_REPORT_DELAY = 0.1;
// read filename from arguments or use default name
if (isset($argv) && array_key_exists(1, $argv)) {
$logFile = $argv[1];
} else {
$logFile = "example.log";
}
/**
* Callback function used for time limited output
* @param $data array with level=>count data
*/
function echoProgress(array $data)
{
global $time;
if (!isset($time) || microtime(true) >= $time + TIME_REPORT_DELAY) { // limits display frequency
$time = microtime(true);
// Print sum to console
printSumToConsole($data);
}
}
/** Prints progress to console in form of changing sum
* @param array $data with level=>count data
*/
function printSumToConsole(array $data): void
{
$total_count = 0;
foreach ($data as $count) {
$total_count += $count;
}
echo("\rZaznamu vyhodnoceno: " . $total_count);
}
// Create LogCounter instance
$logCounter = new LogCounter($logFile);
$logCounter->setWatcher("echoProgress");
// Demonstration of Ignore filter
$ignoreAlerts = new UserFilter();
$ignoreAlerts->setIgnorePattern('/test\.ALERT/');
// Demonstration of Replacement filter
$countNoticeTestMessageAsInfo = new UserFilter();
$countNoticeTestMessageAsInfo->setReplacePattern('(NOTICE: Test message)', "INFO");
// Add filters to LogCounter
$logCounter->addUserFilter($ignoreAlerts);
$logCounter->addUserFilter($countNoticeTestMessageAsInfo);
// Run counter
try {
$data = $logCounter->readFile();
// Override progress with final value
printSumToConsole($data);
echo "\n-----------" . PHP_EOL;
// Display final statistic
foreach ($data as $level => $count) {
echo "$level: $count" . PHP_EOL;
}
} catch (Exception $e) {
echo("Error reading log file. " . $e->getMessage());
}