My bad, I did not debug my servlet properly. If had done that earlier I would 
have seen that it was the extraction of the exception from the request object 
that failed.

When running the webapp in Weblogic as we did earlier, the following code 
worked as expected and the exception was properly fetched:

Object obj = request.getAttribute("javax.servlet.jsp.jspException"); 

When the webapp is running in Tomcat (version 6.0.18, running on Windows server 
2003 as a service, standard "package" downloaded from Tomcat homepage, standard 
Tomcat log settings) the call resulted in "obj" being set to null.

When I changed the code to,

Object obj = request.getAttribute("javax.servlet.error.exception");

... The exception is properly fetched and everyting works as expected also in 
Tomcat

/Andreas

-----Ursprungligt meddelande-----
Från: Imner, Andreas [mailto:andreas.im...@kgk.se] 
Skickat: den 10 juni 2009 11:09
Till: Tomcat Users List
Ämne: RE: Why do stacktraces from servlets print to stdout, when the other end 
up in the log4j -log file?

Hi

The Servlet does not propegate the error. This perticular servlet is one we use 
for handling "unhandled" exceptions and displaying an unexpected-error page to 
the user.
_________________________________
From web.xml

<servlet>
        <servlet-name>exceptionHandler</servlet-name>
        
<servlet-class>se.kgk.webshop.web.general.ExceptionHandlerServlet</servlet-class>
        <init-param>
                <param-name>errorPageURL</param-name>
                <param-value>goto.do?forward=error</param-value>
        </init-param>
        <load-on-startup>3</load-on-startup>
    </servlet>

<servlet-mapping>
                <servlet-name>exceptionHandler</servlet-name>
                <url-pattern>/error</url-pattern>
        </servlet-mapping>

<error-page>
          <exception-type>java.lang.Throwable</exception-type>
          <location>/error</location>
        </error-page>
____________________________________

The servlet code looks something like this 

    private static final Log log = 
LogFactory.getLog(ExceptionHandlerServlet.class);

    protected void doPost(final HttpServletRequest request, final 
HttpServletResponse response)
            throws ServletException {
        try {
            Throwable throwable = null;

            (.... Some code to extract the throwable, the requestURI etc... )
            
            log.error("Exception while handling request: " + requestURI, 
throwable);

            request.getRequestDispatcher(errorPageURL).forward(request, 
response);

        } catch (final Throwable t) {
           t.printStackTrace();
        }
    }

____________________________________


If we run the webapp in Weblogic, both the "Exception while handling..." 
message and the stacktrace gets printed to the log file specified by log4j

If we run exactly the same webapp in Tomcat, the "Exception while handling..." 
message is printed to the log4j -logfile but the stacktrace gets printed to 
stdout.log!?! 
Note that this only applies to log calls from within this servlet. Then doing 
the exact same log4j-call from within one of our filter -classes, everything 
works as expected. 

I suspect something is different with the servlet load sequence or log settings 
when using Tomcat, but what?



-----Ursprungligt meddelande-----
Från: Christopher Schultz [mailto:ch...@christopherschultz.net] 
Skickat: den 9 juni 2009 21:57
Till: Tomcat Users List
Ämne: Re: Why do stacktraces from servlets print to stdout, when the other end 
up in the log4j -log file?

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Andreas,

On 6/9/2009 3:31 PM, Imner, Andreas wrote:
> My companys web application uses log4j 1.2.8 for logging and have
> just recently switched from Weblogic to Tomcat 6.0.18.
> 
> Since the we switched to Tomcat, when we do some logging within a
> servlet
> 
> " private static final Log log =
> LogFactory.getLog(ExceptionHandlerServlet.class);
> log.error("some message", throwable);
> "
> 
> The "some message" -line is written to the file specified by log4j,
> but why does the stacktrace gets written to Tomcats stdout.log?

That depends on what else is going on in your servlet. If you let the
exception propagate (by re-throwing it or throwing a new exception) and
nobody else catches it, then Tomcat's request processing thread code
will catch it and log it to stdout.

> This is our log4j.properties file
> 
> "
> log4j.appender.A1=org.apache.log4j.RollingFileAppender
> log4j.appender.A1.MaxBackupIndex=20
> log4j.appender.A1.MaxFileSize=10MB
> 
> log4j.appender.A1.layout=org.apache.log4j.PatternLayout
> 
> log4j.appender.A1.layout.ConversionPattern=%d{ABSOLUTE} %-5p %X{ak} [%c{1}] 
> %m%n
> log4j.rootLogger=ERROR, A1
> 
> log4j.category.se=INFO
> 
> # All System.out.println is redirected to stdout logger, INFO level
> log4j.category.stdout=INFO
> 
> # All System.err.println (includes Exception.printStackTrace() is redirected 
> to stderr logger, INFO level
> log4j.category.stderr=INFO
> "

The code you provided does not show any use of the "stdout" category,
but it doesn't really matter because the stdout category does not
actually log to stdout :)

Check to see that your code is not allowing exceptions to propagate.

- -chris
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.9 (MingW32)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/

iEYEARECAAYFAkouvpkACgkQ9CaO5/Lv0PDwrACgsjUoqePn7xzDy5qVoBukKdOz
iboAn2qKdOJxCDKQ+/mtp5vDnbv1v5ZN
=AvA2
-----END PGP SIGNATURE-----

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscr...@tomcat.apache.org
For additional commands, e-mail: users-h...@tomcat.apache.org

Reply via email to