要打印Java堆棧信息,可以使用以下方法:
try {
// 代碼塊
} catch (Exception e) {
e.printStackTrace();
}
import java.util.logging.Logger;
Logger logger = Logger.getLogger("LoggerName");
try {
// 代碼塊
} catch (Exception e) {
logger.severe("Exception occurred: " + e.getMessage());
for (StackTraceElement element : e.getStackTrace()) {
logger.severe(element.toString());
}
}
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Logger logger = LoggerFactory.getLogger("LoggerName");
try {
// 代碼塊
} catch (Exception e) {
logger.error("Exception occurred: " + e.getMessage());
for (StackTraceElement element : e.getStackTrace()) {
logger.error(element.toString());
}
}
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
Log log = LogFactory.getLog("LoggerName");
try {
// 代碼塊
} catch (Exception e) {
log.error("Exception occurred: " + e.getMessage());
for (StackTraceElement element : e.getStackTrace()) {
log.error(element.toString());
}
}