I have a situation in which I want to print all the exception caught in catch block using logger.
try {
File file = new File("C:\\className").mkdir();
fh = new FileHandler("C:\\className\\className.log");
logger.addHandler(fh);
logger.setUseParentHandlers(false);
SimpleFormatter formatter = new SimpleFormatter();
fh.setFormatter(formatter);
} catch (Exception e) {
logger.info(e);
}
i got the error logger cannot be applied to java.io.Exception...
My concern is if I do so many thing in try block and I keep only one catch block as catch(Exception e), Then is there any way using logger that print any kind of exception caught in catch block ? Note: we are using java.util.logging.Logger API
You should probably clarify which logger are you using.
org.apache.commons.logging.Log
interface has methodvoid error(Object message, Throwable t)
(and methodvoid info(Object message, Throwable t)
), which logs the stack trace together with your custom message. Log4J implementation has this method too.So, probably you need to write:
If you need to log it with INFO level (though, it might be a strange use case), then:
Hope it helps.
You can use this method to log the exception stack to String
Use:
LOGGER.log(Level.INFO, "Got an exception.", e);
or
LOGGER.info("Got an exception. " + e.getMessage())
;Try to log the stack trace like below: