KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > util > monolog > wrapper > javaLog > ConsoleHandler


1 /**
2  * Copyright (C) 2004 France Telecom R&D
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  */

18 package org.objectweb.util.monolog.wrapper.javaLog;
19
20 import org.objectweb.util.monolog.wrapper.common.OutputStreamSwitcher;
21 import org.objectweb.util.monolog.api.BasicLevel;
22
23 import java.io.OutputStream JavaDoc;
24 import java.io.Writer JavaDoc;
25 import java.io.OutputStreamWriter JavaDoc;
26 import java.io.UnsupportedEncodingException JavaDoc;
27 import java.util.logging.LogRecord JavaDoc;
28 import java.util.logging.ErrorManager JavaDoc;
29
30 /**
31  * The aim of this class is to permit to specify the output for the console
32  * handler of the java.util.logging system.
33  *
34  * This console handler is also able to choose the right ouput (System.err
35  * or System.out) depending on the message level.
36  *
37  * @author S.Chassande-Barrioz
38  */

39 public class ConsoleHandler extends java.util.logging.Handler JavaDoc {
40
41     protected OutputStreamSwitcher oss;
42     private Writer JavaDoc writer;
43
44     public ConsoleHandler() {
45         super();
46     }
47
48     public void desactivateSwitching(OutputStream JavaDoc newOut) {
49         if (oss != null) {
50             oss = null;
51             setOutput(newOut);
52         }
53     }
54
55     public void activateSwitching() {
56         if (oss == null) {
57             oss = new OutputStreamSwitcher();
58             setOutput(oss);
59         }
60     }
61
62     /**
63      * Assign the Outputstream by calling a protected method from the super
64      * class.
65      */

66     public void setOutput(OutputStream JavaDoc out) throws SecurityException JavaDoc {
67         if (out == null) {
68             throw new NullPointerException JavaDoc();
69         }
70         flush();
71         String JavaDoc encoding = getEncoding();
72         if (encoding == null) {
73             writer = new OutputStreamWriter JavaDoc(out);
74         } else {
75             try {
76                 writer = new OutputStreamWriter JavaDoc(out, encoding);
77             } catch (UnsupportedEncodingException JavaDoc ex) {
78             // This shouldn't happen. The setEncoding method
79
// should have validated that the encoding is OK.
80
throw new Error JavaDoc("Unexpected exception " + ex);
81             }
82         }
83     }
84
85
86     public void publish(LogRecord JavaDoc record) {
87         if (!isLoggable(record)) {
88             return;
89         }
90         String JavaDoc msg;
91         try {
92             msg = getFormatter().format(record);
93         } catch (Exception JavaDoc ex) {
94             // We don't want to throw an exception here, but we
95
// report the exception to any registered ErrorManager.
96
reportError(null, ex, ErrorManager.FORMAT_FAILURE);
97             return;
98         }
99
100         if (oss != null) {
101             if (record.getLevel().intValue() >= BasicLevel.WARN) {
102                 oss.switchOutput(System.err);
103             } else {
104                 oss.switchOutput(System.out);
105             }
106         }
107         try {
108             writer.write(msg);
109         } catch (Exception JavaDoc ex) {
110             // We don't want to throw an exception here, but we
111
// report the exception to any registered ErrorManager.
112
reportError(null, ex, ErrorManager.WRITE_FAILURE);
113         }
114         flush();
115     }
116
117     public void flush() {
118         if (writer != null) {
119             try {
120                 writer.flush();
121             } catch (Exception JavaDoc ex) {
122                 // We don't want to throw an exception here, but we
123
// report the exception to any registered ErrorManager.
124
reportError(null, ex, ErrorManager.FLUSH_FAILURE);
125             }
126         }
127     }
128
129     public void close() throws SecurityException JavaDoc {
130         flush();
131     }
132 }
Popular Tags