简体   繁体   English

当我在Java Web应用程序中使用log4j单击html链接时保存日志记录消息

[英]Save Logging messages when I click html link using log4j in java web application

I need to save the logging messages into file when I click html link. 单击html链接时,我需要将日志消息保存到文件中。 I created dynamic web project and .jsp web file in eclipse. 我在Eclipse中创建了动态Web项目和.jsp Web文件。 Also I'm using log4j for logging. 另外,我正在使用log4j进行日志记录。

For now I create log4j.properties file to save logging messages in file. 现在,我创建log4j.properties文件以将日志消息保存在文件中。 Also I implemented Logger: 我也实现了Logger:

private static final Logger logger = LoggerFactory.getLogger(Application.class);

and the message: 和消息:

logger.info("start app");

But I need to save logging when I click some web page link or liked text from the created .jsp file. 但是,当我单击一些网页链接或来自创建的.jsp文件的喜欢的文本时,我需要保存日志记录。 Something like this logging message: 类似于以下日志消息:

2014-06-03 09:53:36,001 INFO root:01 - start app
2014-06-03 09:55:01,002 INFO root:02 - Selected: LINKED TEXT  

Please help or suggest. 请帮助或建议。

Thanks in advance 提前致谢

This is how JSP works. 这就是JSP的工作方式。

JSP is converted to a servlet, that runs in the server side, serving http requests. JSP转换为在服务器端运行的servlet,用于处理http请求。

When you click on a link in the browser, you're generating an event in the browser. 单击浏览器中的链接时,将在浏览器中生成事件。 This event may or may not generate a http request, and this request may or may not be sent to your servlet. 此事件可能会或可能不会生成http请求,并且此请求可能会或可能不会发送到您的servlet。

If you need to capture the events in the browser, you have two ways I guess 如果您需要在浏览器中捕获事件,我想有两种方法

[1] capture the events in the client side, using for example a browser plugin. [1]使用例如浏览器插件捕获客户端中的事件。 This is how automation plugins such as selenium works. 这就是诸如selenium之类的自动化插件的工作方式。 They are pieces of code that run in the browser (in the client). 它们是在浏览器(在客户端)中运行的代码。

[2] attach some javascript code in your page to the click event on every link, so if someone clicks on some anchor element, you also generate a http request to your servlet so you can log the event. [2]将页面中的一些javascript代码附加到每个链接上的click事件,因此,如果某人单击某个锚元素,则还会向servlet生成一个http请求,以便您记录该事件。 The problem of this approach is that you need javascript code for all your pages in order to do that. 这种方法的问题是,您需要所有页面的javascript代码才能执行此操作。

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

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