Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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

This comment has been minimized.

Copy link

@TheMacMini09 TheMacMini09 commented Aug 20, 2014

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

@pedritin

This comment has been minimized.

Copy link

@pedritin pedritin commented Dec 17, 2014

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

@anovoselof

This comment has been minimized.

Copy link

@anovoselof anovoselof commented Mar 16, 2015

Helpfull, Thanks!!

@gouchaoer

This comment has been minimized.

Copy link

@gouchaoer gouchaoer commented Dec 29, 2015

great, thx

@lbp0200

This comment has been minimized.

Copy link

@lbp0200 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

This comment has been minimized.

Copy link

@Sorok-Dva 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

This comment has been minimized.

Copy link

@exprodrigues exprodrigues commented Feb 20, 2017

@wilsaav

This comment has been minimized.

Copy link

@wilsaav 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

This comment has been minimized.

Copy link

@divinity76 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

This comment has been minimized.

Copy link

@divinity76 divinity76 commented May 2, 2017

@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

This comment has been minimized.

Copy link

@divinity76 divinity76 commented May 3, 2017

@viewpointsa

This comment has been minimized.

Copy link

@viewpointsa viewpointsa commented Jul 5, 2017

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

@AlbertEinsteinGlitchPoint

This comment has been minimized.

Copy link

@AlbertEinsteinGlitchPoint AlbertEinsteinGlitchPoint commented May 31, 2018

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

@Reiner030

This comment has been minimized.

Copy link

@Reiner030 Reiner030 commented Oct 9, 2019

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

@VshadowTX

This comment has been minimized.

Copy link

@VshadowTX VshadowTX commented Mar 28, 2021

Thanks Helpfull, Thanks!!

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