1 17 package org.apache.log.output; 18 19 import org.apache.log.LogEvent; 20 import org.apache.log.format.Formatter; 21 22 30 public abstract class AbstractOutputTarget 31 extends AbstractTarget 32 { 33 34 private Formatter m_formatter; 35 36 37 public AbstractOutputTarget() 38 { 39 } 40 41 45 public AbstractOutputTarget( final Formatter formatter ) 46 { 47 m_formatter = formatter; 48 } 49 50 52 protected Formatter getFormatter() 53 { 54 return m_formatter; 55 } 56 57 62 protected void write( final String data ) 63 { 64 } 65 66 70 protected void doProcessEvent( LogEvent event ) 71 { 72 final String data = format( event ); 73 write( data ); 74 } 75 76 80 protected synchronized void open() 81 { 82 if( !isOpen() ) 83 { 84 super.open(); 85 writeHead(); 86 } 87 } 88 89 94 public synchronized void close() 95 { 96 if( isOpen() ) 97 { 98 writeTail(); 99 super.close(); 100 } 101 } 102 103 109 private String format( final LogEvent event ) 110 { 111 if( null != m_formatter ) 112 { 113 return m_formatter.format( event ); 114 } 115 else 116 { 117 return event.toString(); 118 } 119 } 120 121 125 private void writeHead() 126 { 127 if( !isOpen() ) 128 { 129 return; 130 } 131 132 final String head = getHead(); 133 if( null != head ) 134 { 135 write( head ); 136 } 137 } 138 139 143 private void writeTail() 144 { 145 if( !isOpen() ) 146 { 147 return; 148 } 149 150 final String tail = getTail(); 151 if( null != tail ) 152 { 153 write( tail ); 154 } 155 } 156 157 163 private String getHead() 164 { 165 return null; 166 } 167 168 174 private String getTail() 175 { 176 return null; 177 } 178 } 179 | Popular Tags |