Tp5 Seaslog应用实例
一、配置
<?php
/**
* Seaslog 日志系统配置
*/
return [
'base_path'=>'/seaslog/log',//日志根目录
'default_logger'=>'default_logger',//默认日志模块
'web_logger'=>'house/dev/web',//后台操作日志模块
'api_logger'=>'house/dev/api',//接口操作日志模块
'level'=> ['DEBUG'=>SEASLOG_DEBUG,'INFO'=>SEASLOG_INFO,'NOTICE'=>SEASLOG_NOTICE,'WARNING'=>SEASLOG_WARNING,'ERROR'=>SEASLOG_ERROR,
'CRITICAL'=>SEASLOG_CRITICAL,'ALERT'=>SEASLOG_ALERT,'EMERGENCY'=>SEASLOG_EMERGENCY,//等级
],
];
二、Seaslog 核心类
<?php
namespace app\common\service;
use think\facade\Config;
/**
* Seaslog日志系统服务
* Class SeaslogService
* @package core
*/
class SeaslogService
{
//模块目录
private static $log_module;
/**
* 初始化
* SeaslogService constructor.
* @param string $logger
*/
public function __construct($logger='')
{
if($logger==''){
$logger=Config::get('seaslog.default_logger');
}
self::$log_module=$logger;
\Seaslog::setBasePath(Config::get('seaslog.base_path'));
\SeasLog::setLogger(self::$log_module);
}
/**
* 写入日志
* @param string $message
* @param array $content
* @return mixed
*/
public function writeLog($level=SEASLOG_INFO,$message='',$content=[]){
$res=\SeasLog::log ($level,$message,$content);
return $res;
}
/**
* 读取日志
* @return array
*/
public function getLog($level=SEASLOG_INFO,$keyword='',$page=1,$limit=15){
if(checkSys()=='window'){
$count=\SeasLog::analyzerCount($level,NULL);
$list=\SeasLog::analyzerDetail($level,NULL);
}else{
$count=\SeasLog::analyzerCount($level,NULL,$keyword);
$list=\SeasLog::analyzerDetail($level,NULL,$keyword,$page,$limit,SEASLOG_DETAIL_ORDER_DESC);
}
if(!empty($list)){
$i=0;
foreach($list as $k=>$val){
$item=[];
$row=explode('|',$val);
$firstColon=strpos($row[0],':');
//$item['row']=$row;
$item['file_path']=substr($row[0],0,$firstColon);//文件路径
$item['date']=substr($row[0],$firstColon+1);//日期
$item['level']=$row[1];//等级
$item['pid']=$row[2];//进程id
$item['request_id']=$row[3];//get_uniqid()方法生成的惟一值
$item['time']=date('Y-m-d H:i:s',(float)$row[4]);//时间
$item['message']=$row[5];//日志信息
$item['host']=isset($row[6])?$row[6]:'异常';//主机名称
$item['domain']=isset($row[7])?$row[7]:'异常';//域名
$item['url']=isset($row[8])?$row[8]:'异常';//请求url
$item['request_type']=isset($row[9])?$row[9]:'异常';//请求方式
$item['agent_ip']=isset($row[10])?$row[10]:'异常';//请求ip
$item['ind']=$i;
$list[$k]=$item;
$i++;
}
}
//$pageNum=$count==0?1:(ceil($count/$limit)<=0?1:ceil($count/$limit));
empty($list) ? $msg = '暂无数据!' : $msg = '查询成功!';
$info = [
'limit' => $limit,
'page_current' => $page,
'page_sum' => ceil($count / $limit),
'level' =>$level,
'keyword' =>$keyword,
'lastLogger' =>\Seaslog::getLastLogger()
];
$list = [
'code' => 0,
'msg' => $msg,
'count' => $count,
'info' => $info,
'data' => $list,
];
return $list;
}
}
三、写入
/**
* 写入seaslog错误日志
* @level $level
* @param $info
*/
public static function writeErrorSeaslog($info,$level=SEASLOG_CRITICAL){
$seaslog=new SeaslogService(\think\facade\Config::get('seaslog.web_logger'));
$user=session('user');
$content=[
'username'=>isset($user['username'])?$user['username']:'',
'id'=>isset($user['id'])?$user['id']:''
];
$message="错误日志;管理员:{username},Id:{id};信息:{$info}";
$seaslog->writeLog($level,$message,$content);
}
四、读取
/**
* 日志列表
* @return mixed|\think\response\Json
*/
public function index(){
$default_logger=\think\facade\Config::get('seaslog.default_logger');
$web_logger=\think\facade\Config::get('seaslog.web_logger');
$api_logger=\think\facade\Config::get('seaslog.api_logger');
$level_list=\think\facade\Config::get('seaslog.level');
if ($this->request->get('type') == 'ajax') {
$page = $this->request->get('page', 1);
$limit = $this->request->get('limit', 10);
$search = (array)$this->request->get('search', []);
$logger=isset($search['logger'])?$search['logger']:$web_logger;
$seaslog=new SeaslogService($logger);
$keyword=isset($search['keyword'])?$search['keyword']:'';
$level=isset($search['level']) && isset($level_list[$search['level']])?$level_list[$search['level']]:$level_list['CRITICAL'];
$res=$seaslog->getLog($level,$keyword,$page,$limit);
return json($res);
}
\Seaslog::setLogger($web_logger);
$basic_data = [
'title' => '日志列表',
'all_count'=>\Seaslog::analyzerCount(),
'web_logger'=>$web_logger,
'is_page'=>checkSys()=='window'?0:1,
'logger_list'=>array(['name'=>'default_logger','title'=>'默认模块'],['name'=>$web_logger,'title'=>'Web日志模块'],['name'=>$api_logger,'title'=>'Api日志模块']),
'level_list'=>array(['title'=>'DEBUG'],['title'=>'INFO'],['title'=>'NOTICE'],['title'=>'WARNING'],['title'=>'ERROR'],['title'=>'CRITICAL'],['title'=>'ALERT'],['title'=>'EMERGENCY']),
];
//print_r($basic_data['all_count']);
return $this->fetch('', $basic_data);
}
/**
* 获取模块数量
* @return mixed
*/
public function getCountByLogger(){
$post=$this->request->post();
$default_logger=\think\facade\Config::get('seaslog.default_logger');
$logger=isset($post['logger'])?$post['logger']:$default_logger;
\Seaslog::setLogger($logger);
$res=\Seaslog::analyzerCount();
if(!is_array($res)) return __error('获取失败');
return __success('success',$res);
}
五、附件
/**
* 判断操作系统
* @return string
*/
function checkSys()
{
$str = strtoupper(substr(PHP_OS, 0, 3));
if ($str == 'WIN') {
return 'window';
}
return 'linux';
}

浙公网安备 33010602011771号