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

Da WikiSitech.
Vai alla navigazioneVai alla ricerca
 
(2 versioni intermedie di uno stesso utente non sono mostrate)
Riga 1: Riga 1:
 +
[[Programmazione:Java|<< Back to Java]]
 +
 
=Log what is happened=
 
=Log what is happened=
 
Per avere un log 'utilizzabile' è necessario che l'eccezione venga loggata al massimo due volte:
 
Per avere un log 'utilizzabile' è necessario che l'eccezione venga loggata al massimo due volte:
Riga 7: Riga 9:
 
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  
  
{| width="100%"
+
{| width="100%" cellspacing="10"
 
|-valign="top"  
 
|-valign="top"  
 
| width="50%" |
 
| width="50%" |
Riga 88: Riga 90:
 
|-
 
|-
 
| valign="top" colspan="2" |
 
| valign="top" colspan="2" |
==Not Bad Practice but...2 stack trace==
+
==Not Bad Practice but...==
 
<code java>
 
<code java>
 
public class A {
 
public class A {
Riga 107: Riga 109:
 
         A.f();
 
         A.f();
 
       } catch(TExcept t) {
 
       } catch(TExcept t) {
 +
        // viene loggato tutto lo stackTrace per la seconda volta
 
         log.error("error", t);
 
         log.error("error", t);
 
       }       
 
       }       
Riga 114: Riga 117:
 
|-
 
|-
 
| valign="top" colspan="2" |
 
| valign="top" colspan="2" |
 +
 
==Best Practice==
 
==Best Practice==
 
Nell'esempio della Best Practice è stato aggiunta la classe C per mostrare che bisogna fare con la propagazione dell'eccezione
 
Nell'esempio della Best Practice è stato aggiunta la classe C per mostrare che bisogna fare con la propagazione dell'eccezione

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();
  }

}