Napiště "univerzální" program, který přečte libovolně dlouhý textový soubor. Řádek po řádku bude aplikovat uživatelské filtry a dekorátory. Výstupem programu bude počet stejných (upravených) řádků a jejich četností.
Použijte co nejvíce vlastností moderního PHP. Doporučení:
- Iterables
- Anonymous functions, especially Callables
- Types
- And more
Upravte program tak, aby vypisoval průběžný stav nekonečného streamu.
Bude zveřejněno během workshopu ve středu 28.3.2018. Přijďte :-)
php old.php example.log[2018-03-13 12:16:10] test.DEBUG: Test message [] []
[2018-03-13 12:16:10] test.ERROR: Test message [] []
[2018-03-13 12:16:10] test.WARNING: Test message [] []
[2018-03-13 12:16:10] test.WARNING: Test message [] []
[2018-03-13 12:16:10] test.INFO: Test message [] []
[2018-03-13 12:16:10] test.NOTICE: Test message [] []
[2018-03-13 12:16:10] test.EMERGENCY: Test message [] []
[2018-03-13 12:16:10] test.ALERT: Test message [] []
[2018-03-13 12:16:10] test.ERROR: Test message [] []
[2018-03-13 12:16:10] test.NOTICE: Test message [] []
error: 2
warning: 2
notice: 2
info: 1
emergency: 1
alert: 1
<?php$pattern = '/test\.(\w+)/';
// read and parse file$log = file_get_contents($argv[1]);
$rows = explode(PHP_EOL, $log);
// build stats$stats = array();
foreach ($rowsas$row) {
// decorator: extract log levelif (preg_match($pattern, $row, $matches)) {
$level = strtolower($matches[1]);
// filter: do not accept DEBUGif ($level != 'debug') {
if (array_key_exists($level, $stats)) {
$stats[$level]++;
} else {
$stats[$level] = 1;
}
}
}
}
// show statsarsort($stats);
foreach ($statsas$level => $count) {
echo"$level: $count" . PHP_EOL;
}