details:   https://code.openbravo.com/erp/devel/pi/rev/9c333865058f
changeset: 32508:9c333865058f
user:      Asier Lostalé <asier.lostale <at> openbravo.com>
date:      Fri Jul 21 07:48:49 2017 +0200
summary:   fixed 36527: OBException should not log itself by default

  Changed default OBException behavior: now it only logs itself on creation if
  explicitly set or if loger level is debug

diffstat:

 src/org/openbravo/base/exception/OBException.java |  68 ++++++++++++++--------
 1 files changed, 43 insertions(+), 25 deletions(-)

diffs (114 lines):

diff -r 7bdae9313266 -r 9c333865058f 
src/org/openbravo/base/exception/OBException.java
--- a/src/org/openbravo/base/exception/OBException.java Mon Jul 24 10:58:16 
2017 +0200
+++ b/src/org/openbravo/base/exception/OBException.java Fri Jul 21 07:48:49 
2017 +0200
@@ -11,7 +11,7 @@
  * under the License. 
  * The Original Code is Openbravo ERP. 
  * The Initial Developer of the Original Code is Openbravo SLU 
- * All portions are Copyright (C) 2008-2015 Openbravo SLU 
+ * All portions are Copyright (C) 2008-2017 Openbravo SLU 
  * All Rights Reserved. 
  * Contributor(s):  ______________________________________.
  ************************************************************************
@@ -19,59 +19,77 @@
 
 package org.openbravo.base.exception;
 
+import org.apache.commons.lang.StringUtils;
 import org.apache.log4j.Logger;
 import org.openbravo.service.db.DbUtility;
 
 /**
  * This is the base exception for all exceptions in Openbravo. It is an 
unchecked exception which
- * also logs itself.
+ * logs itself if {@code logException=true} is used in constructor or it's 
logger has at least debug
+ * level.
  * 
  * @author mtaal
  */
 public class OBException extends RuntimeException {
-
   private static final long serialVersionUID = 1L;
   private boolean logExceptionNeeded;
 
   public OBException() {
+    this(false);
+  }
+
+  public OBException(boolean logException) {
     super();
-    getLogger().error("Exception", this);
+    logExceptionNeeded = logException;
+    log("Exception", this);
   }
 
   public OBException(String message, Throwable cause) {
-    this(message, cause, true);
+    this(message, cause, false);
+  }
+
+  public OBException(String message, Throwable cause, boolean logException) {
+    super(message, DbUtility.getUnderlyingSQLException(cause));
+    logExceptionNeeded = logException;
+    log(message, cause);
+  }
+
+  public OBException(String message) {
+    this(message, false);
   }
 
   public OBException(String message, boolean logException) {
     super(message);
     logExceptionNeeded = logException;
-    if (logException) {
-      getLogger().error(message, this);
-    }
-  }
-
-  public OBException(String message, Throwable cause, boolean logException) {
-    super(message, DbUtility.getUnderlyingSQLException(cause));
-    logExceptionNeeded = logException;
-    if (logException) {
-      getLogger().error(message, DbUtility.getUnderlyingSQLException(cause));
-    }
-  }
-
-  public OBException(String message) {
-    super(message);
-    getLogger().error(message, this);
+    log(message, this);
   }
 
   public OBException(Throwable cause) {
+    this(cause, false);
+  }
+
+  public OBException(Throwable cause, boolean logException) {
     super(cause);
+    logExceptionNeeded = logException;
+    log(null, cause);
+  }
+
+  private void log(String message, Throwable cause) {
+    boolean shouldLog = isLogExceptionNeeded() || getLogger().isDebugEnabled();
+    if (!shouldLog) {
+      return;
+    }
+
     Throwable foundCause = DbUtility.getUnderlyingSQLException(cause);
-    if (foundCause != cause) {
-      // passing foundCause ensures that the underlying stack trace is printed
-      getLogger().error(cause.getMessage() + " - " + foundCause.getMessage(), 
foundCause);
+
+    String msg;
+    if (StringUtils.isBlank(message)) {
+      msg = foundCause == cause ? cause.getMessage() : (cause.getMessage() + 
"-" + foundCause
+          .getMessage());
     } else {
-      getLogger().error(cause.getMessage(), cause);
+      msg = message;
     }
+    getLogger().error(msg, cause);
   }
 
   /**

------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most
engaging tech sites, Slashdot.org! http://sdm.link/slashdot
_______________________________________________
Openbravo-commits mailing list
Openbravo-commits@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/openbravo-commits

Reply via email to