laravel 日志分析

<?php

$logs = '/\[\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}([\+-]\d{4})?\].*/';

$current_log = [
            '/^\[(\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}([\+-]\d{4})?)\](?:.*?(\w+)\.|.*?)',
            ': (.*?)( in .*?:[0-9]+)?$/i'
        ];

$log_day=date("Y-m-d");
$file="/mnt/hgfs/CentOS7/cdn.ikuai8.com/storage/logs/laravel-2020-05-20.log";
$content=file_get_contents($file);
preg_match_all($logs, $content, $headings);



//$log_data = preg_split($logs, $content);

$levels_imgs = [
    'debug' => 'info-circle',
    'info' => 'info-circle',
    'notice' => 'info-circle',
    'warning' => 'exclamation-triangle',
    'error' => 'exclamation-triangle',
    'critical' => 'exclamation-triangle',
    'alert' => 'exclamation-triangle',
    'emergency' => 'exclamation-triangle',
    'processed' => 'info-circle',
    'failed' => 'exclamation-triangle'
];


$log_info=[];
foreach ($headings as $h) {
    for ($i = 0, $j = count($h); $i < $j; $i++) {

        foreach (array_keys($levels_imgs) as $level) {
            if (strpos(strtolower($h[$i]), '.' . $level) || strpos(strtolower($h[$i]), $level . ':')) {

                preg_match($current_log[0] . $level . $current_log[1], $h[$i], $current);
                if (!isset($current[4])) {
                    continue;
                }

                $log_info[] = array(
                    'context' => $current[3],
                    'level' => $level,
                    
                    'date' => $current[1],
                    'text' => $current[4],
                    'in_file' => isset($current[5]) ? $current[5] : null,
                );
            }
        }
    }
}
array_reverse($log_info);

$error_num=5;
$current_error_num=0;
foreach($log_info as $log_val){
    if($current_error_num<$error_num){
       echo "-----------------------------------------------------\n";
        foreach($log_val as $_key=>$_val){
            echo "[$_key]:\t".$_val."\n";
        }
        $current_error_num+=1;
    }
}

猜你喜欢

转载自www.cnblogs.com/saonian/p/12923076.html