0

I have a web application based on Struts 2 MVC. I had to configure proper logging in the application which i already did and its working fine.

What i now want is, i need all the runtime exceptions and error due to system failure (such as lost database connection, any third party service is down etc etc), to be logged in my application log file.

I read about Thread.UncaughtExceptionHandler and also gone through this answer but still unable to get a start for this requirement.

Can someone suggest me how to initiate with this? What classes to make? What to configure? etc etc. Also to tell, there is no multithreading involved in my application.

Thank you!

Below is my log4j.properties

log4j.appender.file=org.apache.log4j.RollingFileAppender
log4j.appender.file.File=${catalina.home}/logs/backlogApp.log
log4j.appender.file.MaxFileSize=1000KB
log4j.appender.file.MaxBackupIndex=1
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-4p %m%n
log4j.appender.DatePattern='.'yyyy-MM-dd

# Root logger option
log4j.rootLogger=WARN, file
log4j.logger.com.nagarro=WARN

# OFF Hibernate and Spring
log4j.logger.org.hibernate=OFF
log4j.logger.org.springframework=OFF

# OFF all JDBC parameters
log4j.logger.org.hibernate.type=OFF

Also, the global exception handler in struts.xml

<global-results>
        <result name="error">/jsp/errorpage.jsp</result>
        <result name="sessionexpired">/jsp/login.jsp</result>
    </global-results>

    <global-exception-mappings>
        <exception-mapping exception="java.lang.Exception"
            result="error" />
        <exception-mapping exception="javax.persistence.PersistenceException"
            result="error" />
    </global-exception-mappings>
Community
  • 1
  • 1
roger_that
  • 9,493
  • 18
  • 66
  • 102

1 Answers1

0

This should basically happen automatically; how are you configuring your logging?

The existing "exception" interceptor can do this, although it is intended more as a way to implement a "go to this page if an exception occurs".

You could use the same idea and write a thin interceptor around action invocation that only logs the relevant exception information and continues on to the action's original result name, but that seems a little wonky, since an RTE should almost certainly interrupt normal flow.

IMO if you're wrapping up functionality that might throw an RTE then:

  • That functionality should already live in a service, and...
  • ...that service should catch exceptions and wrap them in an app-specific exception, and...
  • ...then you can use the existing "exception" interceptor.
Dave Newton
  • 158,873
  • 26
  • 254
  • 302
  • No, its not about redirecting to an error page if some exception occurred, that i already did using global exception handler mapping in struts.xml. What i need is, suppose i perform an action which calls a third party service but that service is down(say a web service), then my eclipse console shows the exception of **wsdl not found** , but when i tend to see in tomcat/logs directory, i can't find it. Now, in future if this happens at production, how am i going to track the root cause? This is my ultimate problem. – roger_that Jul 17 '13 at 17:05
  • Please see the edits done. I have added the logging configuration. I don't think that is causing problem cause, the exceptions logged are going to the file properly. – roger_that Jul 17 '13 at 17:17
  • @VaibhavShukla Then what's the problem? – Dave Newton Jul 17 '13 at 17:30
  • How to track if any system failure occurs? Where to look for that? Ideally, if something bad happens to application, we check our logs either application log file, or tomcat's catalina but in my case as i explained you in above comment, I am unable to track that. There are no logs for that error, no catalina file for tomcat. I am dumbstruck. – roger_that Jul 17 '13 at 17:34
  • @VaibhavShukla Then your logging is configured incorrectly. If you don't set `logEnabled=true` for the exception interceptor I don't think it'll log exceptions it processes. You'd also need to set the appropriate log level. http://struts.apache.org/release/2.2.x/docs/exception-interceptor.html – Dave Newton Jul 17 '13 at 17:48