我想知道是否有办法使用log4j在日志文件中的jsf-facelet(xhtml)页面上记录EL(表达式语言)错误?我尝试了以下记录器,但徒劳无功..
<logger name="javax.faces">
<level value="WARN" />
<appender-ref ref="appLogFile"/>
</logger>
<logger name="javax.servlet">
<level value="WARN" />
<appender-ref ref="appLogFile"/>
</logger>
<logger name="org.apache">
<level value="WARN" />
<appender-ref ref="appLogFile"/>
</logger>
实际上我在JSF EL中有一些简单的数学计算,如#{intA/intB}
#{intA-intB-intC}
等,并希望记录客户浏览我们的Web应用程序时是否发生错误。
任何提示都表示赞赏: - )
答案 0 :(得分:0)
您可以将其添加到log4j属性或XML,
#log4j.properties
log4j.rootLogger=DEBUG,myapp
log4j.appender.myapp=org.apache.log4j.RollingFileAppender
log4j.appender.myapp.file=/var/log/appLogFile.log
log4j.appender.myapp.maxFileSize=10MB
log4j.appender.myapp.maxBackupIndex=10
log4j.appender.myapp.layout=org.apache.log4j.PatternLayout
log4j.appender.myapp.layout.ConversionPattern=%5p | %d | %F:%L | %m%n
log4j.logger.javax.faces=DEBUG
log4j.logger.javax.el=ERROR
log4j.logger.com.sun.faces=DEBUG
<logger name="javax.faces">
<level value="WARN" />
<appender-ref ref="appLogFile"/>
</logger>
<logger name="javax.el">
<level value="ERROR" />
<appender-ref ref="appLogFile"/>
</logger>
或
如果您想使用错误页面,
<error-page>
<exception-type>javax.el.ELException</exception-type>
<location>/elError.xhtml</location>
</error-page>
elError.xhtml可以像是,
<h:panelGrid>
<h:outputText value="Exception Occured:" />
<h:outputText value="#{ErrorBean.message}" />
</h:panelGrid>
创建ManagedBean以获取错误并记录到数据库或log4j,
@ManagedBean
public class ErrorBean{
public String getMessage() {
FacesContext context = FacesContext.getCurrentInstance();
Map requestMap = context.getExternalContext().getRequestMap();
Throwable exception = (Throwable)
requestMap.get("javax.servlet.error.exception");
// Log the error in database or log4j
return ex.getMessage();
}
}