user3681970
user3681970

Reputation: 1271

log not printing the full stack trace

I am trying to print the whole stack trace in my log file. Here are the sample codes which i have written for various classes. The output of log file is also given. I want the whole stack trace to be printed in log.

First Sample DAO where exception is generated :

public String getPasswordByEmail(String email) throws UserException {
    try {
        beginNewTransaction();
        PreparedStatement stmt = getConnection().prepareStatement(GET_PASSWORD_BY_EMAIL);
        stmt.setString(1, email);

        log.debug("PrepareStatement for selecting user password by email " + stmt.toString());

        ResultSet rs = stmt.executeQuery();
        if(rs != null) {
            rs.next();
        }
        return rs.getString("password");
    }
    catch(SQLException sqe) {
        throw new UserException("Could not retrieve user details",sqe);
    } catch (JiffieTransactionException jte) {
        throw new UserException("Error while securing the database connection", jte);
    }
}

Sample service class which calls the DAO:

public String getPasswordByEmail(String email) throws UserException{
    UserDAO userDao = new UserDAO();
    return userDao.getPasswordByEmail(email);
}

Sample action class which finally catch and log the exception :

 public String sendPasswordToEmail() {
    UserService newService = new UserService();
    String password;
    try {
        password = newService.getPasswordByEmail(email);
    } catch (UserException e) {

        log.error("Error in RetrievePasswordAction : "+e);
        addActionError("Email Id not registered");
        return "failure";
    }  /* more codes */

Output of log file :

23 Apr 2015 11:40:44,755 [http-bio-9999-exec-3] ERROR RetrievePasswordAction  - Error in RetrievePasswordAction : com.markEffy.aggregator.UserNotFoundException: Email id does not exist 

Here is my my log4j.xml

<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="ERROR"  DLog4jContextSelector="org.apache.logging.log4j.core.async.AsyncLoggerContext    Selector">
<Appenders>
<File name="myFile"  fileName="${sys:catalina.home}/logs/Aggregator.log">
  <PatternLayout pattern= "%d{dd MMM yyyy HH:mm:ss,SSS} [%t] %-5p %c{1} %X{user} - %m %n %throwable{full} %n"/>
  </File>
  </Appenders>
  <!-- <Policies>
    <SizeBasedTriggeringPolicy size="5 KB"/>
  </Policies> -->
 <!--  <DefaultRolloverStrategy max="0"/>      
</RollingRandomAccessFile>
<Async name="AsyncRollingFile">
  <AppenderRef ref="RollingFile"/>
</Async> --> 
<Console name="STDOUT" target="SYSTEM_OUT">
  <PatternLayout pattern="%d %-5p [%t] %C{2} (%F:%L) - %m%n%n"/>
</Console>

 <Loggers>
 <Logger name="org.apache.log4j.xml" level="debug">
  <AppenderRef ref="myFile"/>
</Logger>
<Root level="debug">
  <AppenderRef ref="myFile"/>
</Root>
</Loggers>

I want the whole stack trace to come in log file. any help will be deeply appreciated.

Upvotes: 2

Views: 6149

Answers (2)

Remko Popma
Remko Popma

Reputation: 36754

The standard way to log an exception to get a full stack trace is

try {
    ...
} catch (SomeException ex) {
    logger.error("Could not retrieve password", ex); // separate with a comma
}

Your code snippet says log.error("Error in RetrievePasswordAction : "+e). The problem is that you are using a plus between the error message and the exception object. The plus concatenates the string on the left to the object on the right, resulting in a new string. This new string then gets logged. This is equivalent to:

// concatenate String+e.toString() into a new String
String msg = "Error in RetrievePasswordAction : "+e; 
log.error(msg); // then log that new string; not what you intended...

Upvotes: 7

0o&#39;-Varun-&#39;o0
0o&#39;-Varun-&#39;o0

Reputation: 735

catch (UserException e) {
        log.error("Error in RetrievePasswordAction : "+e);
        for(int i= 0; i<e.getStackTrace().length; i++){
            log.error("+++" +e.getStackTrace()[i] +"+++");
        }

I Hope this might help you. Not the best solution but works : )

Upvotes: 1

Related Questions