1 22 package org.jboss.logging.util; 23 24 import java.io.IOException ; 25 import java.io.PrintWriter ; 26 import java.io.Writer ; 27 28 import org.apache.log4j.Logger; 29 import org.apache.log4j.Level; 30 31 43 public class LoggerWriter 44 extends PrintWriter 45 { 46 private Logger logger; 47 private Level level; 48 private boolean inWrite; 49 private boolean issuedWarning; 50 51 56 public LoggerWriter( final Logger logger ) { 57 this( logger, Level.INFO ); 58 } 59 60 67 public LoggerWriter( final Logger logger, 68 final Level level ) { 69 super( new InternalLoggerWriter( logger, level ), true ); 70 } 71 72 75 static class InternalLoggerWriter extends Writer { 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 this.logger = logger; 84 this.level = level; 85 } 86 87 public void write( char[] cbuf, int off, int len ) 88 throws IOException { 89 if ( closed ) { 90 throw new IOException ( "Called write on closed Writer" ); 91 } 92 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 { 104 if ( closed ) { 105 throw new IOException ( "Called flush on closed Writer" ); 106 } 107 } 108 109 public void close() { 110 closed = true; 111 } 112 } 113 114 } 115 | Popular Tags |