KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > portal > common > logging > Log4JWriter


1 /*****************************************
2  * *
3  * JBoss Portal: The OpenSource Portal *
4  * *
5  * Distributable under LGPL license. *
6  * See terms of license at gnu.org. *
7  * *
8  *****************************************/

9 package org.jboss.portal.common.logging;
10
11 import org.apache.log4j.Logger;
12 import org.apache.log4j.Level;
13
14 import java.io.Writer JavaDoc;
15 import java.io.IOException JavaDoc;
16
17 /**
18  * @author <a HREF="mailto:julien@jboss.org">Julien Viet</a>
19  * @version $Revision: 1.1 $
20  */

21 public class Log4JWriter extends Writer JavaDoc
22 {
23
24    /** The open/closed status. */
25    private boolean open;
26
27    /** The logger. */
28    private Logger log;
29
30    /** The logging level. */
31    private Level level;
32
33    /**
34     * @throws IllegalArgumentException if log or level is null
35     */

36    public Log4JWriter(Logger log, Level level) throws IllegalArgumentException JavaDoc
37    {
38       if (log == null)
39       {
40          throw new IllegalArgumentException JavaDoc("No logger provided");
41       }
42       if (level == null)
43       {
44          throw new IllegalArgumentException JavaDoc("No level provided");
45       }
46       this.open = false;
47       this.log = log;
48       this.level = level;
49    }
50
51    public void write(char cbuf[], int off, int len) throws IOException JavaDoc
52    {
53       if (open)
54       {
55          log.log(level, new String JavaDoc(cbuf, off, len));
56       }
57       else
58       {
59          throw new IOException JavaDoc("Stream closed");
60       }
61    }
62
63    public void flush() throws IOException JavaDoc
64    {
65    }
66
67    public void close() throws IOException JavaDoc
68    {
69       open = false;
70    }
71 }
72
Popular Tags