2

I want to tail a dated log file LIVE on the administrator page of my website, but I'm running in to problems. I can get it to work only if I manually input the date in the $filename variable.

Here is the code I found on here too, but would like to know how to get dated files (ex: 2016_04_26.ilog).

<?php
$filename = 'date('Y_m_d').ilog';
$filedir = '/gamelogs/';
$output = shell_exec('exec tail -n250 ' . $filedir$filename');
echo str_replace(PHP_EOL, '<br />', $output);
?>

What is wrong with my code? Thanks in advance!


So after taking a break and coming back to the code, I realized I had the date format wrong and a couple other parts. Here is what I have that works!

<?php
$filename = date('y_m_d').'.ilog'; // misplaced quotes
$filedir = '/home/a3invmgr/destinedtobe/a3ilogs/'; // relative path instead of full path
$output = shell_exec("tail -n100  $filedir$filename"); //exec removed
echo str_replace(PHP_EOL, '<br />', $output); // line OK
?>
Rickie W
  • 35
  • 1
  • 4
  • Have you found your answer? – Pedro Lobito Apr 27 '16 at 10:41
  • No, I tried your solution but was only able to output the log directory and file name like: "/gamelogs/2016_04_26.ilog". But I would like it to select the current dated log file names (ex: YYYY_MM_DD.ilog) and echo it out, the reason is the files are being written to constantly every time a character does or makes an action in the game. I had it working before, but it wouldn't select the current date automatically (if possible, selecting and displaying the feed of the latest .ilog file from a directory would work too as the directory only stores .ilog files. – Rickie W Apr 28 '16 at 00:49
  • I spent about 2 hours after working with all of your guys suggested code with no luck, I'm still trying to figure it out as I type this out. – Rickie W Apr 28 '16 at 00:50

3 Answers3

1

You've a couple of errors, try this:

<?php
$filename = date('Y_m_d').'.ilog'; // misplaced quotes
$filedir = '/full/path/to/gamelogs/'; // relative path instead of full path
$output = shell_exec("tail -n250  {$filedir}{$filename}"); //exec removed
echo str_replace(PHP_EOL, '<br />', $output); // line OK
?>
Pedro Lobito
  • 94,083
  • 31
  • 258
  • 268
0

Two problems that need fixing:

  1. $filedir$filename

that is not a valid php variable.

  1. You have an apostrophe attacking your code here

    $filedir$filename');

Take the apostrophe out.

Fix those two issues and let me know if your problem is solved.

Webeng
  • 7,050
  • 4
  • 31
  • 59
  • Trying this as well as the others still hasn't worked, just gives me a blank page. But I had it working with the original if I entered the date, but the files automatically save and generate a new log for every new day. How can I automatically view the newest file with a .ilog extension would be my second option here I guess, with it only showing the last 50 lines of text or so. – Rickie W Apr 27 '16 at 04:29
0

I think your initial $filename might be wrong - I see too many quotes. Assuming that filenames look like 2016_04_27.ilog then you should use:

$filename = date('Y_m_d'). '.ilog';

Also, to further troubleshoot, you might try putting the execution command into a variable and outputting that:

$stringToExecute = ('tail -n250 ' . $filedir$filename);
echo $stringToExecute;
$output = shell_exec(stringToExecute);

Lastly, if you're echoing log files, you might just want to skip the str_replace and just wrap the results in a <pre>. This will tell the browser to keep newlines intact.

echo "<pre>$output</pre>";
Ben
  • 611
  • 4
  • 10
  • Now it is showing "tail -n50 /gamedir/2016_04_26.ilog" as the output, but I would like to actually tail the entries going into the log file live. How would I go about doing something like that? – Rickie W Apr 27 '16 at 00:44
  • Ah. So, your question has sort of been answered before (http://stackoverflow.com/questions/15025875/what-is-the-best-way-in-php-to-read-last-lines-from-a-file) and the best solution was seen to be: https://gist.github.com/lorenzos/1711e81a9162320fde20 - You might then want to look a using AJAX commands to periodically poll the server for the latest file contents. – Ben Apr 27 '16 at 00:54
  • Also, it's worth noting that traditionally we'd use websockets for a 'live' problem like this, but PHP's websockets support is very limited. – Ben Apr 27 '16 at 00:55