Web implementation of “tail -f filename”?

后端 未结 4 521
别跟我提以往
别跟我提以往 2020-12-31 14:49

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 t

相关标签:
4条回答
  • 2020-12-31 15:19

    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

    0 讨论(0)
  • 2020-12-31 15:20

    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.

    0 讨论(0)
  • 2020-12-31 15:31

    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

    0 讨论(0)
  • 2020-12-31 15:35

    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.

    0 讨论(0)
提交回复
热议问题