在java.util.logging可以初始化與自定義日誌格式記錄儀就像這裏:
log.error("Message", exception);
隨着java.util.logging
這可以通過以下方式進行:
private Logger initTextLogger() throws SecurityException, IOException {
Logger logger = Logger.getLogger(YourClass.class.getName());
FileHandler fileHandler = new FileHandler(logFilePath, false);
SimpleFormatter logFormatter = new SimpleFormatter() {
@Override
public String format(LogRecord record) {
String stacktrace = "";
Throwable t = record.getThrown();
if(t!=null){
StringWriter sw = new StringWriter();
t.printStackTrace(new PrintWriter(sw));
stacktrace = sw.toString();
}
return record.getLevel() + ": " + record.getMessage() + "\r\n"
+ stacktrace;
}
};
fileHandler.setFormatter(logFormatter);
logger.addHandler(fileHandler);
return logger;
}
任何標準的設施只需登錄stacktra CE?有時候,我只是不知道(或者不想)在郵件中寫些什麼 – 2015-10-28 04:09:03