Differenze tra le versioni di "Programmazione:Java/Logging"

Da WikiSitech.
Vai alla navigazioneVai alla ricerca
 
(4 versioni intermedie di uno stesso utente non sono mostrate)
Riga 1: Riga 1:
{| cellspacing=2 width="100%"
+
[[Programmazione:Java|<< Back to Java]]
=Logging=
+
 
 +
=Log what is happened=
 +
Per avere un log 'utilizzabile' è necessario che l'eccezione venga loggata al massimo due volte:
  
Per loggare avere un log 'utilizzabile' è necessario che l'eccezione venga loggata al massimo due volte:
 
 
* '''Chi intercetta l'eccezione originale''' deve loggare senza lo stacktrace e trasformarla in un eccezione applicativa
 
* '''Chi intercetta l'eccezione originale''' deve loggare senza lo stacktrace e trasformarla in un eccezione applicativa
* '''Chi intercetta l'eccezione applicativa''' deve loggare l'eccezione completa di stacktrace
+
* '''Chi intercetta l'eccezione applicativa''' deve loggare l'eccezione completa di stacktrace  
  
tutti gli altri punti in cui passa l'eccezione devono propagarla senza intercettarla e riloggarla nuovamente
+
tutti gli altri punti in cui passa l'eccezione devono propagarla senza intercettarla e riloggarla nuovamente  
|- valign="top"
+
 
|width="50%"|
+
{| width="100%" cellspacing="10"
'''Bad Practice (case 1)'''
+
|-valign="top"  
 +
| width="50%" |
 +
==Bad Practice.....case 1==
 
<code java>
 
<code java>
 
public class A {
 
public class A {
   public static void f() throws AppException {
+
   public static void f() throws TExcept {
 
       try {
 
       try {
 
         // Do something
 
         // Do something
 
       } catch(Throwable t) {
 
       } catch(Throwable t) {
         throw new AppException(t);
+
        // non viene loggato la causa dell'eccezione
 +
         throw new TExcept(t);
 
       }
 
       }
 
   }
 
   }
Riga 25: Riga 29:
 
       try {
 
       try {
 
         A.f();
 
         A.f();
       } catch(AppException t) {
+
       } catch(TExcept t) {
 +
        // l'eccezione viene nascosta
 
       }       
 
       }       
 
   }
 
   }
 
}
 
}
 
</code>
 
</code>
|valign="top" |
+
| width="50%" |  
'''Bad Practice (case 2)'''
+
==Bad Practice.....case 2==
 
<code java>
 
<code java>
 
public class A {
 
public class A {
   public static void f() throws AppException {
+
   public static void f() throws TExcept {
 
       try {
 
       try {
 
         // Do something
 
         // Do something
 
       } catch(Throwable t) {
 
       } catch(Throwable t) {
         t.printStackTrace();
+
        // questo non è log...viene solo messo in console
         throw new AppException(t);
+
         t.printStackTrace();  
 +
         throw new TExcept(t);
 
       }
 
       }
 
   }
 
   }
Riga 48: Riga 54:
 
       try {
 
       try {
 
         A.f();
 
         A.f();
       } catch(AppException t) {
+
       } catch(TExcept t) {
 +
        // questo non è log...viene solo messo in console
 
         t.printStackTrace();
 
         t.printStackTrace();
 
       }       
 
       }       
 
   }
 
   }
}</code>
+
}
|-  
+
</code>
|'''Bad Practice (case 3)'''
+
|-
 +
| valign="top" colspan="2" |
 +
==Bad Practice.....case 3==
 
<code java>
 
<code java>
 
public class A {
 
public class A {
Riga 61: Riga 70:
 
         // Do something
 
         // Do something
 
       } catch(Throwable t) {
 
       } catch(Throwable t) {
 +
        // questo non è log...viene solo messo in console
 
         System.out.println(t.getMessage();
 
         System.out.println(t.getMessage();
 
         throw new TExcept(t);
 
         throw new TExcept(t);
Riga 72: Riga 82:
 
         A.f();
 
         A.f();
 
       } catch(TExcept t) {
 
       } catch(TExcept t) {
 +
        // questo non è log...viene solo messo in console
 
         System.out.println(t.getMessage();
 
         System.out.println(t.getMessage();
 
       }       
 
       }       
 
   }
 
   }
}</code>
+
}
 +
</code>
 +
|-
 +
| valign="top" colspan="2" |
 +
==Not Bad Practice but...==
 +
<code java>
 +
public class A {
 +
  public static void f() throws TExcept {
 +
      try {
 +
        // Do something
 +
      } catch(Throwable t) {
 +
        // viene loggato tutto lo stackTrace
 +
        log.error("error", t);
 +
        throw new TExcept(t);
 +
      }
 +
  }
 +
}
 +
 
 +
public class B  {
 +
  void f1() {
 +
      try {
 +
        A.f();
 +
      } catch(TExcept t) {
 +
        // viene loggato tutto lo stackTrace per la seconda volta
 +
        log.error("error", t);
 +
      }     
 +
  }
 +
}
 +
</code>
 +
|-
 +
| valign="top" colspan="2" |
 +
 
 +
==Best Practice==
 +
Nell'esempio della Best Practice è stato aggiunta la classe C per mostrare che bisogna fare con la propagazione dell'eccezione
 +
{| width="100%"
 +
|- valign="top"
 +
| width="50%" |
 +
Nel caso in cui l'eccezione sia di competenza di C
 +
<code java>
 +
public class A {
 +
  public static void f() throws TExcept {
 +
      try {
 +
        // Do something
 +
      } catch(Throwable t) {
 +
        log.error(t.getMessage());
 +
        throw new TExcept(t);
 +
      }
 +
  }
 +
}
 +
 
 +
public class B  {
 +
  public static void f1() throws TExcept {
 +
      A.f();
 +
  }
 +
}
 +
 
 +
public class C  {
 +
  void f2() {
 +
      try {
 +
        B.f1();
 +
      } catch(TExcept t) {
 +
        log.error("error", t);
 +
      }     
 +
  }
 +
}
 +
</code>
 +
| width="50%" |
 +
Nel caso in cui l'eccezione sia di competenza di B
 +
<code java>
 +
public class A {
 +
  public static void f() throws TExcept {
 +
      try {
 +
        // Do something
 +
      } catch(Throwable t) {
 +
        log.error(t.getMessage());
 +
        throw new TExcept(t);
 +
      }
 +
  }
 +
}
 +
 
 +
public class B  {
 +
  public static void f1() {
 +
      try {
 +
        A.f();
 +
      } catch(TExcept t) {
 +
        log.error("error", t);
 +
      }     
 +
  }
 +
}
 +
 
 +
public class C  {
 +
  void f2() {
 +
      B.f1();
 +
  }
 +
}
 +
</code>
 +
|}
 
|}
 
|}

Versione attuale delle 10:31, 14 apr 2008

<< Back to Java

Log what is happened

Per avere un log 'utilizzabile' è necessario che l'eccezione venga loggata al massimo due volte:

  • Chi intercetta l'eccezione originale deve loggare senza lo stacktrace e trasformarla in un eccezione applicativa
  • Chi intercetta l'eccezione applicativa deve loggare l'eccezione completa di stacktrace

tutti gli altri punti in cui passa l'eccezione devono propagarla senza intercettarla e riloggarla nuovamente

Bad Practice.....case 1

public class A {

  public static void f() throws TExcept {
     try {
        // Do something
     } catch(Throwable t) {
        // non viene loggato la causa dell'eccezione
        throw new TExcept(t);
     }
  }

}

public class B {

  void f1() {
     try {
        A.f();
     } catch(TExcept t) {
        // l'eccezione viene nascosta
     }      
  }

}

Bad Practice.....case 2

public class A {

  public static void f() throws TExcept {
     try {
        // Do something
     } catch(Throwable t) {
        // questo non è log...viene solo messo in console
        t.printStackTrace(); 
        throw new TExcept(t);
     }
  }

}

public class B {

  void f1() {
     try {
        A.f();
     } catch(TExcept t) {
        // questo non è log...viene solo messo in console
        t.printStackTrace();
     }      
  }

}

Bad Practice.....case 3

public class A {

  public static void f() throws TExcept {
     try {
        // Do something
     } catch(Throwable t) {
        // questo non è log...viene solo messo in console
        System.out.println(t.getMessage();
        throw new TExcept(t);
     }
  }

}

public class B {

  void f1() {
     try {
        A.f();
     } catch(TExcept t) {
        // questo non è log...viene solo messo in console
        System.out.println(t.getMessage();
     }      
  }

}

Not Bad Practice but...

public class A {

  public static void f() throws TExcept {
     try {
        // Do something
     } catch(Throwable t) {
        // viene loggato tutto lo stackTrace
        log.error("error", t);
        throw new TExcept(t);
     }
  }

}

public class B {

  void f1() {
     try {
        A.f();
     } catch(TExcept t) {
        // viene loggato tutto lo stackTrace per la seconda volta
        log.error("error", t);
     }      
  }

}

Best Practice

Nell'esempio della Best Practice è stato aggiunta la classe C per mostrare che bisogna fare con la propagazione dell'eccezione

Nel caso in cui l'eccezione sia di competenza di C public class A {

  public static void f() throws TExcept {
     try {
        // Do something
     } catch(Throwable t) {
        log.error(t.getMessage());
        throw new TExcept(t);
     }
  }

}

public class B {

  public static void f1() throws TExcept {
     A.f();
  }

}

public class C {

  void f2() {
     try {
        B.f1();
     } catch(TExcept t) {
        log.error("error", t);
     }      
  }

}

Nel caso in cui l'eccezione sia di competenza di B public class A {

  public static void f() throws TExcept {
     try {
        // Do something
     } catch(Throwable t) {
        log.error(t.getMessage());
        throw new TExcept(t);
     }
  }

}

public class B {

  public static void f1() {
     try {
        A.f();
     } catch(TExcept t) {
        log.error("error", t);
     }      
  }

}

public class C {

  void f2() {
     B.f1();
  }

}