KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > log4j > helpers > SingleLineTracerPrintWriter


1 /*
2  * Copyright 1999-2005 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package org.apache.log4j.helpers;
18
19 /**
20    SingleLineTracerPrintWriter overrides the println function in
21    TracerPrintWriter by replacing the TAB character with spaces.
22    It also does not print the "\n".
23    <p>
24    The default format generated by TracerPrintWriter for exceptions
25    prints on multiple lines, which does not interact well with some
26    logging systems. On the other hand, a stack-trace on one line can be a
27    mite difficult to read, so this class should only be used where really
28    necessary :-)
29    <p>
30    For syslog daemons, tabs in messages are not friendly, hence the
31    replacement of tabs by spaces here. It shouldn't do any harm to
32    do this for all messages...
33    <p>
34    Perhaps it might be better to enhance TracerPrintWriter to have
35    a configuration item for one-line or multi-line mode...
36 */

37 public class SingleLineTracerPrintWriter extends TracerPrintWriter {
38
39   static String JavaDoc TAB = " ";
40
41   public SingleLineTracerPrintWriter(QuietWriter qWriter) {
42     super(qWriter);
43   }
44
45   /**
46      Make the first Exception line print properly by omitting the \n at the
47      end.
48   */

49   public
50    void println(Object JavaDoc o) {
51     this.qWriter.write(o.toString());
52   }
53
54   // Note: the Char[] form is handled by the TracerPrinterWriter super
55
// class
56

57   /**
58      Remove the first character from the string (usually a TAB) and do
59      not print "\n"
60   */

61   public
62   void println(String JavaDoc s) {
63       // remove '^I' and replace it with 4 spaces
64
this.qWriter.write(TAB+s.substring(1));
65   }
66 }
67
Popular Tags