php 简单日志搜索

<?php

/*
	功能 : 实现从压缩日志中找出相关的信息
	要求 : php要安装相关的压缩扩展
	
*/

$dateTime = new \DateTime();
$dateInterval = \DateInterval::createFromDateString('-1 day');
$datePeriod =  new \DatePeriod($dateTime,$dateInterval,30);

$file_path = '/var/log/nginx/';
$file_prefix = "error.log";
$file_surfix = "gz";
$search_string  = "172.16.149.1";


// 时间要转换的.
foreach ($datePeriod as $key => $value) {
		$file = $file_path.$file_prefix."-".$value->format('Ymd').".".$file_surfix;	

		if(file_exists($file))
		{
			$handler = fopen($file,"rb");
			stream_filter_append($handler,'zlib.inflate');
			while(feof($handler) !== true)
			{
				$line = fgets($handler);
				if($line === false)
				{
					continue;
				}
				// 定位日之内中的需要的内容.
				if(strpos($line,$search_string) !== false)
				{
					fwrite(STDOUT,$line);
				}
			}
			fclose($handler);	
		}	
}


?>

猜你喜欢

转载自my.oschina.net/u/1579560/blog/1590306