How to replace printStackTrace with log.error in the below method

323 views Asked by At

Please suggest me how to replace the printstacktace lines with log.error

public class extends Throwable {
    
    private Throwable _prev;

    public synchronized Throwable getPrevThrowable() {
       return _prev;
    }
    
    public void printStackTrace(PrintStream ps) {
       super.printStackTrace(ps);
       Throwable t;
       if ((t = getPrevThrowable()) != null) {
           t.printStackTrace(ps);
       }
            }

Can I implement it like below ??

public class extends Throwable {
        
            public void printStackTrace(PrintStream ps) {
                log.error(ps);
                Throwable t;
                if ((t = getPrevThrowable()) != null) {
                    log.error(t);
                }
            }
1

There are 1 answers

2
rgoers On

I'm not sure what getPrevThrowable() does but you can do

Throwable t;
if ((t = getPrevThrowable()) != null {
    logger.error("", t);
}