KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > easybeans > log > JDKConsoleHandler


1 /**
2  * EasyBeans
3  * Copyright (C) 2006 Bull S.A.S.
4  * Contact: easybeans@objectweb.org
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
19  * USA
20  *
21  * --------------------------------------------------------------------------
22  * $Id:$
23  * --------------------------------------------------------------------------
24  */

25
26 package org.objectweb.easybeans.log;
27
28 import java.io.OutputStreamWriter JavaDoc;
29 import java.io.Writer JavaDoc;
30 import java.util.logging.ErrorManager JavaDoc;
31 import java.util.logging.Handler JavaDoc;
32 import java.util.logging.Level JavaDoc;
33 import java.util.logging.LogRecord JavaDoc;
34
35 /**
36  * Handler that display info message in System.out and error in System.err
37  * because JDK implementation prints all messages in System.err and in Eclipse,
38  * for example, INFO messages are in red !
39  * @author Florent Benoit
40  */

41 public class JDKConsoleHandler extends Handler JavaDoc {
42
43     /**
44      * Writer used for traces INFO, FINE, FINEST, etc.
45      */

46     private Writer JavaDoc infoOutputWriter;
47
48     /**
49      * Writer used for WARNING, SEVERE, FATAL.
50      */

51     private Writer JavaDoc errOutputWriter;
52
53     /**
54      * Creates a new handler with two appender, System.out and System.err.
55      */

56     public JDKConsoleHandler() {
57         super();
58         infoOutputWriter = new OutputStreamWriter JavaDoc(System.out);
59         errOutputWriter = new OutputStreamWriter JavaDoc(System.err);
60     }
61
62     /**
63      * Prints the record on the correct writer.
64      * @param logRecord the given record with all info to log.
65      */

66     @Override JavaDoc
67     public void publish(final LogRecord JavaDoc logRecord) {
68         // Do nothing if not loggable
69
if (!isLoggable(logRecord)) {
70             return;
71         }
72
73         // Format the record
74
String JavaDoc msg = null;
75         try {
76             msg = getFormatter().format(logRecord);
77         } catch (Exception JavaDoc ex) {
78             reportError(null, ex, ErrorManager.FORMAT_FAILURE);
79             return;
80         }
81
82         // Get the correct writer
83
Writer JavaDoc writer = null;
84         if (logRecord.getLevel().intValue() >= Level.WARNING.intValue()) {
85             writer = errOutputWriter;
86         } else {
87             writer = infoOutputWriter;
88         }
89
90         // Write the message
91
try {
92             writer.write(msg);
93         } catch (Exception JavaDoc ex) {
94             reportError(null, ex, ErrorManager.WRITE_FAILURE);
95         }
96
97         // Flush the current writer
98
flush(writer);
99     }
100
101     /**
102      * Flush the given writer.
103      * @param writer the writer to flush.
104      */

105     private void flush(final Writer JavaDoc writer) {
106         try {
107             writer.flush();
108         } catch (Exception JavaDoc ex) {
109             reportError(null, ex, ErrorManager.FLUSH_FAILURE);
110         }
111     }
112
113     /**
114      * Flush all the current writers (info and error).
115      */

116     @Override JavaDoc
117     public void flush() {
118         flush(errOutputWriter);
119         flush(infoOutputWriter);
120     }
121
122     /**
123      * Close the handler by flushing all writers.
124      */

125     @Override JavaDoc
126     public void close() {
127         flush();
128     }
129 }
130
Popular Tags