KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > exceptions > ExceptionsLoggingHandler


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19 /*
20  * ServerLoggingHandler.java
21  *
22  * Created on November 28, 2006, 6:10 PM
23  *
24  * To change this template, choose Tools | Template Manager
25  * and open the template in the editor.
26  */

27
28 package org.netbeans.modules.exceptions;
29
30 import java.io.File JavaDoc;
31 import java.io.FileWriter JavaDoc;
32 import java.io.IOException JavaDoc;
33 import java.io.Writer JavaDoc;
34 import java.util.logging.Formatter JavaDoc;
35 import java.util.logging.Handler JavaDoc;
36 import java.util.logging.LogRecord JavaDoc;
37 import java.util.logging.SimpleFormatter JavaDoc;
38 /**
39  *
40  * @author jindra
41  */

42 public class ExceptionsLoggingHandler extends Handler JavaDoc{
43     public static boolean logOUT = false;
44     private File JavaDoc logFile = null;
45     private FileWriter JavaDoc writer = null;
46     private Formatter JavaDoc formatter = new SimpleFormatter JavaDoc();
47     
48     /** Creates a new instance of ServerLoggingHandler */
49     public ExceptionsLoggingHandler() {
50     }
51     
52     public void publish(LogRecord JavaDoc arg0) {
53         try {
54             getWriter().write(formatter.format(arg0));
55             if (logOUT) System.out.print(formatter.format(arg0));
56         }
57         catch (IOException JavaDoc ex) {
58             java.util.logging.Logger.getLogger("global").log(java.util.logging.Level.SEVERE,
59                                                              ex.getMessage(), ex);
60         }
61     }
62     
63     public void flush(){
64         try {
65             getWriter().flush();
66         } catch (IOException JavaDoc ex) {
67             java.util.logging.Logger.getLogger("global").log(java.util.logging.Level.SEVERE,
68                     ex.getMessage(), ex);
69         }
70     }
71     
72     public void close() throws SecurityException JavaDoc {
73         try {
74             flush();
75             getWriter().close();
76         } catch (IOException JavaDoc ex) {
77             java.util.logging.Logger.getLogger("global").log(java.util.logging.Level.SEVERE,
78                     ex.getMessage(), ex);
79         }
80     }
81     
82     private Writer JavaDoc getWriter() throws IOException JavaDoc{
83         if (logFile == null)
84             logFile = new File JavaDoc("../logs/ExceptionsLogging.log");
85         if (writer == null)
86             writer = new FileWriter JavaDoc(logFile);
87         return writer;
88     }
89     
90 }
91
Popular Tags