简体   繁体   English

如何使用 Log4j 为 jsp 页面创建日志文件

[英]How create log file using Log4j for jsp page

I want to create log file using Log4j.我想使用 Log4j 创建日志文件。 I want to create such logs.我想创建这样的日志。

"2018-10-31 21:05:51,481 - DEBUG - u click button" “2018-10-31 21:05:51,481 - 调试 - 你点击按钮”

I downloaded log4j-2.3-bin .我下载了log4j-2.3-bin

Added files log4j-api-2.3.jar and log4j-api-2.3.jar , clicked "build path" and create log4j.xml .添加文件log4j-api-2.3.jarlog4j-api-2.3.jar ,单击“构建路径”并创建log4j.xml

<?xml version="1.0" encoding="UTF-8"?>
  <Configuration status="DEBUG">
   <Appenders>
     <Console name="Console" target="SYSTEM_OUT">
       <PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n" />
     </Console>
     <File name="MyFile" fileName="all.log" immediateFlush="false" append="false">
       <.PatternLayout pattern="%d{yyy-MM-dd HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/>
     </File>
   </Appenders>
   <Loggers>
     <Root level="debug">
       <AppenderRef ref="Console" />
       <AppenderRef ref="MyFile"/>
     </Root>
   </Loggers>
 </Configuration>

I am confused and do not quite understand how to finish.我很困惑,不太明白如何完成。

I'm using IDE eclipse for web-development with tomcat 8.5.我正在使用 IDE eclipse 通过 tomcat 8.5 进行 web 开发。

MyFile.jps我的文件

<form  method="POST">
    <input  type="submit" name="Clickme" value ="buttonclick" >
        <%
            String button1Click = request.getParameter("buttonclick");
            if(button1Click != null && button1Click.equals("buttonclick")){
        %>
            <p> click </p>
        <%  
            }
        %>
</form>

Create a dummy class, you need it to instantiate the logger:创建一个虚拟类,你需要它来实例化记录器:

public class JspLoggerClass {}

Import the logger and the class in your jsp:在您的 jsp 中导入记录器和类:

<%@ page import="org.apache.log4j.Logger, JspLoggerClass;" %>

Instantiate and use:实例化和使用:

  <%
    Logger logger = Logger.getLogger(JspLoggerClass.class);
    ...
    logger.info("message ...");
  %>

EDITED已编辑

I don't understand you very well.我不是很了解你。 Using log4j you can write only from java code.使用 log4j,您只能从 java 代码编写。 That for in jsp you use the logger in scriptlet only.在 jsp 中,您只能在 scriptlet 中使用记录器。 If you want to log the click event, that is javascript, so the only logger is console.log("...") but this prints on the browser console.如果你想记录点击事件,那就是 javascript,所以唯一的记录器是console.log("...")但这会打印在浏览器控制台上。 When you click the button, there is no way to get the event in jsp scriptlet, this code is executed on the server.当你点击按钮时,jsp scriptlet中没有办法获取事件,这段代码是在服务器上执行的。 What you can do is submit the form, go to a servlet on your server and there log the event.您可以做的是提交表单,转到服务器上的 servlet 并在那里记录事件。

EDITED 2编辑 2

OK, my fault, you are using 2.3 and that is log4j 2 .好的,我的错,你使用的是 2.3,那就是log4j 2 So include in your project / classpath log4j-core and log4j-api jars.所以在你的项目/类路径中包含log4j-corelog4j-api jars。 Import org.apache.logging.log4j.LogManager and org.apache.logging.log4j.Logger .导入org.apache.logging.log4j.LogManagerorg.apache.logging.log4j.Logger No need to create dummy class, you could instantiate the logger with a string name like this:无需创建虚拟类,您可以使用这样的字符串名称实例化记录器:

Logger logger = LogManager.getLogger("my jsp");

And now just use the logger:现在只需使用记录器:

logger.info("...");

Sorry, I cannot post comments.对不起,我不能发表评论。

I think you have an error in your log4j.xml , remove the dot before PatternLayout .我认为您的log4j.xml 中有错误,请删除PatternLayout之前的

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

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