Web implementation of "tail -f filename"?
Asked Answered
U

5

11

I have a log file and want to create a webpage (possibly Python but not strictly) that will work much like unix "tail -f filename" command works (show new log lines when they are written to file).

So that user will continuously see log right in browser.

How would you implement this?

Untune answered 2/11, 2010 at 13:54 Comment(0)
M
9

Tailon is a python webapp that, among other things, provides tail -f like functionality. In addition, wtee (a sister project of tailon) can make all its stdin viewable in the browser - its use is identical to the unix tee command: tail -f filename | wtee

Makell answered 20/6, 2013 at 16:51 Comment(0)
S
2

I implemented this using jquery (.ajax) and php (json).

The flow is essentially as follows:

  • user calls an html page on their browser
  • html page contains an initial jquery .ajax call to a remote php script on the server that performs the required function, in this case, retrieving a few of the last lines of the file being 'tailed'
  • if no new lines are available, the php script just loops (wile the ajax caller waits, ie longpolling), and can be configured to time out if necessary (returning an appropriate value back to the ajax calling function on the client)
  • when new lines are detected by the php script, they are wrapped in a json response and sent back to the ajax calling function on the browser, which then appends it to the existing content of the page.
  • The javascript function will then recursively make the same ajax call, effectively sitting in an infinite loop.

In my specific implementation, i did the following:

  • both the ajax call on the client AND the php script on the server have timeouts to handle, for example, broken connections nicely. Also ensures the ajax call does not wait forever.
  • the ajax call passes a line number as a reference back to the server to tell it what the last line number was that it received, so the server knows which lines to return. Initially the value is zero, and the server will immediately return the last 10 lines of the file
  • when the php script is called, it uses the clients last line number to do a quick check on the file; if new lines have already been added it returns them immediately, if not it sits in a loop (1 second) and then instead checks the files ctime (or mtime) to detect when new lines are written. This is more effective than counting the lines in the file (which could be huge) every second.

See my longpolling/realtime tail implementation using jquery and php here: https://github.com/richardvk/web_file_tail

Saintmihiel answered 2/10, 2015 at 5:7 Comment(0)
C
1

Scullog, having capability of sharing the local drive to the browser. Stream the log file via Socket.IO over browser. It run on any platform such as windows/linux/mac. It run as service or standalone mode.

Clan answered 3/10, 2016 at 17:55 Comment(1)
this should be the accepted answer. Work perfectly. Thanks for making it.Defluxion
M
0

You read the file and print the last lines to the page. You might also use a GET-variable to you define number of rows to output using ?n=x where x is the number of lines.

Mcgaw answered 11/9, 2012 at 22:0 Comment(1)
How to make it u0dated automatically?Endogamy
C
0

Disclaimer : I wrote this https://www.npmjs.com/package/tail-fweb

You can browse code on npm. This can be installed as a command

install -g tail-fweb

And used like tail -f

tail-fweb -port 3000 -f file.log
Clerk answered 25/7, 2023 at 17:1 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.