1 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 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) throws IOException { 63 if (closed) { 64 throw new IOException ("Writer is closed"); 65 } 66 67 while (len > 0 && (cbuf[len - 1] == '\n' || cbuf[len - 1] == '\r')) { 69 len--; 70 } 71 72 if (len > 0) { 74 logger.log(priority, String.copyValueOf(cbuf, off, len)); 75 } 76 } 77 78 public void flush() throws IOException { 79 if (closed) { 80 throw new IOException ("Writer is closed"); 81 } 82 } 83 84 public void close() { 85 closed = true; 86 } 87 } 88 } 89 90 | Popular Tags |