<?php
/**
* **************************************************************************************
* Description : 自动化执行 命令行模式
* monolog,Thriftlog,Phplog,Accesslog,php-fpm,nginx
* yuyuecall,lunciyuyue,reqyuyue,vrayrender,houselog
*
* 导入到 ES
* Filename : AlllogCommand.php
* Create time : 2015-06-27
* Last modified : 2015-06-30
* update : bfire.lai
* $getNum :第个文件读取行数 832行
* **************************************************************************************
*/
class AlllogCommand
{
/*@var _host*/
protected $_host = '';
/*@var _port*/
protected $_port = '';
/*@var _index*/
protected $_index = '';
/*@var _type*/
protected $_type = '';
/*@var _root*/
protected $_root = '';
/*@var _curl*/
protected $_curl;
/*@var _logdir */
protected $_logdir = array();
/*@var _logdir*/
protected $_logflag = 0;
/* 程序执行入口*/
public function run()
{
//网站根目录
if (!defined('ROOT')) {
define('ROOT', dirname(__FILE__));
//目前root 只显示到 D:\wamp\www\tboss\log2es
}
//导入 ES 与 log_Info 配置
$all_Config = require_once(ROOT.'/Allconfig.php');
//导入 cur 配置
require_once(ROOT.'/Curl.php');
$curl = new Curl();
//库与表
$index = $all_Config['params']['es_Conn']['_index'];
$host = $all_Config['params']['es_Conn']['host'];
$port = $all_Config['params']['es_Conn']['port'];
$log_Info = $all_Config['params']['log_Info'];
//init
$this->setHost($host);
$this->setPort($port);
$this->setIndex($index);
$this->setRoot(ROOT);
$this->setCurl($curl);
$this->setLogdir($all_Config['params']['log_Info']);
$this->setLogflag($all_Config['params']['log_Flag']);
$countLog = count($log_Info);
//////////////////////生成文档 start //////////////////////
//新增记录数组集
$arr = array();
$docCountArr = array();
//////////////////////生成文档 end //////////////////////
if(is_array($log_Info) && $countLog)
{
foreach ($log_Info as $key => $value)
{
switch($value['_logtype'])
{
case 1 :
//app log monolog,Thriftlog,appphplog,accesslog
$this->appJsonToEs($value['_dir'],$value['_pointname'],$value['_type'],$value['_proj'],$value['_iid'],$value['_getNum']);
break;
case 2 :
//log2es
$this->logJsonToEs($value['_dir'],$value['_pointname'],$value['_type'],$value['_proj'],$value['_iid'],$value['_getNum']);
break;
case 3 :
//nginxlog
$this->logOneLineToEs($value['_dir'],$value['_pointname'],$value['_type'],$value['_proj'],$value['_iid'],$value['_getNum']);
break;
case 4 :
//phpfpm,yuyuecall,lunciyuyue,reqyuyue,vrayrender
$this->logMoreLineToEs($value['_dir'],$value['_pointname'],$value['_type'],$value['_proj'],$value['_iid'],$value['_getNum']);
break;
case 5 :
//
break;
default:
# code...
break;
}
}
}
//exit;
}
///////////////////////////类属性方法 开始/////////////////////////////////
/**
* Sets specific curl values (updates and keeps default values)
* @param string $curl Params
* @return $this
*/
public function setCurl($curl)
{
$this->_curl= $curl;
return $this;
}
/**
* Sets specific host values (updates and keeps default values)
* @param string $host Params
* @return $this
*/
public function setHost($host)
{
$this->_host= $host;
return $this;
}
/**
* Sets specific port values (updates and keeps default values)
* @param string $port Params
* @return $this
*/
public function setPort($port)
{
$this->_port= $port;
return $this;
}
/**
* Sets specific index values (updates and keeps default values)
* @param string $index Params
* @return $this
*/
public function setIndex($index)
{
$this->_index= $index;
return $this;
}
/**
* Sets specific type values (updates and keeps default values)
* @param string $type Params
* @return $this
*/
public function setType($type)
{
$this->_type = $type;
return $this;
}
/**
* Sets specific root values (updates and keeps default values)
* @param string $root Params
* @return $this
*/
public function setRoot($root)
{
$this->_root = $root;
return $this;
}
/**
* Sets specific logdir values (updates and keeps default values)
* @param string $logdir Params
* @return $this
*/
public function setLogdir($logdir)
{
$this->_logdir = $logdir;
return $this;
}
/**
* Sets specific logflag values (updates and keeps default values)
* @param string $logflag Params
* @return $this
*/
public function setLogflag($logflag)
{
$this->_logflag = $logflag;
return $this;
}
///////////////////////////类属性方法 结束/////////////////////////////////
/**
* @功能:APP 类monolog,thriftlog,appphplog,accesslog 此四类JSON格式处理
* @参数:
* $fileName=> 文件名
* $_pointname=> 指针名
* $_type=> 业务
* $_proj=> 服务
* $_iid=> 节点
* $_getNum=> 第次读取行数
* $return => 返回结果集,
*/
function appJsonToEs($fileName, $_pointname, $_type, $_proj='', $_iid='', $_getNum=100)
{
//读取方式1
$logarr = $this->getLogArr($fileName,$_pointname,0,$_getNum);
//print_r($logarr);
//exit;
//没有数据则直接返回
$countlogarr = count($logarr);
if($countlogarr < 2){
return ;
}
//处理LOG数组
foreach ($logarr as $key => $value)
{
//空值不作处理
if(!$value){return;}
//value str 转换 json 对象
$jsonlogArr = array();
$jsonlogArr = json_decode($value, true);
if(is_array($jsonlogArr)){
//处理第一条记录
$logtime = date("Y-m-d H:i:s");
$_id ='';
$cl ='';
$m ='';
$tsn ='';
$txt ='';
$ip ='';
$ag ='';
$file ='';
$ln =0;
$uid ='';
foreach ($jsonlogArr as $k => $v) {
//日志文本中的时间
if($k == 'time'){
$logtime =$v;
}
if($k == 'REQUEST_TIME'){
$logtime =$v['sec'];
$logtime = date("Y-m-d H:i:s",$logtime);
}
//日志文本中的 IP
if($k == 'REMOTE_ADDR'){
$ip =$v;
}
//日志文本中的 ag
if($k == 'HTTP_USER_AGENT'){
$ag =$v;
}
//日志文本中的 monolog txt
if($k == 'request'){
foreach ($v as $k2 => $v2) {
if($k2 == 'module') {
$cl= $v2;
}elseif($k2 == 'model'){
$cl= $v2;
}
if($k2 == 'action') {$m= $v2;}
}
}
//日志文本中的 thriftlog,appphplog,accesslog txt
if($k == 'data'){
foreach ($v as $k2 => $v2) {
//thriftlog
if($k2 == 'methodName') {$cl= $v2;}
if($k2 == 'param') {
foreach ($v2 as $k3 => $v3) {
if($k3 == 'sort') {
foreach ($v3 as $k4 => $v4) {
if($v4['classname']) {
$m= $v4['classname'];
}
}
}
}
}
//appphplog
if($k2 == 'ERRFILE') {$file= $v2;}
if($k2 == 'ERRLINE') {$ln = (int)$v2;}
if($k2 == 'REQUEST') {
foreach ($v2 as $k3 => $v3) {
if($k3 == 'model') {$cl= $v3;}
if($k3 == 'action') {$m= $v3;}
if($k3 == 'uid') {$uid= $v3;}
}
}
//accesslog
if($k2 == 'req') {
foreach ($v2 as $k3 => $v3) {
if($k3 == 'model') {$cl= $v3;}
if($k3 == 'action') {$m= $v3;}
}
}
}
}
}
$time = time();
$_id = $cl.'-'.$m.'-'.$time.'-'.strtotime($logtime);
$tsn = (int)strtotime($logtime)*1000;
$txt = $value;
$file = $file?$file:$fileName;
$data = array(
'proj'=>$_proj,
'sid' =>'',
'file'=>$file,
'iid' =>$_iid,
'seq' =>0,
'ts' =>$logtime,
'tsn' =>$tsn,
'lv' =>'ERROR',
'lvn' =>40000,
'th' =>'',
'cl' =>$cl,
'm' =>$m,
'ln' =>$ln,
'bsid'=>'',
'esid'=>'',
'txt' =>$txt,
'ex' =>'',
'ag' =>$ag,
'ip' =>$ip,
'uid' =>$uid,
);
//数组转换JSON对象
$jsonStr = json_encode($data);
//数据导入
$url = $_type.'/'.$_id.'/';
//$url = $_type.'/_search';
$this->curlToEs($url,$jsonStr,$_pointname);
}
}
}
/**
* @功能:类 JAVA标准日志格式 处理
* @参数:
* $fileName=> 文件名
* $_pointname=> 指针名
* $_type=> 业务
* $_proj=> 服务
* $_iid=> 节点
* $_getNum=> 第次读取行数
* $return => 返回结果集,
*/
function logJsonToEs($fileName, $_pointname, $_type, $_proj='', $_iid='', $_getNum=100)
{
//读取方式2
$logarr = $this->getLogArr($fileName,$_pointname,0,$_getNum);
//print_r($logarr);
//exit;
//没有数据则直接返回
$countlogarr = count($logarr);
if($countlogarr < 2){
return ;
}
//处理LOG数组
foreach ($logarr as $key => $value) {
//空值不作处理
if(!$value){return;}
//value str 转换 json 对象
$jsonlogArr = array();
$jsonlogArr = json_decode($value, true);
if(is_array($jsonlogArr)){
//处理第一条记录
$_id ='';//编号
$proj =$_proj;//项目编号 服务
$sid ='';//服务名字
$file ='';//采集的日志文件名
$iid =$_iid;//实例号
$seq =0;//日志收集的序列号
$ts = date("Y-m-d H:i:s");//时间戳
$tsn ='';//时间戳的毫秒数字
$lv ='';//日志级别:TRACE,DEBUG等
$lvn ='';//日志级别的数字
$th ='';//线程名
$cl ='';//类名或文件名
$m ='';//方法名
$ln =0;//行号
$bsid ='';//业务会话id
$esid ='';//总线会话id
$txt ='';//日志内容
$ex ='';//异常Exception日志的堆栈信息
$ag ='';//user agent
$ip ='';//调用者ip
$uid ='';//用户id
foreach ($jsonlogArr as $k => $v) {
if($k == 'proj'){ $proj =$v;}
if($k == 'sid') { $sid =$v;}
if($k == 'file'){ $file =$v;}
if($k == 'iid'){ $iid =$v;}
if($k == 'seq'){ $seq =$v;}
if($k == 'ts') { $ts =$v;}
if($k == 'tsn'){ $tsn =$v;}
if($k == 'lv'){ $lv =$v;}
if($k == 'lvn'){ $lvn =$v;}
if($k == 'th'){ $th =$v;}
if($k == 'cl'){ $cl =$v;}
if($k == 'm'){ $m =$v;}
if($k == 'ln'){ $ln =$v;}
if($k == 'bsid'){ $bsid =$v;}
if($k == 'esid'){ $esid =$v;}
if($k == 'txt'){ $txt =$v;}
if($k == 'ex'){ $ex =$v;}
if($k == 'ag'){ $ag =$v;}
if($k == 'ip'){ $ip =$v;}
if($k == 'uid'){ $uid =$v;}
}
$time = time();
$_id = $key.'-'.$time.'-'.$tsn;
$file = $file?$file:$fileName;
$tsn = (int)$tsn*1000;
$data = array(
'proj'=>$proj,
'sid' =>$sid,
'file'=>$file,
'iid' =>$iid,
'seq' =>$seq,
'ts' =>$ts,
'tsn' =>$tsn,
'lv' =>$lv,
'lvn' =>$lvn,
'th' =>$th,
'cl' =>$cl,
'm' =>$m,
'ln' =>$ln,
'bsid'=>$bsid,
'esid'=>$esid,
'txt' =>$txt,
'ex' =>$ex,
'ag' =>$ag,
'ip' =>$ip,
'uid' =>$uid,
);
//print_r($data);
// exit;
//数组转换JSON对象
$jsonStr = json_encode($data);
//数据导入
$url = $_type.'/'.$_id.'/';
//$url = $_type.'/_search';
$this->curlToEs($url,$jsonStr,$_pointname);
}
}
}
/**
* @功能:类 单行 nginxlog 错误 日志格式 处理
* @参数:
* $fileName=> 文件名
* $_pointname=> 指针名
* $_type=> 业务
* $_proj=> 服务
* $_iid=> 节点
* $_getNum=> 第次读取行数
* $return => 返回结果集,
*/
function logOneLineToEs($fileName, $_pointname, $_type, $_proj='', $_iid='', $_getNum=100)
{
//读取方式3
$logarr = $this->getLogArr($fileName,$_pointname,0,$_getNum);
//print_r($logarr);
//exit;
//没有数据则直接返回
$countlogarr = count($logarr);
if($countlogarr < 2){
return ;
}
//处理第一条记录
$_id ='';//编号
$proj =$_proj;//项目编号 服务
$sid ='';//服务名字
$file ='';//采集的日志文件名
$iid =$_iid;//实例号
$seq =0;//日志收集的序列号
$ts = date("Y-m-d H:i:s");//时间戳
$tsn ='';//时间戳的毫秒数字
$lv ='ERROR';//日志级别:TRACE,DEBUG等
$lvn ='';//日志级别的数字
$th ='';//线程名
$cl ='';//类名或文件名
$m ='';//方法名
$ln =0;//行号
$bsid ='';//业务会话id
$esid ='';//总线会话id
$txt ='';//日志内容
$ex ='';//异常Exception日志的堆栈信息
$ag ='';//user agent
$ip ='';//调用者ip
$uid ='';//用户id
//处理日志记录时间与IP逻辑
$findme1 = '[';
$findme2 = ',';
$findme3 = ']';
$timestr ='';
//处理LOG数组
foreach ($logarr as $key => $value) {
//空值不作处理
if(!$value){return;}
//获取时间日期位置
$pos2 = $this->findStrindex($value,$findme1);
if ($pos2 !== false) {
$timestr = substr($value,0,$pos2);
}
//获取IP 开始与结束位置
$pos1 = $this->findStrindex($value,$findme2,1);
$pos3 = $this->findStrindex($value,$findme2,2);
if ($pos1 !== false && $pos3 !== false) {
$ip = substr($value,$pos1+1,$pos3-$pos1-1);
}
//获取lv 开始与结束位置
$pos4 = $this->findStrindex($value,$findme1);
$pos5 = $this->findStrindex($value,$findme3);
if ($pos4 !== false && $pos5 !== false) {
$lv = substr($value,$pos4+1,$pos5-$pos4-1);
}
//lv lvn 取值
$level = $this->getLoglevel($lv);
$lv = $level['lv'];
$lvn = $level['lvn'];
//处理
$logtime = strtotime($timestr);
$time = time();
$_id = $key.'-'.$time.'-'.$logtime;
$ts = date('Y/m/d H:i:s', $logtime);
$tsn = (int)$logtime*1000;
$txt = $value;
$data = array(
'proj'=>$proj,
'sid' =>$sid,
'file'=>$file,
'iid' =>$iid,
'seq' =>$seq,
'ts' =>$ts,
'tsn' =>$tsn,
'lv' =>$lv,
'lvn' =>$lvn,
'th' =>$th,
'cl' =>$cl,
'm' =>$m,
'ln' =>$ln,
'bsid'=>$bsid,
'esid'=>$esid,
'txt' =>$txt,
'ex' =>$ex,
'ag' =>$ag,
'ip' =>$ip,
'uid' =>$uid,
);
//print_r($data);
//exit;
//数组转换JSON对象
$jsonStr = json_encode($data);
//数据导入
$url = $_type.'/'.$_id.'/';
//$url = $_type.'/_search';
$this->curlToEs($url,$jsonStr,$_pointname);
}
}
/**
* @功能:类 多行 php标准日志格式 处理
* @参数:
* $fileName=> 文件名
* $_pointname=> 指针名
* $_type=> 业务
* $_proj=> 服务
* $_iid=> 节点
* $_getNum=> 第次读取行数
* $return => 返回结果集,
*/
function logMoreLineToEs($fileName, $_pointname, $_type, $_proj='', $_iid='', $_getNum=100)
{
//读取方式4
$logarr = $this->getLogArr($fileName,$_pointname,1,$_getNum);
//print_r($logarr);
//exit;
//没有数据则直接返回
$countlogarr = count($logarr);
if($countlogarr < 2){
return ;
}
//处理日志记录时间与IP逻辑
$findme1 = '[';
$findme2 = ',';
$findme3 = ']';
$timestr ='';
//处理LOG数组
//日志格式
//[2015-06-29 09:20:09][INFO][IP: 192.168.3.39][pc] [flash-house] txt
//$arr =array();
foreach ($logarr as $key => $value) {
//处理第一条记录
$_id ='';//编号
$proj =$_proj;//项目编号 服务
$sid ='';//服务名字
$file ='';//采集的日志文件名
$iid =$_iid;//实例号
$seq =0;//日志收集的序列号
$ts = date("Y-m-d H:i:s");//时间戳
$tsn ='';//时间戳的毫秒数字
$lv ='ERROR';//日志级别:TRACE,DEBUG等
$lvn =40000;//日志级别的数字
$th ='';//线程名
$cl ='';//类名或文件名
$m ='';//方法名
$ln =0;//行号
$bsid ='';//业务会话id
$esid ='';//总线会话id
$txt ='';//日志内容
$ex ='';//异常Exception日志的堆栈信息
$ag ='';//user agent
$ip ='';//调用者ip
$uid ='';//用户id
//空值不作处理
if(!$value){return;}
//获取时间日期结束位置
$pos2 = $this->findStrindex($value,$findme3);
if ($pos2 !== false) {
$timestr = substr($value,0,$pos2);
}
//获取时间日期开始位置
$pos1 = $this->findStrindex($timestr,$findme1);
if ($pos1 !== false) {
$timestr = substr($timestr,$pos1+1);
}else{
$value = '['.$value;
}
//获取lv 开始与结束位置
$lvt = '';
$pos5 = $this->findStrindex($value,$findme1,2);
$pos6 = $this->findStrindex($value,$findme3,2);
if ($pos5 !== false && $pos6 !== false) {
$lvt = substr($value,$pos5+1,$pos6-$pos5-1);
}
$lvt = strtoupper($lvt);
$loglevelArr = array('ERROR','WARN','INFO','DEBUG','TRACE');
if (in_array($lvt,$loglevelArr, TRUE)){
//lv lvn 取值
$level = $this->getLoglevel($lvt);
$lv = $level['lv'];
$lvn = $level['lvn'];
}
//获取ip 开始与结束位置
$ipt = '';
$pos5 = $this->findStrindex($value,$findme1,3);
$pos6 = $this->findStrindex($value,$findme3,3);
if ($pos5 !== false && $pos6 !== false) {
$ipt = substr($value,$pos5+1,$pos6-$pos5-1);
}
//判断某字符出现次数
$ipcount = substr_count($ipt,'.');
if($ipcount == 3){
$ip = $ipt;
}
//获取proj 开始与结束位置
$projt = '';
$pos5 = $this->findStrindex($value,$findme1,4);
$pos6 = $this->findStrindex($value,$findme3,4);
if ($pos5 !== false && $pos6 !== false) {
$projt = substr($value,$pos5+1,$pos6-$pos5-1);
}
$projlen = strlen($projt);
if($projlen >0 && $projlen<20 && $_type !='yuyuecall' ){
$proj = $projt;
}
//获取iid 开始与结束位置
$iidt = '';
$pos5 = $this->findStrindex($value,$findme1,5);
$pos6 = $this->findStrindex($value,$findme3,5);
if ($pos5 !== false && $pos6 !== false) {
$iidt = substr($value,$pos5+1,$pos6-$pos5-1);
}
$iidlen = strlen($iidt);
if($iidlen >0 && $iidlen<20 && $_type !='yuyuecall' ){
$iid = $iidt;
}
//获取txt 开始与结束位置
$txtt = '';
$pos6 = $this->findStrindex($value,$findme3,5);
if ($pos6 !== false) {
$txtt = substr($value,$pos6+1);
}
//判断txt 是否是JSON 数据
$txtarr = json_decode($txtt, true);
$jsontxt = json_encode($txtarr);
if($jsontxt !='null'){
$txt = $txtt;
}
if(!$txt){
$txt = $value;
}
//处理
$logtime = strtotime($timestr);
$time = time();
$_id = $key.'-'.$time.'-'.$logtime;
$ts = date('Y/m/d H:i:s', $logtime);
$tsn = (int)$logtime*1000;
$data = array(
'proj'=>$proj,
'sid' =>$sid,
'file'=>$file,
'iid' =>$iid,
'seq' =>$seq,
'ts' =>$ts,
'tsn' =>$tsn,
'lv' =>$lv,
'lvn' =>$lvn,
'th' =>$th,
'cl' =>$cl,
'm' =>$m,
'ln' =>$ln,
'bsid'=>$bsid,
'esid'=>$esid,
'txt' =>$txt,
'ex' =>$ex,
'ag' =>$ag,
'ip' =>$ip,
'uid' =>$uid,
);
//print_r($data);
//exit;
//数组转换JSON对象
$jsonStr = json_encode($data);
//数据导入
$url = $_type.'/'.$_id.'/';
//$url = $_type.'/_search';
$this->curlToEs($url,$jsonStr,$_pointname);
}
//print_r($arr);
//exit;
}
/**
* @功能:通过CURL 方式把数据POST 到ES 系统中
* @参数:
* $url=> POST 地址
* $jsonStr=> 数据的JSON串
* $return => 返回结果集,
*/
function curlToEs($url,$jsonStr,$intype='')
{
//$url = 'http://'.$this->_host.':'.$this->_port.'/'.$this->_index.'/'.$_type;
//$url = $url.'/'.$_id
//$url = $url.'/_search';
//$output = $this->_curl->get($url);
//URL 正式地址
$url = 'http://'.$this->_host.':'.$this->_port.'/'.$this->_index.'/'.$url;
$output = $this->_curl->put($url, $jsonStr);
//$output = $this->_curl->get($url);
//记录日志
$param['intype'] = $intype;
$param['txt'] = $output;
$param['curlUrl'] = $url;
$param['curlParam'] = $jsonStr;
$args = $this->_curl->getErrNo();
$this->rewritetolog($param, $args);
}
/**
* @功能:获取日志数组
* @参数:$fileName=>获取日志的路径,
* $indexType=> 要获取的类型
* $getNum=> 要获取的行数
* $return => 返回结果集,
*/
function del_dir()
{
$now = time();
$time = strtotime('-1 day', $now);
$yday = date('Ymd', $time);
$root = $this->_root;
$indexName = $root.'/log/logIndex'.$yday.'.txt';
if (file_exists($indexName)) {
$result=unlink ($indexName);
}
}
/**
* @功能:获取日志数组
* @参数:$fileName=>获取日志的路径,
* $indexType=> 要获取的类型
* $moreLine=> 是否多行处理
* $getNum=> 要获取的行数
* $return => 返回结果集,
*/
function getLogArr($fileName,$indexType,$moreLine=0,$getNum=100)
{
$root = $this->_root;
$indexName = $root.'/log/logIndex'.date('Ymd').'.txt';
$txtIndex = @file_get_contents($indexName);
$arrIndex = explode("\n", $txtIndex);
$IndexArr = array();
if($arrIndex[0]){
$allindexArr = json_decode($arrIndex[0], true);
if(is_array($allindexArr)){
$IndexArr = $allindexArr;
}
}else{
$IndexArr[$indexType] = 0;
$indexJson = json_encode($IndexArr);
$this->writeFile($indexName,$indexJson,'w');
}
//del dir for yesteday
//检查是否有昨天Index文件
$this->del_dir();
$logarr = array();
$findex = 0;
if (!file_exists($fileName)) {
//记录日志
$param['intype'] = $indexType;
$param['txt'] = "this is no file in $fileName";
$args = 404;
$this->rewritetolog($param, $args);
return $logarr;
}
$fp = @fopen($fileName, 'r');
$i = 0;
$content = '';
fseek($fp, $findex);
while(!feof($fp) && $i++< $getNum)
//while(!feof($fp))
{
$content .= fgets($fp);
$findex = ftell($fp);//记录findex以便下次读取
}
//日志多行处理方式
if($moreLine){
$logarr = explode("\n[", $content);
$count = count($logarr);
if($count>1){
$first = $logarr[0];
$end = end($logarr);
$endarrName = $root.'/log/endarr.php';
$txtIndex = @file_get_contents($endarrName);
$arrIndex = explode("\n", $txtIndex);
$endarr = array();
if($arrIndex[0]){
$allindexArr = json_decode($arrIndex[0], true);
if(is_array($allindexArr)){
$endarr = $allindexArr;
}
}
//首字符
$firstStr = substr( $first, 0, 1 );
if($firstStr == '['){
if(@$endarr[$indexType]){
$logarr[$count-1] = $endarr[$indexType];
}
}else{
$allendstr = $endarr[$indexType].$first;
$logarr[0] = $allendstr;
}
//最后一行存入数组
$endarr[$indexType] = $end;
$indexJson = json_encode($endarr);
$this->writeFile($endarrName,$indexJson,'w');
}
}else{
//日志单行处理方式
$logarr = explode("\n", $content);
}
//文件指针位置
$IndexArr[$indexType] = $findex;
$indexJson = json_encode($IndexArr);
$this->writeFile($indexName,$indexJson,'w');
return $logarr;
}
/**
* 查找某字符在字符串中的位置
* @param string $string Params
* @param string $find Params
* @param int $num Params
* @return $pos
*/
public function findStrindex($string, $find, $num=1)
{
$pos = -1;
$n = 0;
do{
$pos = strpos($string, $find, $pos+1);
$n++;
if($n==$num){
break;
}
}while($pos!==false);
return $pos;
}
/*
**
* 写文件
* @param string $file 文件路径
* @param string $str 写入内容
* @param char $mode 写入模式
*/
function writeFile($file,$str,$mode='w'){
$oldmask = @umask(0);
$fp = @fopen($file,$mode);
@flock($fp, 3);
if(!$fp){
Return false;
} else {
@fwrite($fp,$str);
@fclose($fp);
@umask($oldmask);
Return true;
}
}
/**
* @功能:日志记录
* @参数:
@param $param Array()
$param['intype'] => 要记录日志的类型,
$param['txt'] => 返回结果集,
* $args => 返回结果状态,0表示成功,非0表示失败
* $logger => 是否要记录日志
* @返回:若转换成功返回true,否则返回false或直接跳出
* 显示行号 __LINE__
*/
function rewritetolog($param=array(), $args='', $logger =true)
{
//$log['_id'] = '';//编号
//$log['proj'] = '';//项目编号
$log['sid'] = '';//服务名字
$log['file'] = '';//采集的日志文件名
$log['iid'] = '';//实例号
$log['seq'] = 0;//日志收集的序列号
//$log['ts'] = '';//时间戳
//$log['tsn'] = '';//时间戳的毫秒数字
$log['lv'] = 'ERROR';//日志级别:TRACE,DEBUG等
$log['lvn'] = 40000;//日志级别的数字
$log['th'] = '';//线程名
$log['cl'] = '';//类名或文件名
$log['m'] = '';//方法名
$log['ln'] = 0;//行号
$log['bsid'] = '';//业务会话id
$log['esid'] = '';//总线会话id
$log['txt'] = '';//日志内容
$log['ex'] = '';//异常Exception日志的堆栈信息
$log['ag'] = '';//user agent
$log['ip'] = '';//调用者ip
$log['uid'] = '';//用户id
$time = time();
$ts = date('Y-m-d H:i:s',$time);
$tsn = (int)($time*1000);
$log['proj'] = 'log2es';//项目编号
$log['ts'] = $ts;//时间戳
$log['tsn'] = $tsn;//时间戳的毫秒数字
$logStr = '';
if($logger){
$status = 'INFO';
$flag = $this->_logflag;
$curl = $this->_curl;
$t['time'] = (string)date("Y-m-d H:i:s");
$t['name'] = $param['intype']?(string)$param['intype']:'';
$t['txt'] = $param['txt']?$param['txt']:'';
$t['curlUrl'] = @$param['curlUrl']?(string)$param['curlUrl']:'';
$t['curlParam'] = @$param['curlParam']?$param['curlParam']:'';
$t['curlInfo'] = $curl->getInfo(); //$curl->getInfo();
$t['curlStatus'] = $curl->getStatus();//$curl->getStatus();
$t['curlError'] = $curl->getError(); //$curl->getError();
$t['curlErrNo'] = $curl->getErrNo(); //$curl->getErrNo();
if($flag){
if($args){
$status = 'ERROR';
}
$t['status'] = (string)$status;
//处理日志记录
$level = $this->getLoglevel($status);//日志级别
$log['lv'] = $level['lv'];//日志级别:TRACE,DEBUG等
$log['lvn'] = $level['lvn'];//日志级别的数字
$log['txt'] = $t;//日志内容
//print_r($log);
//exit;
//$_SERVER
$logStr = json_encode($log);
$logStr = $logStr.'
';
$root = $this->_root;
$fileName = $root.'/log/';
$fileName = $fileName.date('Y-m-d').".log";
$this->writeFile($fileName,$logStr,'a');
}else{
if($args){
$status = 'ERROR';
//}
$t['status'] = (string)$status;
//处理日志记录
$level = $this->getLoglevel($status);//日志级别
$log['lv'] = $level['lv'];//日志级别:TRACE,DEBUG等
$log['lvn'] = $level['lvn'];//日志级别的数字
$log['txt'] = $t;//日志内容
//$_SERVER
$logStr = json_encode($log);
$logStr = $logStr.'
';
$root = $this->_root;
$fileName = $root.'/log/';
$fileName = $fileName.date('Y-m-d').".log";
$this->writeFile($fileName,$logStr,'a');
}
}
}
}
/*
**
* 日志级别
* @param string $lv 日志级别字符串
* @return int 返回数字
*/
function getLoglevel($lv)
{
$lv = strtoupper($lv);
$loglevel = array(
'ERROR'=>'40000',//40000
'WARN'=>'30000',//30000
'INFO'=>'20000',//20000
'DEBUG'=>'10000',//10000
'TRACE'=>'5000',//5000
);
if($loglevel[$lv]){
$level['lv'] = $lv;
$level['lvn']= $loglevel[$lv];
}else{
$level['lv'] = 'ERROR';
$level['lvn']= 40000;
}
return $level;
}
}
//执行程序脚本
$alllog = new AlllogCommand();
$alllog->run();
?>