1 17 package org.apache.log.output; 18 19 import org.apache.log.LogEvent; 20 import org.apache.log.LogTarget; 21 import org.apache.log.Priority; 22 import org.apache.log.util.Closeable; 23 24 31 public class PriorityFilteringTarget 32 extends AbstractTarget 33 { 34 private final Priority m_priority; 35 36 private final boolean m_closeWrapped; 37 38 39 private LogTarget m_targets[]; 40 41 45 public PriorityFilteringTarget(Priority priority, 46 boolean closeWrappedTarget) 47 { 48 m_priority = priority; 49 m_closeWrapped = closeWrappedTarget; 50 open(); 51 } 52 53 56 public PriorityFilteringTarget(Priority priority) 57 { 58 this(priority, false ); 59 } 60 61 66 public void addTarget( final LogTarget target ) 67 { 68 if( null == m_targets ) 69 { 70 m_targets = new LogTarget[]{target}; 71 } 72 else 73 { 74 final LogTarget oldTargets[] = m_targets; 75 m_targets = new LogTarget[ oldTargets.length + 1 ]; 76 System.arraycopy( oldTargets, 0, m_targets, 0, oldTargets.length ); 77 m_targets[ m_targets.length - 1 ] = target; 78 } 79 } 80 81 82 85 protected void doProcessEvent(LogEvent event) throws Exception 86 { 87 if ( event != null 88 && m_targets != null 89 && !event.getPriority().isLower(m_priority) ) 90 { 91 for( int i = 0; i < m_targets.length; i++ ) 92 { 93 m_targets[ i ].processEvent( event ); 94 } 95 } 96 } 97 98 101 public void close() 102 { 103 super.close(); 104 105 if( m_closeWrapped && m_targets != null ) 106 { 107 for( int i = 0; i < m_targets.length; i++ ) 108 { 109 if ( m_targets[i] instanceof Closeable ) 110 { 111 ( (Closeable)m_targets[i] ).close(); 112 } 113 } 114 } 115 } 116 117 } 118 | Popular Tags |