Show txt file on a web page that updates every second

I kinda shoot in the dark; I don't know how to do this, so some pointers and / or links to useful tutorials would be great:

I have a website on which I want to display a text file (server log). Probably built in. The problem is that this file is updated whenever events occur on the server (usually more than half a second). How can I make it so that the webpage displays the file in real time, i.e. shows the live channel of the file?

I guess he will use javascript and AJAX, but my knowledge on both is pretty limited. Any pointers and help would be appreciated :)

+7
source share
5 answers

My answer uses PHP and Ajax, although switching to ASP or any other language will not be hard.
In the head

<script type="text/javascript"> function Ajax() { var $http, $self = arguments.callee; if (window.XMLHttpRequest) { $http = new XMLHttpRequest(); } else if (window.ActiveXObject) { try { $http = new ActiveXObject('Msxml2.XMLHTTP'); } catch(e) { $http = new ActiveXObject('Microsoft.XMLHTTP'); } } if ($http) { $http.onreadystatechange = function() { if (/4|^complete$/.test($http.readyState)) { document.getElementById('ReloadThis').innerHTML = $http.responseText; setTimeout(function(){$self();}, 1000); } }; $http.open('GET', 'loadtxt.php' + '?' + new Date().getTime(), true); $http.send(null); } } </script> 

In organism

  <script type="text/javascript"> setTimeout(function() {Ajax();}, 1000); </script> <div id="ReloadThis">Default text</div> </body> 

Now, using loadtxt.php, read the values โ€‹โ€‹of the text file

  <?php $file = "error.txt"; $f = fopen($file, "r"); while ( $line = fgets($f, 1000) ) { print $line; } ?> 
+8
source

Using jQuery you can do the following

 setInterval(function() { $('#element').load('/url/to/file'); }, 1000); 

Updates div with element identifier with file contents every 1 second

+7
source

You can use jQuery.get to get the file every few seconds and refresh the page to display the content.

0
source

There are various ways to do this ...

You can look into a lengthy survey .

Attach a meta refresh tag to refresh the page every X seconds.

tail -f /path/to/log.log in the terminal, a preview of the last few lines of this file will open - this is what I do if I need to read the error logs when debugging.

Or just refresh the page manually when you go, it can be frustrating if the page automatically changes its contents.

As you said, your file is very large, I would use the PHP file() function to simply grab the first sum of X lines from the file, in order to maintain bandwidth reduction and readability!

0
source

Others talked about loading the log file with each update, but depending on the file size, this error would be a bad idea. You might want to create a server-side page that will read the log file and track how many of them have already been provided to you, and give you only new bits. If its 10k file will be annoying (and possibly lags), so it is transmitted to you every second.

Otherwise, other people seem to have covered most of the client side.

0
source

All Articles