Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Display last lines of file

Status
Not open for further replies.

proggybilly

Programmer
Apr 30, 2008
110
US
Hey all,

I am trying to write a web interface that will either display the last 100 lines of a log file or display the whole thing depending on what the user chooses. I can get it to display but not with the proper formatting.

I have used this code to get the last 100 lines

$display = system("tail -100 /var/log/mylog.log");

however when I echo that to the screen it does not display properly.

Can someone please help me
 
If the whole log displays properly
you should be able to create a tpl that will replicate the full log formatting and call the excerpt via the tpl
 
I already mentioned that it does not display properly. That is the problem. Whether it is the whole log or the tail of the log....
 
it does not display properly
can you be a bit more specific? What would be proper?


Maybe you can use the file() function instead. That way you have more control over the lines of the file, and you can display them as you see fit instead of relying on whatever the system call returns.

----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.
 
How are you displaying the output. I believe you have to take help of HTML features/tags like 'table' and 'wordwrap' to fit it into screen.

--------------------------------------------------------------------------
I never set a goal because u never know whats going to happen tommorow.
 
By it does not display properly I mean that all the text is run together, the newline calls and carriage returns are not recognized.
 
Also have a look at PHP functions like nl2br etc.

--------------------------------------------------------------------------
I never set a goal because u never know whats going to happen tommorow.
 
Took suggestions of using <pre> tags and that did not do it for me. Any suggestions doing it with out me running the system call?
 
Actually made it work using the <pre> tags.. Was actually simpler than I thought once I took a closer look at the file() function..

Basically used file() to load into an array. Got a count of elements in the array. Then ran a for loop starting my counter at the element 100 less than the total line count.

<?php

$display = file('/var/log/script.log');
$count = count($display);
// echo $count;
echo '<pre>';
for($i=$count - 20; $i<$count; $i++){
echo $display[$i];
}
echo '</pre>';
?>


 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top