views:

127

answers:

3

Hiya,

I'm trying to find a efficient way to watch the server log on a webpage, i don't mind building an app i just can't work out the best way to do it.

Is there a way to keep a stream open to a file with php and to the browser? or will it have to be done by polling the file every x seconds?

Thanks in advance,

Shadi

A: 

The best way to do it is to use AJAX to pull the file content every x seconds, giving the illusion of real time.

If you do want real time, you can use an XMPP server, but from what I can see, the first solution is far sufficient and does't require a lot of work.

e-satis
Could you explain the XMPP Server part a little more, or point me the direction of relevant information as i can't seem to find something that would work for what i'm doing...
Shadi Almosri
XMPP is a communication protocole (just like HTTP, the one your use for web pages). But unlike HTTP, XMPP is suited for real time communication and can do so stuff like data pushin data and load balancing. Using XMPP usually require a dedicated server, the Ejabberd is one of the most famous but require the knowledge in Erlang. This is a very powerful solution, but it's likely overkill, you'll need a huge amount of work just to do something AJAX would handle fine in an hour.
e-satis
+1  A: 

Remember that the web model is essentially stateless (disconnected). Having that in mind when a client submits a request, the server processes the request and then send a response accordingly. You can have track of the clients action using cookies and/or sessions, but the resources reserved for a request are released after the response is submitted back. I think that the best way to meet your goal, is to develop a web services that checks for the status of the log and fetch the diff (if any). Your app may consist of a web page with a div that will display the diff from the web service. A script with a timer will trigger the call to the web service. I will try to do something like this in a few weeks, and I will post the entire solution on moropo blog (spanish). You can ask for a post translation using the comments.

Moropo
+1  A: 

The best solution is definitely AJAX in some capacity. The only way to have the server "push" to you the way you describe (maintain an open stream) would require the HTTP connection to remain open which would ultimately trigger timeouts and consume a lot of resources. I would look into the Cometd library. The downside to this is that I believe it depends on Java although the site does mention perl, python and "other languages." In the worst case, you could use a specific jetty implementation just for log monitoring on a specific port. Regardless, that framework would most likely be your best bet.

Any web-based chat mechanism essentially uses a push architecture and would be good to look at for some inspiration. In this case, instead of users creating messages that are fired to other users, the server creates the events (when a log message is generated). Check out this article on Facebook chat for some insight into how they do it. Google chat might be worth looking into if you can find some stuff on the architecture.

For the actual logging, I'm not sure if you are in need of help for that, but log4php which is currently under incubation might be a good place to start as it provides you with a configuration that can simultaneously log to an arbitrary number of "loggers" like database, file, socket, etc. You could likely find one that would allow you to tie it into whatever push framework you elect to use.

Good luck!

Chris Thompson