Created
October 6, 2012 22:08
-
-
Save deizel/3846335 to your computer and use it in GitHub Desktop.
PHP log tail example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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> |
@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)
@wilsaav maybe check out this server - https://github.com/divinity76/tailserver/blob/master/tailserver.php
Automatic scroll done
window.scrollTo(0,document.body.scrollHeight);
Nop it simply doesnt work on linux ubuntu tested it now, not logging my syslog file
Found also this nice Bootstrap based multi-file Logviewer with optional grep:
https://github.com/taktos/php-tail
Thanks Helpfull, Thanks!!
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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 betail.php
orlogs.php
oranythingelse.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