按线程分解log4j格式日志脚本

<?php


function readThreadName($line){
    $start=strpos($line,' [');
    if($start!=false){
        $end=strpos($line,'] ',$start);
        return substr($line,$start+2,$end-$start-2);
    }else{
        return '';
    }
}
function putLine2File($content,$fileName){
    global $fileRese;
    $fileName=trim($fileName);
    if(isset($fileRese[$fileName])){
        //echo 'setted';
       $res=$fileRese[$fileName];
    }else{
        //echo 'added';
        $res=fopen('threads/'.$fileName,'a+');
        $fileRese[$fileName]=$res;
    }
    var_dump($fileName);
    var_dump($res);
    echo 'write:'+fwrite($res,$content."\n");
}

$lines=file('search.log');
$preLineThread='';
$fileReses=array();
$num=0;
foreach($lines as $line){
    echo $num++;
    $thread=readThreadName($line);
    if(empty($thread)){
        $thread=$preLineThread;
    }
    putLine2File($line,$thread);
    $preLineThread=$thread;
    //break;
}

?>

 

你可能感兴趣的:(log4j)