01: /*
02: * $Id: Log4jLoggerWriter.java,v 1.1 2003/10/26 21:10:17 ajzeneski Exp $
03: *
04: * Copyright (c) 2003 The Open For Business Project - www.ofbiz.org
05: *
06: * Permission is hereby granted, free of charge, to any person obtaining a
07: * copy of this software and associated documentation files (the "Software"),
08: * to deal in the Software without restriction, including without limitation
09: * 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;
27: import java.io.PrintWriter;
28: import java.io.Writer;
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 $Revision: 1.1 $
38: * @since 3.0
39: */
40: public class Log4jLoggerWriter extends PrintWriter {
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 {
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)
63: throws IOException {
64: if (closed) {
65: throw new IOException("Writer is closed");
66: }
67:
68: // Remove the eol
69: while (len > 0
70: && (cbuf[len - 1] == '\n' || cbuf[len - 1] == '\r')) {
71: len--;
72: }
73:
74: // send to log4j
75: if (len > 0) {
76: logger
77: .log(priority, String.copyValueOf(cbuf, off,
78: len));
79: }
80: }
81:
82: public void flush() throws IOException {
83: if (closed) {
84: throw new IOException("Writer is closed");
85: }
86: }
87:
88: public void close() {
89: closed = true;
90: }
91: }
92: }
|