KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > petals > util > monolog > wrapper > javalog > ConsoleHandler


1 /**
2  * PETALS - PETALS Services Platform.
3  * Copyright (c) 2005 EBM Websourcing, http://www.ebmwebsourcing.com/
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
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  * -------------------------------------------------------------------------
19  * $Id: ConsoleHandler.java 12:18:02 ddesjardins $
20  * -------------------------------------------------------------------------
21  */

22 /**
23  * Copyright (C) 2004 France Telecom R&D
24  *
25  * This library is free software; you can redistribute it and/or
26  * modify it under the terms of the GNU Lesser General Public
27  * License as published by the Free Software Foundation; either
28  * version 2 of the License, or (at your option) any later version.
29  *
30  * This library is distributed in the hope that it will be useful,
31  * but WITHOUT ANY WARRANTY; without even the implied warranty of
32  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
33  * Lesser General Public License for more details.
34  *
35  * You should have received a copy of the GNU Lesser General Public
36  * License along with this library; if not, write to the Free Software
37  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
38  */

39 package org.objectweb.petals.util.monolog.wrapper.javalog;
40
41 import java.io.OutputStream JavaDoc;
42 import java.io.OutputStreamWriter JavaDoc;
43 import java.io.UnsupportedEncodingException JavaDoc;
44 import java.io.Writer JavaDoc;
45 import java.util.logging.ErrorManager JavaDoc;
46 import java.util.logging.LogRecord JavaDoc;
47
48 import org.objectweb.util.monolog.api.BasicLevel;
49 import org.objectweb.util.monolog.wrapper.common.OutputStreamSwitcher;
50
51 /**
52  * The aim of this class is to permit to specify the output for the console
53  * handler of the java.util.logging system.
54  *
55  * This console handler is also able to choose the right ouput (System.err or
56  * System.out) depending on the message level.
57  *
58  * @author S.Chassande-Barrioz
59  * @author ddesjardins - eBMWebsourcing
60  */

61 public class ConsoleHandler extends java.util.logging.Handler JavaDoc {
62
63     protected OutputStreamSwitcher oss;
64
65     private Writer JavaDoc writer;
66
67     public ConsoleHandler() {
68         super();
69     }
70
71     public void activateSwitching() {
72         if (oss == null) {
73             oss = new OutputStreamSwitcher();
74             setOutput(oss);
75         }
76     }
77
78     public void close() throws SecurityException JavaDoc {
79         flush();
80     }
81
82     public void desactivateSwitching(OutputStream JavaDoc newOut) {
83         if (oss != null) {
84             oss = null;
85             setOutput(newOut);
86         }
87     }
88
89     public void flush() {
90         if (writer != null) {
91             try {
92                 writer.flush();
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.FLUSH_FAILURE);
97             }
98         }
99     }
100
101     public void publish(LogRecord JavaDoc record) {
102         if (!isLoggable(record)) {
103             return;
104         }
105         String JavaDoc msg;
106         try {
107             msg = getFormatter().format(record);
108         } catch (Exception JavaDoc ex) {
109             // We don't want to throw an exception here, but we
110
// report the exception to any registered ErrorManager.
111
reportError(null, ex, ErrorManager.FORMAT_FAILURE);
112             return;
113         }
114
115         if (oss != null) {
116             if (record.getLevel().intValue() >= BasicLevel.WARN) {
117                 oss.switchOutput(System.err);
118             } else {
119                 oss.switchOutput(System.out);
120             }
121         }
122         try {
123             writer.write(msg);
124         } catch (Exception JavaDoc ex) {
125             // We don't want to throw an exception here, but we
126
// report the exception to any registered ErrorManager.
127
reportError(null, ex, ErrorManager.WRITE_FAILURE);
128         }
129         flush();
130     }
131
132     /**
133      * Assign the Outputstream by calling a protected method from the super
134      * class.
135      */

136     public void setOutput(OutputStream JavaDoc out) throws SecurityException JavaDoc {
137         if (out == null) {
138             throw new NullPointerException JavaDoc();
139         }
140         flush();
141         String JavaDoc encoding = getEncoding();
142         if (encoding == null) {
143             writer = new OutputStreamWriter JavaDoc(out);
144         } else {
145             try {
146                 writer = new OutputStreamWriter JavaDoc(out, encoding);
147             } catch (UnsupportedEncodingException JavaDoc ex) {
148                 // This shouldn't happen. The setEncoding method
149
// should have validated that the encoding is OK.
150
throw new Error JavaDoc("Unexpected exception " + ex);
151             }
152         }
153     }
154 }
Popular Tags