Skip to content

Instantly share code, notes, and snippets.

@deizel
Created October 6, 2012 22:08
Show Gist options
  • Star 35 You must be signed in to star a gist
  • Fork 10 You must be signed in to fork a gist
  • Save deizel/3846335 to your computer and use it in GitHub Desktop.
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

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

@pedritin
Copy link

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

@anovoselof
Copy link

Helpfull, Thanks!!

@gouchaoer
Copy link

great, thx

@lbp0200
Copy link

lbp0200 commented Dec 13, 2016

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
Copy link

Sorok-Dva commented Feb 10, 2017

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

@wilsaav
Copy link

wilsaav commented Apr 12, 2017

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
Copy link

divinity76 commented May 2, 2017

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

@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
Copy link

divinity76 commented May 3, 2017

@viewpointsa
Copy link

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

@AlbertEinsteinGlitchPoint

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

@Reiner030
Copy link

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

@VshadowTX
Copy link

Thanks Helpfull, Thanks!!

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