1 17 package org.apache.log.output; 18 19 import javax.servlet.ServletContext ; 20 import org.apache.log.format.Formatter; 21 22 28 public class ServletOutputLogTarget 29 extends AbstractOutputTarget 30 { 31 private ServletContext m_context; 33 34 39 public ServletOutputLogTarget( final ServletContext context, final Formatter formatter ) 40 { 41 super( formatter ); 42 m_context = context; 43 open(); 44 } 45 46 51 public ServletOutputLogTarget( final ServletContext context ) 52 { 53 m_context = context; 54 open(); 55 } 56 57 62 protected void write( final String message ) 63 { 64 final int len = message.length(); 65 final char last = len > 0 ? message.charAt( len - 1 ) : 0; 66 final char prev = len > 1 ? message.charAt( len - 2 ) : 0; 67 final String trimmedMessage; 68 if( prev == '\r' && last == '\n' ) 69 { 70 trimmedMessage = message.substring( 0, len - 2 ); 71 } 72 else if( last == '\n' ) 73 { 74 trimmedMessage = message.substring( 0, len - 1 ); 75 } 76 else 77 { 78 trimmedMessage = message; 79 } 80 81 final ServletContext context = m_context; 82 if( null != context ) 83 { 84 synchronized( context ) 85 { 86 context.log( trimmedMessage ); 87 } 88 } 89 } 90 91 95 public synchronized void close() 96 { 97 super.close(); 98 m_context = null; 99 } 100 } 101 | Popular Tags |