KickJava   Java API By Example, From Geeks To Geeks.

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


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.PrintWriter JavaDoc;
26 import java.io.Writer JavaDoc;
27
28 import org.apache.log4j.Logger;
29 import org.apache.log4j.Level;
30
31 /**
32  * A subclass of PrintWriter that redirects its output to a log4j Logger. <p>
33  *
34  * This class is used to have something to give api methods that require a
35  * PrintWriter for logging. JBoss-owned classes of this nature generally ignore
36  * the PrintWriter and do their own log4j logging.
37  *
38  * @author <a HREF="mailto:d_jencks@users.sourceforge.net">David Jencks</a>
39  * .
40  * @created August 19, 2001
41  * @version $$
42  */

43 public class LoggerWriter
44    extends PrintWriter JavaDoc
45 {
46    private Logger logger;
47    private Level level;
48    private boolean inWrite;
49    private boolean issuedWarning;
50
51    /**
52     * Redirect logging to the indicated logger using Level.INFO
53     *
54     * @param logger Description of Parameter
55     */

56    public LoggerWriter( final Logger logger ) {
57       this( logger, Level.INFO );
58    }
59
60    /**
61     * Redirect logging to the indicated logger using the given level. The
62     * ps is simply passed to super but is not used.
63     *
64     * @param logger Description of Parameter
65     * @param level Description of Parameter
66     */

67    public LoggerWriter( final Logger logger,
68          final Level level ) {
69       super( new InternalLoggerWriter( logger, level ), true );
70    }
71
72    /**
73     * @created August 19, 2001
74     */

75    static class InternalLoggerWriter extends Writer JavaDoc {
76       private Logger logger;
77       private Level level;
78       private boolean closed;
79
80       public InternalLoggerWriter( final Logger logger, final Level level ) {
81          lock = logger;
82          //synchronize on this logger
83
this.logger = logger;
84          this.level = level;
85       }
86
87       public void write( char[] cbuf, int off, int len )
88          throws IOException JavaDoc {
89          if ( closed ) {
90             throw new IOException JavaDoc( "Called write on closed Writer" );
91          }
92          // Remove the end of line chars
93
while ( len > 0 && ( cbuf[len - 1] == '\n' || cbuf[len - 1] == '\r' ) ) {
94             len--;
95          }
96          if ( len > 0 ) {
97             logger.log( level, String.copyValueOf( cbuf, off, len ) );
98          }
99       }
100
101
102       public void flush()
103          throws IOException JavaDoc {
104          if ( closed ) {
105             throw new IOException JavaDoc( "Called flush on closed Writer" );
106          }
107       }
108
109       public void close() {
110          closed = true;
111       }
112    }
113
114 }
115
Popular Tags