FAQ

[Tomcat-users] ServletException logging changes between 5.5 and 6.0/7.0?

C C
Feb 9, 2012 at 7:41 pm
Hello,

I'm seeing a difference in the way Tomcat handles logging exceptions thrown by a servlet, and I wonder if it's a configuration change or if this is simply how Tomcat is expected to behave.

Tomcat versions tested: 5.5.26, 6.0.35, 7.0.25
OS: Windows 7
Java: 1.5.0_22 for Tomcat 5.5.26 and 6.0.35, 1.6.0_24 for Tomcat 7.0.25

To test, I created a webapp with a single servlet, ExceptionServlet, that simply throw a ServletException in its service() method, e.g.:

public class ExceptionServlet extends HttpServlet {
protected void service(HttpServletRequest arg0, HttpServletResponse arg1)

throws ServletException, IOException {
throw new ServletException("HEY SERVLET EXCEPTION");
}
}


Further, the webapp uses log4j (1.2.16) and commons-logging (1.1.1). The log4j.properties for the webapp looks like this:

log4j.rootLogger=INFO,A
log4j.appender.A=org.apache.log4j.RollingFileAppender

log4j.appender.A.file=/opt/personal/logs/ex/ex.info.log
log4j.appender.A.layout=org.apache.log4j.PatternLayout
log4j.appender.A.layout.conversionPattern=%m%n
log4j.appender.A.maxFileSize=10MB
log4j.appender.A.maxBackupIndex=10


In a default deployment of Tomcat 5.5.26, I drop the webapp in the webapps directory, start up Tomcat, and call the ExceptionServlet. The resultant exception message and stack trace appear in the log file specified by the webapp's log4j.properties file, as I would expect.

Servlet.service() for servlet ex threw exception

javax.servlet.ServletException: HEY SERVLET EXCEPTION
at personal.ExceptionServlet.service(ExceptionServlet.java:17)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:269)
...


However, in a default deployment of Tomcat 6.0.35 or Tomcat 7.0.25, when I drop the webapp in the webapps directory, start up the respective Tomcat, and invoke the ExceptionServlet, the resultant exception message and stack trace appear in the Tomcat's log file, e.g. $TOMCAT_HOME/logs/localhost.2012-02-09.log. It's pretty much the same log message as above:

# from Tomcat 7.0.25
SEVERE: Servlet.service() for servlet [ex] in context with path [/et] threw exception [HEY SERVLET EXCEPTION] with root cause
javax.servlet.ServletException: HEY SERVLET EXCEPTION
at personal.ExceptionServlet.service(ExceptionServlet.java:17)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
...


I made no configuration changes to any of the Tomcat installations.

So I guess my questions are:

1. Is it correct to expect that exceptions thrown by a servlet should be logged in the webapp's log files?
2. If so, what changed between Tomcat 5.5 and Tomcat 6.0/7.0 that would cause exceptions thrown by a servlet to be logged in Tomcat's log files, or what configuration change do I need to make so that the exception appears in the webapp's logs?

Thanks in advance for any insights into my problem!

Chris

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

Search Discussions

10 responses

  • Konstantin Kolinko at Feb 9, 2012 at 8:03 pm

    2012/2/9 C C <ccblip@yahoo.com>:
    Hello,

    I'm seeing a difference in the way Tomcat handles logging exceptions thrown by a servlet, and I wonder if it's a configuration change or if this is simply how Tomcat is expected to behave.

    Tomcat versions tested: 5.5.26, 6.0.35, 7.0.25
    OS: Windows 7
    Java: 1.5.0_22 for Tomcat 5.5.26 and 6.0.35, 1.6.0_24 for Tomcat 7.0.25

    To test, I created a webapp with a single servlet, ExceptionServlet, that simply throw a ServletException in its service() method, e.g.:

    public class ExceptionServlet extends HttpServlet {
    protected void service(HttpServletRequest arg0, HttpServletResponse arg1)

    throws ServletException, IOException {
    throw new ServletException("HEY SERVLET EXCEPTION");
    }
    }


    Further, the webapp uses log4j (1.2.16) and commons-logging (1.1.1). The log4j.properties for the webapp looks like this:

    log4j.rootLogger=INFO,A
    log4j.appender.A=org.apache.log4j.RollingFileAppender

    log4j.appender.A.file=/opt/personal/logs/ex/ex.info.log
    log4j.appender.A.layout=org.apache.log4j.PatternLayout
    log4j.appender.A.layout.conversionPattern=%m%n
    log4j.appender.A.maxFileSize=10MB
    log4j.appender.A.maxBackupIndex=10


    In a default deployment of Tomcat 5.5.26, I drop the webapp in the webapps directory, start up Tomcat, and call the ExceptionServlet. The resultant exception message and stack trace appear in the log file specified by the webapp's log4j.properties file, as I would expect.

    Servlet.service() for servlet ex threw exception

    javax.servlet.ServletException: HEY SERVLET EXCEPTION
    at personal.ExceptionServlet.service(ExceptionServlet.java:17)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:269)
    ...


    However, in a default deployment of Tomcat 6.0.35 or Tomcat 7.0.25, when I drop the webapp in the webapps directory, start up the respective Tomcat, and invoke the ExceptionServlet, the resultant exception message and stack trace appear in the Tomcat's log file, e.g. $TOMCAT_HOME/logs/localhost.2012-02-09.log. It's pretty much the same log message as above:

    # from Tomcat 7.0.25
    SEVERE: Servlet.service() for servlet [ex] in context with path [/et] threw exception [HEY SERVLET EXCEPTION] with root cause
    javax.servlet.ServletException: HEY SERVLET EXCEPTION
    at personal.ExceptionServlet.service(ExceptionServlet.java:17)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
    ...


    I made no configuration changes to any of the Tomcat installations.

    So I guess my questions are:

    1. Is it correct to expect that exceptions thrown by a servlet should be logged in the webapp's log files?
    No.

    Out of the blue I would think that it would be logged into the same
    file as if using Servlet.log() call. That goes to Context log file.

    Thus it went to the localhost.log file, because you have not
    configured a separate one for your context (like e.g. the manager app
    does). I do not know why your configuration worked in 5.5.

    Note that logging implementation has changed between 5.5 and 6.0 (as
    mentioned in the Migration Guide [1]) in that sense that Tomcat no
    longer provides commons-logging.jar library to be used by
    applications, but uses its own renamed version of the classes. Maybe
    it is related.

    [1] http://tomcat.apache.org/migration.html
    2. If so, what changed between Tomcat 5.5 and Tomcat 6.0/7.0 that would cause exceptions thrown by a servlet to be logged in Tomcat's log files, or what configuration change do I need to make so that the exception appears in the webapp's logs?

    Thanks in advance for any insights into my problem!

    Chris
    Best regards,
    Konstantin Kolinko

    ---------------------------------------------------------------------
    To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
    For additional commands, e-mail: users-help@tomcat.apache.org
  • Christopher Schultz at Feb 9, 2012 at 9:01 pm
    Chris,
    On 2/9/12 2:40 PM, C C wrote:
    To test, I created a webapp with a single servlet,
    ExceptionServlet, that simply throw a ServletException in its
    service() method, e.g.:

    public class ExceptionServlet extends HttpServlet { protected void
    service(HttpServletRequest arg0, HttpServletResponse arg1)

    throws ServletException, IOException { throw new
    ServletException("HEY SERVLET EXCEPTION"); } }


    Further, the webapp uses log4j (1.2.16) and commons-logging
    (1.1.1). The log4j.properties for the webapp looks like this:

    log4j.rootLogger=INFO,A
    log4j.appender.A=org.apache.log4j.RollingFileAppender

    log4j.appender.A.file=/opt/personal/logs/ex/ex.info.log
    log4j.appender.A.layout=org.apache.log4j.PatternLayout
    log4j.appender.A.layout.conversionPattern=%m%n
    log4j.appender.A.maxFileSize=10MB
    log4j.appender.A.maxBackupIndex=10
    So log4j.properties is located in WEB-INF/classes?
    Where is log4j.jar?

    Do you have any other code in this example webapp -- for instance,
    something that actually loads the log4j.properties file?

    In order to use log4j with Tomcat 6.0 and 7.0, you need to download
    some Tomcat "extras" as described here:
    http://tomcat.apache.org/tomcat-7.0-doc/logging.html#Using_Log4j

    So if you haven't done that, then log4j will not be used at all,
    unless you have both written some code to initialize log4j during
    webapp startup and written an error handler to capture errors and log
    them to some kind of log.
    In a default deployment of Tomcat 5.5.26, I drop the webapp in the
    webapps directory, start up Tomcat, and call the ExceptionServlet.
    The resultant exception message and stack trace appear in the log
    file specified by the webapp's log4j.properties file, as I would
    expect.
    I wouldn't expect that at all, unless your log4j.properties file lives
    at the server level and not at the webapp level. So, which is it?

    - -chris


    ---------------------------------------------------------------------
    To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
    For additional commands, e-mail: users-help@tomcat.apache.org
  • Caldarale, Charles R at Feb 9, 2012 at 9:06 pm

    From: Christopher Schultz
    Subject: Re: ServletException logging changes between 5.5 and 6.0/7.0?
    In order to use log4j with Tomcat 6.0 and 7.0, you need to download
    some Tomcat "extras" as described here:
    http://tomcat.apache.org/tomcat-7.0-doc/logging.html#Using_Log4j
    Just to be clear: that's only when you want Tomcat to use log4j for its own logging; nothing to do with any logging implementation inside the webapp itself.

    - Chuck


    THIS COMMUNICATION MAY CONTAIN CONFIDENTIAL AND/OR OTHERWISE PROPRIETARY MATERIAL and is thus for use only by the intended recipient. If you received this in error, please contact the sender and delete the e-mail and its attachments from all computers.
  • Christopher Schultz at Feb 9, 2012 at 9:16 pm
    Chuck,
    On 2/9/12 4:06 PM, Caldarale, Charles R wrote:
    From: Christopher Schultz
    Subject: Re: ServletException logging changes between 5.5 and
    6.0/7.0?
    In order to use log4j with Tomcat 6.0 and 7.0, you need to
    download some Tomcat "extras" as described here:
    http://tomcat.apache.org/tomcat-7.0-doc/logging.html#Using_Log4j
    Just to be clear: that's only when you want Tomcat to use log4j
    for its own logging; nothing to do with any logging implementation
    inside the webapp itself.
    Yes, with one exception: use of ServletContext.log(). If you use that
    for logging (which I happen to think is foolish), then you'll need to
    configure your logging at the Tomcat-level.

    - -chris


    ---------------------------------------------------------------------
    To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
    For additional commands, e-mail: users-help@tomcat.apache.org
  • Ken dias at Feb 9, 2012 at 9:23 pm
    unsubscribe

    From: Chuck.Caldarale@unisys.com
    To: users@tomcat.apache.org
    Date: Thu, 9 Feb 2012 15:06:39 -0600
    Subject: RE: ServletException logging changes between 5.5 and 6.0/7.0?
    From: Christopher Schultz
    Subject: Re: ServletException logging changes between 5.5 and 6.0/7.0?
    In order to use log4j with Tomcat 6.0 and 7.0, you need to download
    some Tomcat "extras" as described here:
    http://tomcat.apache.org/tomcat-7.0-doc/logging.html#Using_Log4j
    Just to be clear: that's only when you want Tomcat to use log4j for its own logging; nothing to do with any logging implementation inside the webapp itself.

    - Chuck


    THIS COMMUNICATION MAY CONTAIN CONFIDENTIAL AND/OR OTHERWISE PROPRIETARY MATERIAL and is thus for use only by the intended recipient. If you received this in error, please contact the sender and delete the e-mail and its attachments from all computers.
  • Caldarale, Charles R at Feb 9, 2012 at 9:27 pm

    From: ken dias
    Subject: RE: ServletException logging changes between 5.5 and 6.0/7.0?
    unsubscribe
    Zero-for-three...

    http://tomcat.apache.org/lists.html#tomcat-users

    - Chuck


    THIS COMMUNICATION MAY CONTAIN CONFIDENTIAL AND/OR OTHERWISE PROPRIETARY MATERIAL and is thus for use only by the intended recipient. If you received this in error, please contact the sender and delete the e-mail and its attachments from all computers.


    ---------------------------------------------------------------------
    To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
    For additional commands, e-mail: users-help@tomcat.apache.org
  • Ken dias at Feb 9, 2012 at 9:24 pm
    unsubscribe

    From: Chuck.Caldarale@unisys.com
    To: users@tomcat.apache.org
    Date: Thu, 9 Feb 2012 15:06:39 -0600
    Subject: RE: ServletException logging changes between 5.5 and 6.0/7.0?
    From: Christopher Schultz
    Subject: Re: ServletException logging changes between 5.5 and 6.0/7.0?
    In order to use log4j with Tomcat 6.0 and 7.0, you need to download
    some Tomcat "extras" as described here:
    http://tomcat.apache.org/tomcat-7.0-doc/logging.html#Using_Log4j
    Just to be clear: that's only when you want Tomcat to use log4j for its own logging; nothing to do with any logging implementation inside the webapp itself.

    - Chuck


    THIS COMMUNICATION MAY CONTAIN CONFIDENTIAL AND/OR OTHERWISE PROPRIETARY MATERIAL and is thus for use only by the intended recipient. If you received this in error, please contact the sender and delete the e-mail and its attachments from all computers.
  • C C at Feb 9, 2012 at 10:00 pm
    Hi Christopher,

    ----- Original Message -----
    From: Christopher Schultz <chris@christopherschultz.net>
    To: Tomcat Users List <users@tomcat.apache.org>
    Cc:
    Sent: Thursday, February 9, 2012 1:00 PM
    Subject: Re: ServletException logging changes between 5.5 and 6.0/7.0?

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

    Chris,
    On 2/9/12 2:40 PM, C C wrote:
    To test, I created a webapp with a single servlet,
    ExceptionServlet, that simply throw a ServletException in its
    service() method, e.g.:
    ... snip ...
    So log4j.properties is located in WEB-INF/classes?
    Where is log4j.jar?

    My log4j.properties is in WEB-INF/classes, and log4j.jar is in WEB-INF/lib.
    Do you have any other code in this example webapp -- for instance,
    something that actually loads the log4j.properties file?

    I don't have any other code in my example webapp. The only code is that ExceptionServlet. In the ExceptionServlet I don't have any code that loads log4j.properties or configures log4j in any way.
    In order to use log4j with Tomcat 6.0 and 7.0, you need to download
    some Tomcat "extras" as described here:
    http://tomcat.apache.org/tomcat-7.0-doc/logging.html#Using_Log4j

    Thanks, I've done this, and found it doesn't really address my problem. My problem is not that I want Tomcat6/7 to use log4j for logging, my problem is that, when my ExceptionServlet throws the ServletException, on TC5.5 the exception was logged in the webapp's log, whereas on TC6/7 it was logged in Tomcat's localhost.log.

    Konstantin provided a response which seemed to make sense: an exception thrown by a servlet is probably logged using Servlet.log(), so the log message should end up in the Context file. Because my webapp also had commons-logging.jar in WEB-INF/lib, that was probably interfering with / taking over the commons-logging.jar that is shipped with TC5.5. If I remove all the logging jars from my webapp on TC5.5, the behavior looks like TC6/7, where the servlet's exception is logged in Tomcat's log.

    So if you haven't done that, then log4j will not be used at all,
    unless you have both written some code to initialize log4j during
    webapp startup and written an error handler to capture errors and log
    them to some kind of log.
    In a default deployment of Tomcat 5.5.26, I drop the webapp in the
    webapps directory, start up Tomcat, and call the ExceptionServlet.
    The resultant exception message and stack trace appear in the log
    file specified by the webapp's log4j.properties file, as I would
    expect.
    I wouldn't expect that at all, unless your log4j.properties file lives
    at the server level and not at the webapp level. So, which is it?

    Again, log4j.jar and log4j.properties at the webapp level. I think my understanding of where a servlet exception should be logged is incorrect, and the fact my webapp had commons-logging.jar somehow interfered with where the exception was logged. Does that sound about right?

    Thanks,
    Chris

    ---------------------------------------------------------------------
    To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
    For additional commands, e-mail: users-help@tomcat.apache.org
  • Christopher Schultz at Feb 9, 2012 at 10:49 pm
    Chris,
    On 2/9/12 5:00 PM, C C wrote:
    From: Christopher Schultz <chris@christopherschultz.net>

    So log4j.properties is located in WEB-INF/classes? Where is
    log4j.jar?
    My log4j.properties is in WEB-INF/classes, and log4j.jar is in
    WEB-INF/lib.
    Okay.
    Do you have any other code in this example webapp -- for
    instance, something that actually loads the log4j.properties
    file?
    I don't have any other code in my example webapp. The only code is
    that ExceptionServlet. In the ExceptionServlet I don't have any
    code that loads log4j.properties or configures log4j in any way.
    I wouldn't expect Tomcat to configure log4j for you in that case. I'm
    surprised that it somehow worked in Tomcat 5.5.
    In order to use log4j with Tomcat 6.0 and 7.0, you need to
    download some Tomcat "extras" as described here:
    http://tomcat.apache.org/tomcat-7.0-doc/logging.html#Using_Log4j
    Thanks, I've done this, and found it doesn't really address my
    problem. My problem is not that I want Tomcat6/7 to use log4j for
    logging, my problem is that, when my ExceptionServlet throws the
    ServletException, on TC5.5 the exception was logged in the
    webapp's log, whereas on TC6/7 it was logged in Tomcat's
    localhost.log.
    Gotcha. I know for my webapps, I explicitly configure error handlers
    in web.xml to catch errors, log them, and show nice messages to users.
    Konstantin provided a response which seemed to make sense: an
    exception thrown by a servlet is probably logged using
    Servlet.log(), so the log message should end up in the Context
    file. Because my webapp also had commons-logging.jar in
    WEB-INF/lib, that was probably interfering with / taking over the
    commons-logging.jar that is shipped with TC5.5. If I remove all
    the logging jars from my webapp on TC5.5, the behavior looks like
    TC6/7, where the servlet's exception is logged in Tomcat's log.
    It's quite possible that Tomcat 5.5 wasn't re-naming packages to avoid
    this kind of class interference, and that Tomcat was logging to the
    webapp's logger. That's not how I understand the class and resource
    loading stuff to work, but I've been wrong before. Perhaps Tomcat was
    using the "wrong" context class loader when trying to load the logging
    configuration and getting yours. That might do it.
    I wouldn't expect that at all, unless your log4j.properties file
    lives at the server level and not at the webapp level. So, which
    is it?
    Again, log4j.jar and log4j.properties at the webapp level. I think
    my understanding of where a servlet exception should be logged is
    incorrect, and the fact my webapp had commons-logging.jar somehow
    interfered with where the exception was logged. Does that sound
    about right?
    Right: configure one or more error-page(s) in web.xml and point them
    at a servlet that can log the errors appropriately. On the other hand,
    you can configure Tomcat to log stuff that goes to
    ServletContext.log() to go to the log file of your choice: you just
    have to set it up using CATALINA_BASE/conf/logging.properties instead
    of inside your webapp (which is one reason I wouldn't favor doing it:
    because your logging configuration is outside of your webapp's control).

    Hope that helps,
    - -chris



    ---------------------------------------------------------------------
    To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
    For additional commands, e-mail: users-help@tomcat.apache.org
  • Konstantin Kolinko at Feb 9, 2012 at 11:07 pm

    So log4j.properties is located in WEB-INF/classes? Where is
    log4j.jar?
    My log4j.properties is in WEB-INF/classes, and log4j.jar is in
    WEB-INF/lib.
    Okay.
    Do you have any other code in this example webapp -- for
    instance, something that actually loads the log4j.properties
    file?
    I don't have any other code in my example webapp. The only code is
    that ExceptionServlet. In the ExceptionServlet I don't have any
    code that loads log4j.properties or configures log4j in any way.
    I wouldn't expect Tomcat to configure log4j for you in that case. I'm
    surprised that it somehow worked in Tomcat 5.5.
    It makes sense.

    Tomcat 5.5 comes with full commons-logging library and it can
    autodetect if log4j is available.

    In Tomcat 6.0 the default logging library has that detection code removed.
    You have to download alternative copy of the library from "extras" to
    re-enable autodetection (as was already mentioned in this thread
    below).
    In order to use log4j with Tomcat 6.0 and 7.0, you need to
    download some Tomcat "extras" as described here:
    http://tomcat.apache.org/tomcat-7.0-doc/logging.html#Using_Log4j
    (...)

    Best regards,
    Konstantin Kolinko

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

Related Discussions