log4j 输出完整 e.printStackTrace() 同等异常信息


public void erroStackTrace(Object obj) {
		if (obj instanceof Exception) {
			Exception eObj = (Exception) obj;
			StringWriter sw = null;
			PrintWriter pw = null;
			try {
				sw = new StringWriter();
				pw = new PrintWriter(sw);
				String exceptionStack = "";
				eObj.printStackTrace(pw);
				exceptionStack = sw.toString();
				logger.error(exceptionStack);
			} catch (Exception e) {
				e.printStackTrace();
			} finally {
				try {
					pw.close();
					sw.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		} else {
			logger.error(obj);
		}
	}

 将异常信息转换成String 再通过 log.error(); 输出到 log4j 的日志文件

你可能感兴趣的:(java)