1 17 18 package org.apache.avalon.logging.logkit; 19 20 import org.apache.avalon.framework.logger.Logger; 21 22 import org.apache.avalon.logging.data.CategoryDirective; 23 import org.apache.avalon.logging.data.CategoriesDirective; 24 25 import org.apache.avalon.logging.provider.LoggingRuntimeException; 26 import org.apache.avalon.logging.provider.LoggingManager; 27 import org.apache.avalon.logging.provider.LoggingException; 28 29 import org.apache.avalon.util.i18n.ResourceManager; 30 import org.apache.avalon.util.i18n.Resources; 31 32 import org.apache.log.Hierarchy; 33 import org.apache.log.LogTarget; 34 import org.apache.log.Priority; 35 import org.apache.log.output.io.StreamTarget; 36 import org.apache.log.format.Formatter; 37 38 44 public class DefaultLoggingManager implements LoggingManager 45 { 46 50 private static final Resources REZ = 51 ResourceManager.getPackageResources( DefaultLoggingManager.class ); 52 53 private static final String DEFAULT_PRIORITY = "INFO"; 54 55 public static final String DEFAULT_FORMAT = 56 "[%7.7{priority}] (%{category}): %{message}\\n%{throwable}"; 57 58 public static final Formatter FORMAT = 59 new StandardFormatter( DEFAULT_FORMAT ); 60 61 public static final Formatter CONSOLE = 62 new StandardFormatter( DEFAULT_FORMAT, false ); 63 64 68 71 private final LogTargetManager m_targets; 72 73 76 private final Hierarchy m_hierarchy = new Hierarchy(); 77 78 81 private final boolean m_debug; 82 83 87 90 private Logger m_logger; 91 92 96 105 public DefaultLoggingManager( 106 Logger logger, LogTargetManager targets, CategoriesDirective categories, 107 String channel, boolean debug ) 108 throws Exception 109 { 110 if( null == targets ) 111 { 112 throw new NullPointerException ( "targets" ); 113 } 114 115 m_debug = debug; 116 m_targets = targets; 117 m_logger = logger; 118 119 123 if( !debug ) 124 { 125 String priority = getDefaultPriority( categories ); 126 getHierarchy().setDefaultPriority( 127 Priority.getPriorityForName( priority ) ); 128 } 129 else 130 { 131 getHierarchy().setDefaultPriority( 132 Priority.getPriorityForName( "DEBUG" ) ); 133 } 134 135 LogTarget target = getDefaultLogTarget( categories ); 136 getHierarchy().setDefaultLogTarget( target ); 137 138 addCategories( categories ); 139 140 m_logger = getLoggerForCategory( channel ); 141 m_logger.debug( "logging system established" ); 142 } 143 144 private LogTarget getDefaultLogTarget( CategoriesDirective categories ) 145 throws LoggingException 146 { 147 String id = categories.getTarget(); 148 if( null == id ) 149 { 150 return new StreamTarget( System.out, FORMAT ); 151 } 152 else 153 { 154 LogTarget target = m_targets.getLogTarget( id ); 155 if( null != target ) return target; 156 157 final String error = 158 REZ.getString( 159 "manager.invalid-default-target", id ); 160 throw new LoggingException( error ); 161 } 162 } 163 164 private String getDefaultPriority( CategoriesDirective categories ) 165 { 166 if( null != categories ) 167 { 168 String priority = categories.getPriority(); 169 if( priority != null ) return priority; 170 } 171 return DEFAULT_PRIORITY; 172 } 173 174 175 179 184 public void addCategories( CategoriesDirective descriptor ) 185 { 186 addCategories( "", descriptor ); 187 } 188 189 196 public void addCategories( String root, CategoriesDirective directive ) 197 { 198 final String path = filter( root ); 199 addCategory( path, directive.getPriority(), directive.getTarget() ); 200 CategoryDirective[] categories = directive.getCategories(); 201 for( int i = 0; i < categories.length; i++ ) 202 { 203 CategoryDirective category = categories[i]; 204 final String name = filter( category.getName() ); 205 final String base = getBasePath( path, name ); 206 if( category instanceof CategoriesDirective ) 207 { 208 CategoriesDirective c = (CategoriesDirective) category; 209 addCategories( base, c ); 210 } 211 else 212 { 213 final String priority = category.getPriority(); 214 final String target = category.getTarget(); 215 addCategory( base, priority, target ); 216 } 217 } 218 } 219 220 private String getBasePath( String root, String name ) 221 { 222 if( root.equals( "" ) ) return name; 223 return filter( root + "." + name ); 224 } 225 226 231 public Logger getLoggerForCategory( final String category ) 232 { 233 org.apache.log.Logger log = addCategory( category, null, null ); 234 return new LogKitLogger( log ); 235 } 236 237 241 private org.apache.log.Logger addCategory( 242 String path, String priority, String target ) 243 { 244 return addCategory( path, priority, target, true ); 245 } 246 247 private org.apache.log.Logger addCategory( 248 String path, String priority, String target, boolean notify ) 249 { 250 final String name = filter( path ); 251 final org.apache.log.Logger logger; 252 253 if( null != priority ) 254 { 255 if( !name.equals( "" ) ) 256 { 257 final String message = 258 REZ.getString( 259 "manager.notify.add-category-name-priority", name, priority.toLowerCase() ); 260 debug( message ); 261 } 262 else 263 { 264 final String message = 265 REZ.getString( 266 "manager.notify.add-category-priority", priority.toLowerCase() ); 267 debug( message ); 268 } 269 } 270 else 271 { 272 if( !name.equals( "" ) ) 273 { 274 final String message = 275 REZ.getString( 276 "manager.notify.add-category-name", name ); 277 debug( message ); 278 } 279 else 280 { 281 final String message = 282 REZ.getString( 283 "manager.notify.add-category" ); 284 debug( message ); 285 } 286 } 287 288 try 289 { 290 logger = getHierarchy().getLoggerFor( name ); 291 } 292 catch( Throwable e ) 293 { 294 final String error = 295 REZ.getString( 296 "manager.error.internal", name ); 297 throw new LoggingRuntimeException( error, e ); 298 } 299 300 if( !m_debug && priority != null ) 301 { 302 final Priority priorityValue = Priority.getPriorityForName( priority ); 303 if( !priorityValue.getName().equals( priority ) ) 304 { 305 final String message = 306 REZ.getString( "manager.error.priority", priority, name ); 307 throw new IllegalArgumentException ( message ); 308 } 309 logger.setPriority( priorityValue ); 310 } 311 312 if( target != null ) 313 { 314 if( !target.equals( "default" ) ) 315 { 316 final LogTarget logTarget = 317 (LogTarget) m_targets.getLogTarget( target ); 318 if( logTarget != null ) 319 { 320 logger.setLogTargets( new LogTarget[]{ logTarget } ); 321 } 322 } 323 } 324 325 return logger; 326 } 327 328 private String filter( String name ) 329 { 330 if( name == null ) return ""; 331 String path = name.replace( '/', '.' ); 332 if( path.startsWith( "." ) ) 333 { 334 path = path.substring( 1 ); 335 return filter( path ); 336 } 337 if( path.endsWith( "." ) ) 338 { 339 path = path.substring( 0, path.length() -1 ); 340 return filter( path ); 341 } 342 return path; 343 } 344 345 private Hierarchy getHierarchy() 346 { 347 return m_hierarchy; 348 } 349 350 private void debug( String message ) 351 { 352 if( m_logger != null ) m_logger.debug( message ); 353 } 354 } 355 | Popular Tags |