Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JasperException does not appear in log4j file

Tags:

spring

log4j

I have a website built with Spring, and a logging engine with Log4J. Everything is logged fine, except errors in JSP like this one:

org.apache.jasper.JasperException: /views/user/registration_form.jsp (line: 80, column: 107)

The error appears in the Eclipse console, but not in the log4j default files. Any idea?

Thanks a lot!

EDIT : My log4j configuration file:

# Root logger option
log4j.rootLogger=DEBUG, infofile, errorfile, stdout

# Log "INFO" messages to a log file
log4j.appender.infofile=org.apache.log4j.RollingFileAppender
log4j.appender.infofile.File=/Library/apache-tomcat-8.0.9/logs/info.log
log4j.appender.infofile.MaxFileSize=1MB
log4j.appender.infofile.MaxBackupIndex=1
log4j.appender.infofile.layout=org.apache.log4j.PatternLayout
log4j.appender.infofile.layout.ConversionPattern=%d{dd-MM-yyyy HH:mm:ss} %-5p %c{1}:%L - %m%n
log4j.appender.infofile.threshold=INFO

# Log "ERROR" messages to a log file
log4j.appender.errorfile=org.apache.log4j.RollingFileAppender
log4j.appender.errorfile.File=/Library/apache-tomcat-8.0.9/logs/error.log
log4j.appender.errorfile.MaxFileSize=1MB
log4j.appender.errorfile.MaxBackupIndex=1
log4j.appender.errorfile.layout=org.apache.log4j.PatternLayout
log4j.appender.errorfile.layout.ConversionPattern=%d{dd-MM-yyyy HH:mm:ss} %-5p %c{1}:%L - %m%n
log4j.appender.errorfile.threshold=ERROR

# Log "INFO" messages to the console
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.out
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{dd-MM-yyyy HH:mm:ss} %-5p %c{1}:%L - %m%n
log4j.appender.stdout.threshold=INFO

log4j.logger.org.springframework.integration.expression.ExpressionUtils=ERROR
# Hibernate
#log4j.logger.org.hibernate=INFO, hb
#log4j.logger.org.hibernate.SQL=DEBUG
#log4j.logger.org.hibernate.type=TRACE
#log4j.logger.org.hibernate.hql.ast.AST=info
#log4j.logger.org.hibernate.tool.hbm2ddl=warn
#log4j.logger.org.hibernate.hql=debug
#log4j.logger.org.hibernate.cache=info
#log4j.logger.org.hibernate.jdbc=debug

EDIT 2 : more precisely, we are able to handle ANY exception in our Spring MVC application, except those thrown by the view page (JasperException for example).

like image 273
siebmanb Avatar asked Jul 28 '15 14:07

siebmanb


People also ask

Where can I find log4j configuration file?

The file is named log4j. properties and is located in the $DGRAPH_HOME/dgraph-hdfs-agent/lib directory. The file defines the ROLLINGFILE appenders for the root logger and also sets the log level for the file. The level of the root logger is defined as INFO and attaches the ROLLINGFILE appender to it.

How do I enable debug mode in log4j?

You need to set the logger level to the lowest you want to display. For example, if you want to display DEBUG messages, you need to set the logger level to DEBUG. The Apache log4j manual has a section on Configuration. Show activity on this post.

What is log4j Appender file file?

The log4j. properties file is a log4j configuration file which stores properties in key-value pairs. The log4j properties file contains the entire runtime configuration used by log4j. This file will contain log4j appenders information, log level information and output file names for file appenders.


1 Answers

First thing to try would be to configure org.springframework.web.servlet.handler.SimpleMappingExceptionResolver to handle JasperException (or any kind for that matter?). Strangely I could not get it to work (it doesn't seem to be involved at all in the stack).

Something of the sort:

<bean
    class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver"
    p:defaultErrorView="uncaughtException">
    <property name="exceptionMappings">
        <props>
            <prop key=".AccessDeniedException">error403</prop>
            <prop key=".DataAccessException">error500</prop>
            <prop key=".NoSuchRequestHandlingMethodException">error404</prop>
            <prop key=".TypeMismatchException">error404</prop>
            <prop key=".MissingServletRequestParameterException">error404</prop>
            <prop key=".TimeoutException">errorTimeout</prop>
            <prop key=".JasperException">error500</prop>

        </props>
    </property>
    <property name="warnLogCategory"
        value="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver" />
    <property name="defaultStatusCode" value="500" />
</bean>

If that isn't enough, you can always specify your own extension of SimpleMappingExceptionResolver.

Now if that doesn't work at all (it did not for me), you can provide your own javax.servlet.Filter and that will for sure work (though may not be the "spring-est" way).

Something like:

public class MyExceptionHandlerFilter extends GenericFilterBean {

public void doFilter(javax.servlet.ServletRequest request, javax.servlet.ServletResponse response, javax.servlet.FilterChain chain) throws java.io.IOException ,javax.servlet.ServletException {
    try {
        chain.doFilter(request, response);
    } catch (Exception e) {
        // do something with the exception here...
    }
}

}

In a Spring 3/4 Java Config, you could put that class above in a @Configuration class like so:

@Configuration
public class MyConfig {
  ...
  @Bean
  public Filter exceptionSink() {
    return new MyExceptionHandlerFilter();
  }
  ...
}

(There's really a thousand ways to do it in Spring...as usual). And if you're using web.xml, it'd look like this:

<filter>
    <filter-name>ExceptionHandlerFilter</filter-name>
    <filter-class>com.test.MyExceptionHandlerFilter</filter-class>
</filter>
...
<filter-mapping>
    <filter-name>ExceptionHandlerFilter</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>

If you can provide a stacktrace of the error, it might shed some light on your filter chain (would be looking for any "springframework" classes in there) to see if there could be a less verbose/more spring-ish way to go about it. The chain will be different depending on your config (e.g. might see some spring-security filters in there if you're using it, etc.).

like image 59
lpezet Avatar answered Oct 17 '22 01:10

lpezet