0

我有一个这样的日志文件:

2013-04-08-03-17-52: Cleaning Up all data for restart operation
2013-04-08-03-18-02: Creating new instance of app before closing
2013-04-08-03-18-03: New instance created and running
2013-04-08-03-18-03: Application started

目前,我每秒都在加载完整的日志文件,以便使用 jquery ajax 向用户显示它,因为这太低效了,我试图找出一些方法来仅从日志文件中加载更新的行。

他们是否有任何方式仅在特定时间戳之后获取行2013-04-08-03-18-03 ?为此,我将管理具有最后一个时间戳的变量,并且每次我获得新行时都会更新它。

我是 PHP 新手,只知道读写文件的基础知识。

4

3 回答 3

1

您可能需要先检查文件修改时间以查看是否需要重新加载日志文件。您可以filemtime为此使用 -function。

此外,您可以使用file_get_contents-function 使用偏移量从某个点读取文件。

编辑:那么,它是如何工作的?

假设您已将上次修改时间存储在会话变量中$_SESSION['log_lastmod'],并将最近的偏移量存储在$_SESSION['log_offset'].

session_start();

// First, check if the variables exist. If not, create them so that the entire log is read.
if (!isset($_SESSION['log_lastmod']) && !isset($_SESSION['log_offset'])) {
    $_SESSION['log_lastmod'] = 0;
    $_SESSION['log_offset'] = 0;
}

if (filemtime('log.txt') > $_SESSION['log_lastmod']) {
    // read file from offset
    $newcontent = file_get_contents('log.txt', NULL, NULL, $_SESSION['log_offset']);
    // set new offset (add newly read characters to last offset)
    $_SESSION['log_offset'] += strlen($newcontent);
    // set new lastmod-time
    $_SESSION['log_lastmod'] = filemtime('log.txt');

    // manipulate $newcontent here to what you want it to show
} else {
    // put whatever should be returned to the client if there are no updates here
}
于 2013-04-07T22:28:20.590 回答
1

你可以试试

session_start();
if (! isset($_SESSION['log'])) {
    $log = new stdClass();
    $log->timestamp = "2013-04-08-03-18-03";
    $log->position = 0;
    $log->max = 2;
    $_SESSION['log'] = &$log;
} else {
    $log = &$_SESSION['log'];
}

$format = "Y-m-d-:g:i:s";
$filename = "log.txt";

// Get last date
$dateLast = DateTime::createFromFormat($format, $log->timestamp);

$fp = fopen($filename, "r");
fseek($fp, $log->position); // prevent loading all file to memory

$output = array();
$i = 0;
while ( $i < $log->max && ! feof($fp) ) {
    $content = fgets($fp);

    // Check if date is current
    if (DateTime::createFromFormat($format, $log->timestamp) < $dateLast)
        continue;

    $log->position = ftell($fp); // save current position
    $log->timestamp = strstr($content, ":", true); // save curren time;
    $output[] = $content;
    $i ++;
}

fclose($fp);
echo json_encode($output); // send to ajax 
于 2013-04-07T22:44:14.247 回答
0

尝试这样的事情:

<?php
    session_start();
    $lines = file(/*Insert logfile path here*/);
    if(isset($_SESSION["last_date_stamp"])){
        $new_lines = array();
        foreach($lines as $line){
            // If the date stamp is newer than the session variable, add it to $new_lines
        }
        $returnLines = array_reverse($newLines); // the reverse is to put the lines in the right order
    } else {
        $returnLines = $lines;
    }
    $_SESSION['last_date_stamp'] = /* Insert the most recent date stamp here */;
    // return the $returnLines variable
?>

这样做是逐行读取文件,如果会话变量已经存在,则将所有日期戳比会话变量中的新的行添加到返回变量$returnLines,如果不存在会话变量,它将放入文件中的所有行$returnLines

在此之后,它创建.更新会话变量以供下次执行代码时使用。最后,它使用$returnLines变量返回日志文件数据。

希望这可以帮助。

于 2013-04-07T22:34:09.817 回答