KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > joseki > logging > java > StreamHandlerFlush


1 /*
2  * (c) Copyright 2003, 2004 Hewlett-Packard Development Company, LP
3  * [See end of file]
4  */

5
6
7 package org.joseki.logging.java;
8
9 import java.io.* ;
10 import java.util.logging.*;
11
12 /** A stream handler for java logging that flushes after every write.
13  *
14  * @author Andy Seaborne
15  * @version $Id: StreamHandlerFlush.java,v 1.2 2004/04/30 14:13:14 andy_seaborne Exp $
16  */

17
18 public class StreamHandlerFlush extends StreamHandler
19 {
20     OutputStream output = null ;
21     
22     public StreamHandlerFlush()
23     {
24         super() ;
25         configure2() ;
26         super.setOutputStream(System.out);
27         output = System.out ;
28     }
29      
30     public StreamHandlerFlush(OutputStream out, Formatter formatter)
31     {
32         super(out, formatter) ;
33         output = out ;
34         configure2() ;
35     }
36
37     // Unfortunately, much of class StreamHandler internals are not
38
// accessible. This code "fixes" it so that properties based on
39
// this class's name will be in effect.
40
// This method is derived from StreamHandler.configure
41

42     private void configure2()
43     {
44         String JavaDoc cname = StreamHandlerFlush.class.getName();
45
46         LogManager manager = LogManager.getLogManager();
47
48         // Level
49
String JavaDoc tmp = manager.getProperty(cname + ".level");
50         if (tmp == null)
51             tmp = "INFO";
52         setLevel(Level.parse(tmp));
53
54         // Filter
55
tmp = manager.getProperty(cname + ".filter");
56         try
57         {
58             if (tmp != null)
59             {
60                 Class JavaDoc clz = ClassLoader.getSystemClassLoader().loadClass(tmp);
61                 setFilter((Filter) clz.newInstance());
62             }
63         }
64         catch (Exception JavaDoc ex)
65         {
66             // We got one of a variety of exceptions in creating the
67
// class or creating an instance.
68
// Drop through.
69
}
70
71         // Formatter
72
tmp = manager.getProperty(cname + ".formatter");
73         if (tmp == null)
74             tmp = OneLineFormatter.class.getName();
75         try
76         {
77             Class JavaDoc clz = ClassLoader.getSystemClassLoader().loadClass(tmp);
78             setFormatter((Formatter) clz.newInstance());
79         }
80         catch (Exception JavaDoc ex)
81         {
82             // We got one of a variety of exceptions in creating the
83
// class or creating an instance.
84
// Drop through.
85
}
86
87         /// Encoding
88
try
89         {
90             setEncoding(manager.getProperty(cname + ".encoding"));
91         }
92         catch (Exception JavaDoc ex)
93         {
94             try
95             {
96                 setEncoding(null);
97             }
98             catch (Exception JavaDoc ex2)
99             {
100                 // doing a setEncoding with null should always work.
101
// assert false;
102
}
103         }
104     }
105     public void publish(LogRecord record)
106     {
107         super.publish(record) ;
108         super.flush() ;
109         //try { output.flush() ; } catch (IOException ioEx) {}
110
}
111     
112 }
113
114
115 /*
116  * (c) Copyright 2003, 2004 Hewlett-Packard Development Company, LP
117  * All rights reserved.
118  *
119  * Redistribution and use in source and binary forms, with or without
120  * modification, are permitted provided that the following conditions
121  * are met:
122  * 1. Redistributions of source code must retain the above copyright
123  * notice, this list of conditions and the following disclaimer.
124  * 2. Redistributions in binary form must reproduce the above copyright
125  * notice, this list of conditions and the following disclaimer in the
126  * documentation and/or other materials provided with the distribution.
127  * 3. The name of the author may not be used to endorse or promote products
128  * derived from this software without specific prior written permission.
129  *
130  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
131  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
132  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
133  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
134  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
135  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
136  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
137  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
138  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
139  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
140  */

141
Popular Tags