KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > logging > util > LoggerStream


1 /*
2   * JBoss, Home of Professional Open Source
3   * Copyright 2005, JBoss Inc., and individual contributors as indicated
4   * by the @authors tag. See the copyright.txt in the distribution for a
5   * full listing of individual contributors.
6   *
7   * This is free software; you can redistribute it and/or modify it
8   * under the terms of the GNU Lesser General Public License as
9   * published by the Free Software Foundation; either version 2.1 of
10   * the License, or (at your option) any later version.
11   *
12   * This software is distributed in the hope that it will be useful,
13   * but WITHOUT ANY WARRANTY; without even the implied warranty of
14   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15   * Lesser General Public License for more details.
16   *
17   * You should have received a copy of the GNU Lesser General Public
18   * License along with this software; if not, write to the Free
19   * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20   * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21   */

22 package org.jboss.logging.util;
23
24 import java.io.IOException JavaDoc;
25 import java.io.PrintStream JavaDoc;
26
27 import org.apache.log4j.Logger;
28 import org.apache.log4j.Level;
29
30
31 /**
32  * A subclass of PrintStream that redirects its output to a log4j Logger.
33  *
34  * <p>This class is used to map PrintStream/PrintWriter oriented logging onto
35  * the log4j Categories. Examples include capturing System.out/System.err
36  *
37  * @version <tt>$Revision: 1958 $</tt>
38  * @author <a HREF="mailto:Scott.Stark@jboss.org">Scott Stark</a>.
39  * @author <a HREF="mailto:jason@planet57.com">Jason Dillon</a>
40  */

41 public class LoggerStream
42     extends PrintStream JavaDoc
43 {
44    /**
45     * Default flag to enable/disable tracing println calls.
46     * from the system property <tt>org.jboss.logging.util.LoggerStream.trace</tt>
47     * or if not set defaults to <tt>false</tt>.
48     */

49    public static final boolean TRACE =
50       getBoolean(LoggerStream.class.getName() + ".trace", false);
51
52    /** Helper to get boolean value from system property or use default if not set. */
53    private static boolean getBoolean(String JavaDoc name, boolean defaultValue)
54    {
55       String JavaDoc value = System.getProperty(name, null);
56       if (value == null)
57          return defaultValue;
58       return new Boolean JavaDoc(value).booleanValue();
59    }
60    
61    private Logger logger;
62    private Level level;
63    private boolean inWrite;
64    private boolean issuedWarning;
65    
66    /**
67     * Redirect logging to the indicated logger using Level.INFO
68     */

69    public LoggerStream(final Logger logger)
70    {
71       this(logger, Level.INFO, System.out);
72    }
73     
74    /**
75     * Redirect logging to the indicated logger using the given
76     * level. The ps is simply passed to super but is not used.
77     */

78    public LoggerStream(final Logger logger,
79                        final Level level,
80                        final PrintStream JavaDoc ps)
81    {
82       super(ps);
83       this.logger = logger;
84       this.level = level;
85    }
86     
87    public void println(String JavaDoc msg)
88    {
89       if( msg == null )
90          msg = "null";
91       byte[] bytes = msg.getBytes();
92       write(bytes, 0, bytes.length);
93    }
94     
95    public void println(Object JavaDoc msg)
96    {
97       if( msg == null )
98          msg = "null";
99       byte[] bytes = msg.toString().getBytes();
100       write(bytes, 0, bytes.length);
101    }
102     
103    public void write(byte b)
104    {
105       byte[] bytes = {b};
106       write(bytes, 0, 1);
107    }
108     
109    private ThreadLocal JavaDoc recursiveCheck = new ThreadLocal JavaDoc();
110    public void write(byte[] b, int off, int len)
111    {
112       Boolean JavaDoc recursed = (Boolean JavaDoc)recursiveCheck.get();
113       if (recursed != null && recursed.equals(Boolean.TRUE))
114       {
115          /* There is a configuration error that is causing looping. Most
116             likely there are two console appenders so just return to prevent
117             spinning.
118          */

119          if( issuedWarning == false )
120          {
121             String JavaDoc msg = "ERROR: invalid console appender config detected, console stream is looping";
122             try
123             {
124                out.write(msg.getBytes());
125             }
126             catch(IOException JavaDoc ignore)
127             {
128             }
129             issuedWarning = true;
130          }
131          return;
132       }
133
134       // Remove the end of line chars
135
while( len > 0 && (b[len-1] == '\n' || b[len-1] == '\r') && len > off )
136          len --;
137
138       // HACK, something is logging exceptions line by line (including
139
// blanks), but I can't seem to find it, so for now just ignore
140
// empty lines... they aren't very useful.
141
if (len != 0)
142       {
143          String JavaDoc msg = new String JavaDoc(b, off, len);
144          recursiveCheck.set(Boolean.TRUE);
145          if (TRACE)
146          {
147             logger.log(level, msg, new Throwable JavaDoc());
148          }
149          else
150          {
151             logger.log(level, msg);
152          }
153          recursiveCheck.set(Boolean.FALSE);
154       }
155    }
156 }
157
Popular Tags