1 17 package org.apache.log; 18 19 import org.apache.log.format.PatternFormatter; 20 import org.apache.log.output.io.StreamTarget; 21 import org.apache.log.util.DefaultErrorHandler; 22 import org.apache.log.util.LoggerListener; 23 24 31 public class Hierarchy 32 { 33 public static final String DEFAULT_FORMAT = 35 "%7.7{priority} %23.23{time:yyyy-MM-dd' 'HH:mm:ss.SSS} " + 36 "[%8.8{category}] (%{context}): %{message}\n%{throwable}"; 37 38 private static final Hierarchy c_hierarchy = new Hierarchy(); 40 41 private ErrorHandler m_errorHandler; 43 44 private Logger m_rootLogger; 46 47 private LoggerListener m_loggerListener; 49 50 61 public static Hierarchy getDefaultHierarchy() 62 { 63 return c_hierarchy; 64 } 65 66 70 public Hierarchy() 71 { 72 m_errorHandler = new DefaultErrorHandler(); 73 m_rootLogger = new Logger( new InnerErrorHandler(), 74 new InnerLoggerListener(), 75 "", null, null ); 76 77 final PatternFormatter formatter = new PatternFormatter( DEFAULT_FORMAT ); 79 final StreamTarget target = new StreamTarget( System.out, formatter ); 80 81 setDefaultLogTarget( target ); 82 } 83 84 90 public void setDefaultLogTarget( final LogTarget target ) 91 { 92 if( null == target ) 93 { 94 throw new IllegalArgumentException ( "Can not set DefaultLogTarget to null" ); 95 } 96 97 final LogTarget[] targets = new LogTarget[]{target}; 98 getRootLogger().setLogTargets( targets ); 99 } 100 101 107 public void setDefaultLogTargets( final LogTarget[] targets ) 108 { 109 if( null == targets || 0 == targets.length ) 110 { 111 throw new IllegalArgumentException ( "Can not set DefaultLogTargets to null" ); 112 } 113 114 for( int i = 0; i < targets.length; i++ ) 115 { 116 if( null == targets[ i ] ) 117 { 118 final String message = "Can not set DefaultLogTarget element to null"; 119 throw new IllegalArgumentException ( message ); 120 } 121 } 122 123 getRootLogger().setLogTargets( targets ); 124 } 125 126 132 public void setDefaultPriority( final Priority priority ) 133 { 134 if( null == priority ) 135 { 136 final String message = "Can not set default Hierarchy Priority to null"; 137 throw new IllegalArgumentException ( message ); 138 } 139 140 getRootLogger().setPriority( priority ); 141 } 142 143 148 public void setErrorHandler( final ErrorHandler errorHandler ) 149 { 150 if( null == errorHandler ) 151 { 152 final String message = "Can not set default Hierarchy ErrorHandler to null"; 153 throw new IllegalArgumentException ( message ); 154 } 155 156 m_errorHandler = errorHandler; 157 } 158 159 168 public synchronized void addLoggerListener( final LoggerListener loggerListener ) 169 { 170 if( null == loggerListener ) 171 { 172 throw new NullPointerException ( "loggerListener" ); 173 } 174 175 if( null == m_loggerListener ) 176 { 177 m_loggerListener = loggerListener; 178 } 179 else 180 { 181 final String message = "LoggerListener already set on a unicast event notifier"; 182 throw new UnsupportedOperationException ( message ); 183 } 184 } 185 186 192 public synchronized void removeLoggerListener( final LoggerListener loggerListener ) 193 { 194 if( null == loggerListener ) 195 { 196 throw new NullPointerException ( "loggerListener" ); 197 } 198 199 if( null != m_loggerListener && m_loggerListener == loggerListener ) ; 200 { 201 m_loggerListener = null; 202 } 203 } 204 205 211 public Logger getLoggerFor( final String category ) 212 { 213 return getRootLogger().getChildLogger( category ); 214 } 215 216 222 private synchronized void notifyLoggerCreated( final String category, 223 final Logger logger ) 224 { 225 if( null != m_loggerListener ) 226 { 227 m_loggerListener.loggerCreated( category, logger ); 228 } 229 } 230 231 236 private class InnerLoggerListener 237 extends LoggerListener 238 { 239 245 public void loggerCreated( final String category, 246 final Logger logger ) 247 { 248 notifyLoggerCreated( category, logger ); 249 } 250 } 251 252 private class InnerErrorHandler 253 implements ErrorHandler 254 { 255 262 public void error( final String message, 263 final Throwable throwable, 264 final LogEvent event ) 265 { 266 m_errorHandler.error( message, throwable, event ); 267 } 268 } 269 270 278 public final Logger getRootLogger() 279 { 280 return m_rootLogger; 281 } 282 } 283 | Popular Tags |