1 19 package org.apache.avalon.excalibur.logger; 20 21 import java.util.HashMap ; 22 import java.util.Map ; 23 24 import org.apache.avalon.excalibur.logger.util.LoggerSwitch; 25 import org.apache.avalon.excalibur.logger.util.LoggerUtil; 26 import org.apache.avalon.framework.logger.LogEnabled; 27 import org.apache.avalon.framework.logger.Logger; 28 29 50 public abstract class AbstractLoggerManager 51 implements LogEnabled, LoggerManager 52 { 53 58 final protected Map m_loggers = new HashMap (); 59 60 61 protected String m_prefix; 62 63 69 private LoggerSwitch m_switch; 70 71 72 private Logger m_logger; 73 74 78 private String m_switchTo; 79 80 81 private boolean m_enableLoggingInvoked = false; 82 83 84 private boolean m_startInvoked = false; 85 86 91 final private Logger m_defaultLoggerOverride; 92 93 100 protected Logger getLogger() 101 { 102 return m_logger; 103 } 104 105 116 public AbstractLoggerManager( final String prefix, final String switchTo, 117 Logger defaultLoggerOverride ) 118 { 119 m_prefix = prefix; 120 m_switchTo = switchTo; 121 122 m_switch = new LoggerSwitch( null, null ); 123 m_logger = m_switch.get(); 124 125 m_defaultLoggerOverride = defaultLoggerOverride; 126 } 127 128 131 public void enableLogging( final Logger fallbackLogger ) 132 { 133 if ( m_enableLoggingInvoked ) 134 { 135 throw new IllegalStateException ( "enableLogging() already called" ); 136 } 137 m_switch.setFallback( fallbackLogger ); 138 m_enableLoggingInvoked = true; 139 } 140 141 144 public void start() 145 { 146 if ( m_startInvoked ) 147 { 148 throw new IllegalStateException ( "start() already invoked" ); 149 } 150 151 if ( m_switchTo != null ) 152 { 153 if ( m_logger.isDebugEnabled() ) 154 { 155 final String message = "LoggerManager: switching logging to " + 156 "this.getLoggerForCategory('" + 157 LoggerUtil.getFullCategoryName( m_prefix, m_switchTo) + "')."; 158 m_logger.debug( message ); 159 } 160 161 final Logger ourOwn = this.getLoggerForCategory( m_switchTo ); 162 163 if ( ourOwn == null ) 164 { 165 throw new NullPointerException ( "ourOwn" ); 166 } 167 168 m_switch.setPreferred( ourOwn ); 169 170 if ( m_logger.isDebugEnabled() ) 171 { 172 final String message = "LoggerManager: have switched logging to " + 173 "this.getLoggerForCategory('" + 174 LoggerUtil.getFullCategoryName( m_prefix, m_switchTo) + "')."; 175 m_logger.debug( message ); 176 } 177 } 178 else 179 { 180 if ( m_logger.isDebugEnabled() ) 181 { 182 final String message = "LoggerManager: switchTo is null, " + 183 "no switch of our own logging."; 184 m_logger.debug( message ); 185 } 186 } 187 m_startInvoked = true; 188 } 189 190 191 public void stop(){} 192 193 196 public final Logger getDefaultLogger() 197 { 198 return getLoggerForCategory( null ); 199 } 200 201 207 protected abstract Logger doGetLoggerForCategory( final String fullCategoryName ); 208 209 215 public final Logger getLoggerForCategory( final String categoryName ) 216 { 217 if ( m_defaultLoggerOverride != null && 218 ( categoryName == null || categoryName.length() == 0 ) ) 219 { 220 return m_defaultLoggerOverride; 221 } 222 223 final String fullCategoryName = 224 LoggerUtil.getFullCategoryName( m_prefix, categoryName ); 225 226 final Logger logger; 227 final Logger newLogger; 228 229 synchronized( m_loggers ) 230 { 231 logger = (Logger)m_loggers.get( fullCategoryName ); 232 233 if ( logger == null ) 234 { 235 newLogger = doGetLoggerForCategory( fullCategoryName ); 236 m_loggers.put( fullCategoryName, newLogger ); 237 } 238 else 239 { 240 241 newLogger = null; 242 } 243 } 244 245 if( null != logger ) 246 { 247 if( m_logger.isDebugEnabled() ) 248 { 249 m_logger.debug( "Logger for category " + fullCategoryName + " returned" ); 250 } 251 return logger; 252 } 253 254 if( m_logger.isDebugEnabled() ) 255 { 256 m_logger.debug( "Logger for category " + fullCategoryName + " not defined in " 257 + "configuration. New Logger created and returned" ); 258 } 259 260 return newLogger; 261 } 262 } 263 | Popular Tags |