简体   繁体   English

将日志从REST服务发送到HTML页面

[英]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. 我在服务器A上有一个REST服务。由于使用了log4j,该服务正在做一些事情并记录了一些消息。

Aside, I have a web page on server B that is calling the service thanks to AJAX and getting the response. 另外,我在服务器B上有一个网页,该网页感谢AJAX并获得响应。 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. 换句话说,我希望每次服务器A端都有新的日志消息时,视图都会显示它。

Any ideas to achieve that ? 有什么想法可以实现?

EDIT: How to use a websocket to retrieve logs from a log4j socket appender? 编辑:如何使用websocket从log4j套接字追加程序检索日志?

You would create a REST endpoint to retrieve the raw log data as text/plain . 您将创建一个REST端点,以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. 然后,您的客户端可以请求此类端点,检索他们想要的数据,并在HTML页面中显示日志。

If you prefer rendering the HTML page on server side, instead of accepting text/plain , accept text/html . 如果您希望在服务器端呈现HTML页面,而不是接受text/plain ,请接受text/html


For real time logging, you could consider WebSockets . 对于实时日志记录,可以考虑使用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. 每次我想记录一条消息时,我都会获取记录器的实例,并将新消息添加到日志的ConcurrentLinkedList中。

On the other hand, I have created a new rest service. 另一方面,我创建了一个新的休息服务。 Every second I call this service with an ajax query. 我每秒都会用ajax查询调用此服务。 It removes the log messages of the list and return them. 它删除列表的日志消息并返回它们。 The view display them. 视图显示它们。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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