如果代码里到处都是logger,明显不太好维护,而显得凌乱。
我们在throw时就把日志记录了,是最好的选择,代码如下:
package com.uet.common.exception; import org.slf4j.Logger; import org.slf4j.LoggerFactory; /** * 数据获得:运行时RuntimeException * <h3>使用场景</h3> * <p>Action层使用</p> * @author huangjs * @version 2014-6-18 23:34:44 */ public class ActionAccessException extends RuntimeException { private static final long serialVersionUID = 1L; public ActionAccessException() { super(); getLogger().error("Exception", this); } public ActionAccessException(String message, Throwable cause) { super(message, cause); getLogger().error(message, cause); } public ActionAccessException(String message) { super(message); getLogger().error(message, this); } public ActionAccessException(Throwable cause) { super(cause); getLogger().error(cause.getMessage(), cause); } /** * This method returns a logger which can be used by a subclass. The logger * is specific for the instance of the Exception (the subclass). * * @return the class-specific Logger */ protected Logger getLogger() { return LoggerFactory.getLogger(getClass()); } }