简体   繁体   中英

Send logs from a REST service to an HTML page

I have a REST service on a server A. The service is doing some stuff and logging some messages thanks to log4j.

Aside, I have a web page on server B that is calling the service thanks to AJAX and getting the response. Apart from receiving the response (which works fine to me), I would like to print on the page the log messages from the server side.

In other words, I would like that every time there is a new log message on server A side, the view display it.

Any ideas to achieve that ?

EDIT: How to use a websocket to retrieve logs from a log4j socket appender?

You would create a REST endpoint to retrieve the raw log data as text/plain . It could be something as following:

GET /logs HTTP/1.1
Accept: text/plain

You also could provide some query string parameters to filter the logs by date and time, as following:

GET /logs?from=2016-07-03T10:00:00Z&to=2016-07-04T10:00:00Z HTTP/1.1
Accept: text/plain

Then your client can request such endpoint, retrieve the data they want to and display the logs in a HTML page.

If you prefer rendering the HTML page on server side, instead of accepting text/plain , accept text/html .


For real time logging, you could consider WebSockets .

I finally found a way to solve my problem:

I have created a singleton logger. Every time I want to log a message, I am getting the instance of the logger and add the new message to the ConcurrentLinkedList of logs.

On the other hand, I have created a new rest service. Every second I call this service with an ajax query. It removes the log messages of the list and return them. The view display them.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM