KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ofbiz > base > util > Log4jLoggerWriter


1 /*
2  * $Id: Log4jLoggerWriter.java 5462 2005-08-05 18:35:48Z jonesde $
3  *
4  * Copyright (c) 2003 The Open For Business Project - www.ofbiz.org
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a
7  * copy of this software and associated documentation files (the "Software"),
8  * to deal in the Software without restriction, including without limitation
9  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10  * and/or sell copies of the Software, and to permit persons to whom the
11  * Software is furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included
14  * in all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
17  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
19  * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
20  * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT
21  * OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR
22  * THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23  */

24 package org.ofbiz.base.util;
25
26 import java.io.IOException JavaDoc;
27 import java.io.PrintWriter JavaDoc;
28 import java.io.Writer JavaDoc;
29
30 import org.apache.log4j.Logger;
31 import org.apache.log4j.Priority;
32
33 /**
34  * Writer implementation for writing to a log4j logger.
35  *
36  * @author <a HREF="mailto:jaz@ofbiz.org">Andy Zeneski</a>
37  * @version $Rev: 5462 $
38  * @since 3.0
39  */

40 public class Log4jLoggerWriter extends PrintWriter JavaDoc {
41
42     public Log4jLoggerWriter(Logger logger) {
43         this(logger, Priority.INFO);
44     }
45
46     public Log4jLoggerWriter(Logger logger, Priority priority) {
47         super(new Log4jPrintWriter(logger, priority), true);
48     }
49
50     static class Log4jPrintWriter extends Writer JavaDoc {
51
52         private Logger logger = null;
53         private Priority priority = null;
54         private boolean closed = false;
55
56         public Log4jPrintWriter(Logger logger, Priority priority) {
57             lock = logger;
58             this.logger = logger;
59             this.priority = priority;
60         }
61
62         public void write(char[] cbuf, int off, int len) throws IOException JavaDoc {
63             if (closed) {
64                 throw new IOException JavaDoc("Writer is closed");
65             }
66
67             // Remove the eol
68
while (len > 0 && (cbuf[len - 1] == '\n' || cbuf[len - 1] == '\r')) {
69                 len--;
70             }
71
72             // send to log4j
73
if (len > 0) {
74                 logger.log(priority, String.copyValueOf(cbuf, off, len));
75             }
76         }
77
78         public void flush() throws IOException JavaDoc {
79             if (closed) {
80                 throw new IOException JavaDoc("Writer is closed");
81             }
82         }
83
84         public void close() {
85             closed = true;
86         }
87     }
88 }
89
90
Popular Tags