Skip to content

Instantly share code, notes, and snippets.

@deizel
Created October 6, 2012 22:08
Show Gist options
  • Select an option

  • Save deizel/3846335 to your computer and use it in GitHub Desktop.

Select an option

Save deizel/3846335 to your computer and use it in GitHub Desktop.
PHP log tail example
<?php
if (isset($_GET['ajax'])) {
session_start();
$handle = fopen('/private/var/log/system.log', 'r');
if (isset($_SESSION['offset'])) {
$data = stream_get_contents($handle, -1, $_SESSION['offset']);
echo nl2br($data);
} else {
fseek($handle, 0, SEEK_END);
$_SESSION['offset'] = ftell($handle);
}
exit();
}
?>
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<script src="http://code.jquery.com/jquery-1.8.2.min.js"></script>
<script src="http://creativecouple.github.com/jquery-timing/jquery-timing.min.js"></script>
<script>
$(function() {
$.repeat(1000, function() {
$.get('tail.php?ajax', function(data) {
$('#tail').append(data);
});
});
});
</script>
</head>
<body>
<div id="tail">Starting up...</div>
</body>
</html>
@TheMacMini09

Copy link
Copy Markdown

Is there any way to keep the view at the bottom of the screen? i.e. keep scrolling?

@pedritin

Copy link
Copy Markdown

You must set the position of scrollbar at the bottom whenever requested by ajax

@anovoselof

Copy link
Copy Markdown

Helpfull, Thanks!!

@gouchaoer

Copy link
Copy Markdown

great, thx

@lbp0200

lbp0200 commented Dec 13, 2016

Copy link
Copy Markdown

I change it for my use

if (isset($_SESSION['offset'])) {
                $data = stream_get_contents($handle, -1, $_SESSION['offset']);
                $_SESSION['offset'] += strlen($data);
                echo $data;
            } else {
                fseek($handle, 0, SEEK_END);
                $_SESSION['offset'] = ftell($handle);
            }

@Sorok-Dva

Sorok-Dva commented Feb 10, 2017

Copy link
Copy Markdown

The mode is not binary-safe ('b' is missing)

From official documentation:
Note: For portability, it is strongly recommended that you always use the 'b' flag when opening files with fopen().
Note: Again, for portability, it is also strongly recommended that you re-write code that uses or relies upon the 't' mode so that it uses the correct line endings and 'b' mode instead.

Replacing $handle = fopen('/private/var/log/system.log', 'r'); by $handle = fopen('/private/var/log/system.log', 'rb');

@exprodrigues

Copy link
Copy Markdown

@wilsaav

wilsaav commented Apr 12, 2017

Copy link
Copy Markdown

Hi

The code places it in a PHP daemon but stops for no reason, I saw a comment that recommended placing a 'b' in the 'fopen' but it is not clear to me that it is 'b' and if the problem is solved, I would appreciate it To guide me.

@divinity76

divinity76 commented May 2, 2017

Copy link
Copy Markdown

don't hardcode the name tail.php , make the code name-independent, just do $.get('?ajax' and the browser will fill in the blanks, wether it be tail.php or logs.php or anythingelse.php.
furthermore, don't use setInterval for this (i assume that's what repeat() does?) - if you're on a slow connection, and use setInterval, it will start downloading several copies of the same logs simultaneously! (if its slow enough, will keep doing this until you reach the browser's max socket limit!) - call the function once, and have it call setTimeout() on itself

@divinity76

Copy link
Copy Markdown

@wilsaav this code is not built as a daemon at all, if you want to daemonize this, you'd better write it from scratch.

this code is built to run on-demand whenever the browser request a log update, and the browser javascript is built to request a log update 1000 milliseconds (aka 1 second)

@divinity76

divinity76 commented May 3, 2017

Copy link
Copy Markdown

@viewpointsa

Copy link
Copy Markdown

Automatic scroll done
window.scrollTo(0,document.body.scrollHeight);

@AlbertEinsteinGlitchPoint

Copy link
Copy Markdown

Nop it simply doesnt work on linux ubuntu tested it now, not logging my syslog file

@Reiner030

Copy link
Copy Markdown

Found also this nice Bootstrap based multi-file Logviewer with optional grep:
https://github.com/taktos/php-tail

@VshadowTX

Copy link
Copy Markdown

Thanks Helpfull, Thanks!!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment