java 无法在jsp页面中使用登录
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14019249/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Can't use logging in jsp page
提问by Mahmoud Saleh
i am using the following libraries for logging:
我正在使用以下库进行日志记录:
- slf4j-api 1.6.4
- slf4j-log4j12 1.6.4
- commons-logging 1.1.1
- slf4j-api 1.6.4
- slf4j-log4j12 1.6.4
- 公共日志记录 1.1.1
and my log4j.propertiesfile:
和我的log4j.properties文件:
log.dir=logs
rrd.dir=${log.dir}/rrd
datestamp=yyyy-MM-dd/HH:mm:ss.SSS
roll.pattern.hourly=.yyyy-MM-dd.HH
roll.pattern.daily=.yyyy-MM-dd
log4j.rootLogger=ON
log4j.appender.myConsoleAppender=org.apache.log4j.ConsoleAppender
log4j.appender.myConsoleAppender.layout=org.apache.log4j.PatternLayout
log4j.appender.myConsoleAppender.layout.ConversionPattern=%d{${datestamp}} [%t] %-5p %m%n
log4j.appender.myFileAppender=org.apache.log4j.DailyRollingFileAppender
log4j.appender.myFileAppender.DatePattern=${roll.pattern.hourly}
log4j.appender.myFileAppender.File=${log.dir}/MyAPP.log
log4j.appender.myFileAppender.layout=org.apache.log4j.PatternLayout
log4j.appender.myFileAppender.layout.ConversionPattern=%d{${datestamp}} [%t] %-5p %m%n
log4j.logger.com.myapp=DEBUG, myConsoleAppender, myFileAppender
in the jsp pagei do the following:
在jsp页面中,我执行以下操作:
<%@page import="org.apache.commons.logging.*"%>
<%
Log log = LogFactory.getLog(getClass());
log.debug("login page");
NOTE:logging is working fine in java classes.
注意:日志记录在 Java 类中工作正常。
please advise why it's not working, thanks.
请告知为什么它不起作用,谢谢。
采纳答案by Mahmoud Saleh
i just added the following line to log4j.propertiesand it works fine now:
我刚刚在log4j.properties 中添加了以下行,现在工作正常:
log4j.logger.org.apache.jsp=DEBUG, myConsoleAppender, myFileAppender
回答by Shashi
To use logger in the jsp file, initialize the logger object in the following way:
要在 jsp 文件中使用 logger,请按以下方式初始化 logger 对象:
<% Logger logger = LoggerFactory.getLogger(this.getClass()); %>
回答by Harish Raj
Just like any other custom tag library, you must properly configure it for your web application. This means putting the log.tld file into the WEB-INF directory and installing the Log tag JAR file into the WEB-INF/lib directory. You will also need to add the appropriate taglib element to your web application's deployment descriptor, similar to how you added the Struts tag libraries:
就像任何其他自定义标记库一样,您必须为您的 Web 应用程序正确配置它。这意味着将 log.tld 文件放入 WEB-INF 目录并将 Log tag JAR 文件安装到 WEB-INF/lib 目录中。您还需要将适当的 taglib 元素添加到 Web 应用程序的部署描述符中,类似于添加 Struts 标记库的方式:
Web.xml
网页.xml
<taglib>
<taglib-uri>/WEB-INF/log.tld</taglib-uri>
<taglib-location>/WEB-INF/log.tld</taglib-location>
</taglib>
in your JSP,
在您的 JSP 中,
<%@ taglib uri="/WEB-INF/log.tld" prefix="logger" %>
<logger:debug message="This is a debug message from a jsp using the Log tag" />
<html>
<head>
<title>Using the Log Tag in a JSP page</title>
</head>
<body>
<logger:info message="This is another message using the log4j tag" />
There should be two log messages in the log4j log file.
</body>
</html>
This approach is much cleaner with a larger and more complex JSP page
这种方法对于更大和更复杂的 JSP 页面更清晰
回答by Ahmed
1- in your configurationFile:log4j.xml
<logger name="processus.jsp">
<level value="info" />
<appender-ref ref="fileAppender" />
</logger>
1- 在您的配置文件中:log4j.xml
<logger name="processus.jsp">
<level value="info" />
<appender-ref ref="fileAppender" />
</logger>
2- in the page JSP: `<%@ page import="org.apache.log4j.Logger"%>
2-在页面JSP中:`<%@ page import="org.apache.log4j.Logger"%>
<% Logger logger = Logger.getLogger( "yourPage.jsp" ); %>`
<% Logger logger = Logger.getLogger("yourPage.jsp"); %>`
回答by Muhammad Imran Tariq
Its best to make a logger class in java that will initialize logging.
最好在java中创建一个记录器类来初始化日志记录。
See this linkfor java class. Call its logDebug() method from JSP to log messages like LogClassName.logDebug("message here");
有关java 类,请参阅此链接。从 JSP 调用它的 logDebug() 方法来记录消息,例如LogClassName.logDebug("message here");