Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/290.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/logging/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在php中保留日志文件中最后n行数_Php_Logging_Lines - Fatal编程技术网

如何在php中保留日志文件中最后n行数

如何在php中保留日志文件中最后n行数,php,logging,lines,Php,Logging,Lines,我想删除旧日志文件中的所有行,并保留底部的50行 我该怎么做,如果可能的话,我们可以改变这些线的方向 normal input 111111 2222222 3333333 44444444 5555555 output like 555555 4444444 3333333 22222 111111 查看顶部和50或100行的新日志 如何将其与此连接 // set source file name and path $source = "toi200686.txt"; // rea

我想删除旧日志文件中的所有行,并保留底部的50行

我该怎么做,如果可能的话,我们可以改变这些线的方向

normal input

111111
2222222
3333333
44444444
5555555

output like

555555
4444444
3333333
22222
111111
查看顶部和50或100行的新日志

如何将其与此连接

// set source file name and path 
$source = "toi200686.txt"; 
// read raw text as array 
$raw = file($source) or die("Cannot read file"); 
// join remaining data into string 
$data = join('', $raw); 
// replace special characters with HTML entities 
// replace line breaks with <br />  
$html = nl2br(htmlspecialchars($data)); 
//设置源文件名和路径
$source=“toi200686.txt”;
//以数组形式读取原始文本
$raw=文件($source)或模具(“无法读取文件”);
//将剩余数据连接到字符串中
$data=join(“”,$raw);
//用HTML实体替换特殊字符
//将换行符替换为
$html=nl2br(htmlspecialchars($data));
它将输出作为HTML文件。那么,您的代码将如何运行

$lines = file('/path/to/file.log'); // reads the file into an array by line
$flipped = array_reverse($lines); // reverse the order of the array
$keep = array_slice($flipped,0, 50); // keep the first 50 elements of the array
从那里你可以用
$keep
点任何东西。例如,如果你想把它吐出来:

echo implode("\n", $keep);

从那里你可以用
$keep
点任何东西。例如,如果你想把它吐出来:

echo implode("\n", $keep);


这将用于截断日志文件:

exec("tail -n 50 /path/to/log.txt", $log);
file_put_contents('/path/to/log.txt', implode(PHP_EOL, $log));

这将返回
$log
tail
的输出,并将其写回日志文件。

这将用于截断日志文件:

exec("tail -n 50 /path/to/log.txt", $log);
file_put_contents('/path/to/log.txt', implode(PHP_EOL, $log));
这将返回
$log
tail
的输出,并将其写回日志文件。

最佳形式为:

<?
print `tail -50 /path/to/file.log`;
?>

最佳形式是:

<?
print `tail -50 /path/to/file.log`;
?>

这有点复杂,但由于整个文件未加载到数组中,因此使用的内存较少。基本上,它保持一个N长度的数组,并在从文件中读取新行时推送新行,同时将一行移走。因为换行符是由fgets返回的,所以即使使用填充数组,也可以简单地进行内爆以查看N行

<?php
$handle = @fopen("/path/to/log.txt", "r");
$lines = array_fill(0, $n-1, '');

if ($handle) {
    while (!feof($handle)) {
        $buffer = fgets($handle);
        array_push($lines, $buffer);
        array_shift($lines);
    }
    fclose($handle);
}

print implode("",$lines);
?>


只是展示另一种处理方法,特别是当您没有
tail
可供使用时。

这有点复杂,但由于整个文件未加载到数组中,因此使用的内存较少。基本上,它保持一个N长度的数组,并在从文件中读取新行时推送新行,同时将一行移走。因为换行符是由fgets返回的,所以即使使用填充数组,也可以简单地进行内爆以查看N行

<?php
$handle = @fopen("/path/to/log.txt", "r");
$lines = array_fill(0, $n-1, '');

if ($handle) {
    while (!feof($handle)) {
        $buffer = fgets($handle);
        array_push($lines, $buffer);
        array_shift($lines);
    }
    fclose($handle);
}

print implode("",$lines);
?>


只是展示另一种处理方法,尤其是当您没有
尾部
可供使用时。

此方法使用关联数组每次仅存储
$tail
行数。它不会用所有行填充整个数组

$tail=50;
$handle = fopen("file", "r");
if ($handle) {
    $i=0;
    while (!feof($handle)) {
        $buffer = fgets($handle,2048);
        $i++;
        $array[$i % $tail]=$buffer;
    }
    fclose($handle);
    for($o=$i+1;$o<$i+$tail;$o++){
        print $array[$o%$tail];
    }
}
$tail=50;
$handle=fopen(“文件”、“r”);
如果($handle){
$i=0;
而(!feof($handle)){
$buffer=fgets($handle,2048);
$i++;
$array[$i%$tail]=$buffer;
}
fclose($handle);

对于($o=$i+1;$o此方法使用关联数组每次仅存储
$tail
行数。它不会用所有行填充整个数组

$tail=50;
$handle = fopen("file", "r");
if ($handle) {
    $i=0;
    while (!feof($handle)) {
        $buffer = fgets($handle,2048);
        $i++;
        $array[$i % $tail]=$buffer;
    }
    fclose($handle);
    for($o=$i+1;$o<$i+$tail;$o++){
        print $array[$o%$tail];
    }
}
$tail=50;
$handle=fopen(“文件”、“r”);
如果($handle){
$i=0;
而(!feof($handle)){
$buffer=fgets($handle,2048);
$i++;
$array[$i%$tail]=$buffer;
}
fclose($handle);


对于($o=$i+1;$o+1,虽然当日志文件很大时,他可能会遇到内存问题,但如何将其与此连接?,//设置源文件名和路径$source=“toi200686.txt”;//将原始文本读取为数组$raw=file($source)或die(“无法读取文件”);//将剩余数据连接到字符串$data=join(“”,$raw);//用HTML实体替换特殊字符//用
$HTML=nl2br(htmlspecialchars($data))替换换行符;它将输出作为html文件,那么您的代码将如何正确运行?ThanksI认为最好在翻转数组之前获取数组切片,以避免复制可能是大型数组的内容。
$keep=array\u reverse(array\u slice($lines,count($lines)-$n-1,count($lines)-1))是的,JeffB,但是在哪里写50?在那一行?我得到了工作(prodigitalson)50在$n。所以…
$keep=array\u reverse(array\u slice($lines,count($lines)-51,count($lines)-1))
+1虽然当日志文件很大时,他可能会遇到内存问题,但如何将其与此文件连接?,//设置源文件名和路径$source=“toi200686.txt”;//将原始文本读取为数组$raw=file($source)或die(“无法读取文件”);//将剩余数据连接到字符串$data=join(“”,$raw);//用HTML实体替换特殊字符//用
$HTML=nl2br(htmlspecialchars($data))替换换行符;它将输出作为html文件,那么您的代码将如何正确运行?ThanksI认为最好在翻转数组之前获取数组切片,以避免复制可能是大型数组的内容。
$keep=array\u reverse(array\u slice($lines,count($lines)-$n-1,count($lines)-1))是的,JeffB,但是在哪里写50?在那一行?我得到了工作(prodigitalson)50在$n。所以…
$keep=array\u reverse(array\u slice($lines,count($lines)-51,count($lines)-1))
sure不支持安装Cygwin工具。这种方法的问题是在回写日志时获取换行符。sure不支持安装Cygwin工具。这种方法的问题是在回写日志时获取换行符+1。显然资源密集度较低……我只是碰巧避免了资源句柄如果可能的话:-)我现在很困惑,告诉我我应该选哪一个use@Mikeyy:取决于内存是否是一个问题。考虑到这些都是文本文件,很可能不是,所以两种解决方案都可以。是的,那么哪一种是最好的,我想你的是最好的,我所有这些都可以工作:D@Mikeyy:你失去了我。我需要一个更具体的描述和/或一个例子。作为一个新问题问这个问题可能会更好。这个问题也可以+1。资源密集度肯定会更低…我只是碰巧避免了资源句柄