124 lines
2.8 KiB
PHP
Executable File
124 lines
2.8 KiB
PHP
Executable File
<?php
|
|
defined('BASEPATH') OR exit('No direct script access allowed');
|
|
/**
|
|
* 查看日志
|
|
* Created by PhpStorm.
|
|
* User: xuxb
|
|
* Date: 2019/11/8
|
|
* Time: 15:33
|
|
*/
|
|
class Log extends CI_Controller{
|
|
|
|
function __construct(){
|
|
parent::__construct();
|
|
}
|
|
|
|
/**
|
|
* @param $method
|
|
* @param string $version
|
|
*/
|
|
function _remap($method, $version = ''){
|
|
|
|
$segments = array_slice($this->uri->segment_array(), 2);
|
|
$dir = implode('/', $segments);
|
|
|
|
$path = $this->path($method, $dir);
|
|
|
|
if(is_file($path)){
|
|
return $this->get_log($path);
|
|
} elseif(is_dir($path)) {//日志文件
|
|
return $this->get_dir($path);
|
|
} else {
|
|
exit("file unkonw;path=$path");
|
|
}
|
|
|
|
}
|
|
|
|
/**
|
|
* 获取日志
|
|
* @param $path
|
|
*/
|
|
protected function get_log($path){
|
|
$n = $this->input->get('n');
|
|
!$n && $n = 100;
|
|
|
|
$start = $this->input->get('start');
|
|
$start < $n && $start = $n;
|
|
|
|
if(!$fp=fopen($path,'r')){
|
|
exit("[error] file can not open;path=$path\n");
|
|
}
|
|
|
|
$pos = -2;
|
|
$t = " ";
|
|
while ($start > 0) {
|
|
while ($t != "\n") {
|
|
if(-1 != fseek($fp, $pos, SEEK_END)){
|
|
$t = fgetc($fp);
|
|
$pos--;
|
|
} else {
|
|
fseek($fp, 0, SEEK_SET);
|
|
$start = 0;
|
|
break;
|
|
}
|
|
|
|
}
|
|
$t = " ";
|
|
$start--;
|
|
}
|
|
|
|
while (!feof($fp) && $n-- > 0){
|
|
echo fgets($fp);
|
|
};
|
|
|
|
//关闭文件
|
|
fclose($fp);
|
|
|
|
}
|
|
|
|
/**
|
|
* 展示目录
|
|
* @param $path
|
|
*/
|
|
protected function get_dir($path){
|
|
$arr = array();
|
|
if ($dh = opendir($path)){
|
|
while (($file = readdir($dh)) !== false){
|
|
if('.' == $file || '..' == $file){
|
|
continue;
|
|
}
|
|
$arr[] = $file;
|
|
}
|
|
|
|
sort($arr);
|
|
$arr = array_reverse($arr);//倒序排列
|
|
|
|
// echo json_encode($arr);
|
|
foreach($arr as $v){
|
|
echo "<a href='/" .$this->uri->uri_string(). "/{$v}'>{$v}</a><br/>";
|
|
}
|
|
|
|
closedir($dh);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 获取文件路径
|
|
* @param $method
|
|
* @param $dir
|
|
* @return string
|
|
*/
|
|
private function path($method, $dir){
|
|
if('api' == $method){
|
|
$file_path = "../../api/logs/{$dir}";
|
|
} elseif('admin' == $method) {
|
|
$file_path = APPPATH . "logs/{$dir}";
|
|
} elseif('h5' == $method){
|
|
$file_path = "../../home/logs/{$dir}";;
|
|
} else {
|
|
exit("method unknow;method={$method};dir={$dir}");
|
|
}
|
|
|
|
return $file_path;
|
|
}
|
|
} |