1 17 18 package org.apache.avalon.logging.logkit.factory; 19 20 import java.io.File ; 21 import java.io.IOException ; 22 import java.util.StringTokenizer ; 23 24 import org.apache.avalon.framework.configuration.Configuration; 25 import org.apache.avalon.framework.configuration.ConfigurationException; 26 import org.apache.avalon.framework.context.ContextException; 27 28 import org.apache.avalon.logging.logkit.DefaultLoggingManager; 29 import org.apache.avalon.logging.logkit.FormatterFactory; 30 import org.apache.avalon.logging.logkit.LogTargetFactory; 31 import org.apache.avalon.logging.logkit.LogTargetException; 32 33 import org.apache.avalon.util.i18n.ResourceManager; 34 import org.apache.avalon.util.i18n.Resources; 35 36 import org.apache.log.LogTarget; 37 import org.apache.log.format.Formatter; 38 import org.apache.log.output.io.FileTarget; 39 import org.apache.log.output.io.rotate.FileStrategy; 40 import org.apache.log.output.io.rotate.OrRotateStrategy; 41 import org.apache.log.output.io.rotate.RevolvingFileStrategy; 42 import org.apache.log.output.io.rotate.RotateStrategy; 43 import org.apache.log.output.io.rotate.RotateStrategyByDate; 44 import org.apache.log.output.io.rotate.RotateStrategyBySize; 45 import org.apache.log.output.io.rotate.RotateStrategyByTime; 46 import org.apache.log.output.io.rotate.RotateStrategyByTimeOfDay; 47 import org.apache.log.output.io.rotate.RotatingFileTarget; 48 import org.apache.log.output.io.rotate.UniqueFileStrategy; 49 50 51 52 143 public class FileTargetFactory implements LogTargetFactory 144 { 145 149 private static final Resources REZ = 150 ResourceManager.getPackageResources( FileTargetFactory.class ); 151 152 private static final long SECOND = 1000; 153 private static final long MINUTE = 60 * SECOND; 154 private static final long HOUR = 60 * MINUTE; 155 private static final long DAY = 24 * HOUR; 156 157 private static final long KILOBYTE = 1000; 158 private static final long MEGABYTE = 1000 * KILOBYTE; 159 160 164 private final File m_basedir; 165 private final FormatterFactory m_formatter; 166 167 171 public FileTargetFactory( File basedir, FormatterFactory formatter ) 172 { 173 m_basedir = basedir; 174 m_formatter = formatter; 175 } 176 177 181 184 public final LogTarget createTarget( final Configuration configuration ) 185 throws LogTargetException 186 { 187 try 188 { 189 final Configuration confFilename = 190 configuration.getChild( "filename" ); 191 final String filename = 192 confFilename.getValue(); 193 194 final Configuration confRotation = 195 configuration.getChild( "rotation", false ); 196 197 final Configuration confFormat = 198 configuration.getChild( "format", false ); 199 200 final Configuration confAppend = 201 configuration.getChild( "append" ); 202 final boolean append = 203 confAppend.getValueAsBoolean( false ); 204 205 final LogTarget logtarget; 206 207 final File file = resolveFile( filename ); 208 209 final Formatter formatter = 210 m_formatter.createFormatter( confFormat ); 211 212 if( null == confRotation ) 213 { 214 return new FileTarget( file, append, formatter ); 215 } 216 else 217 { 218 if( confRotation.getChildren().length == 0 ) 219 { 220 final String error = 221 REZ.getString( "file.error.missing-rotation" ); 222 throw new LogTargetException( error ); 223 } 224 final Configuration confStrategy = 225 confRotation.getChildren()[ 0 ]; 226 final RotateStrategy rotateStrategy = 227 getRotateStrategy( confStrategy ); 228 final FileStrategy fileStrategy = 229 getFileStrategy( confRotation, file ); 230 231 try 232 { 233 return new RotatingFileTarget( 234 append, formatter, rotateStrategy, fileStrategy ); 235 } 236 catch( Throwable e ) 237 { 238 final String error = 239 REZ.getString( "file.error.logkit-rotation" ); 240 throw new LogTargetException( error, e ); 241 } 242 } 243 } 244 catch( final IOException e ) 245 { 246 final String error = 247 REZ.getString( "file.error.io" ); 248 throw new LogTargetException( error, e ); 249 } 250 catch( ConfigurationException e ) 251 { 252 final String error = 253 REZ.getString( "file.error.config" ); 254 throw new LogTargetException( error, e ); 255 } 256 catch( Throwable e ) 257 { 258 final String error = 259 REZ.getString( "file.error.internal" ); 260 throw new LogTargetException( error, e ); 261 } 262 } 263 264 private File resolveFile( final String filename ) 265 { 266 final File file = new File ( filename ); 267 if( file.isAbsolute() ) return file; 268 return new File ( m_basedir, filename ); 269 } 270 271 private RotateStrategy getRotateStrategy( final Configuration conf ) 272 { 273 final String type = conf.getName(); 274 275 if( "or".equals( type ) ) 276 { 277 final Configuration[] configurations = conf.getChildren(); 278 final int size = configurations.length; 279 280 final RotateStrategy[] strategies = new RotateStrategy[ size ]; 281 for( int i = 0; i < size; i++ ) 282 { 283 strategies[ i ] = getRotateStrategy( configurations[ i ] ); 284 } 285 286 return new OrRotateStrategy( strategies ); 287 } 288 else if( "size".equals( type ) ) 289 { 290 final String value = conf.getValue( "2m" ); 291 292 final int count = value.length(); 293 final char end = value.charAt( count - 1 ); 294 final long no; 295 final long size; 296 297 switch( end ) 298 { 299 case 'm': 300 no = Long.parseLong( value.substring( 0, count - 1 ) ); 301 size = no * MEGABYTE; 302 break; 303 case 'k': 304 no = Long.parseLong( value.substring( 0, count - 1 ) ); 305 size = no * KILOBYTE; 306 break; 307 default: 308 size = Long.parseLong( value ); 309 } 310 311 return new RotateStrategyBySize( size ); 312 } 313 else if( "date".equals( type ) ) 314 { 315 final String value = conf.getValue( "yyyyMMdd" ); 316 return new RotateStrategyByDate( value ); 317 } 318 else if( "interval".equals( type ) ) 319 { 320 final String value = conf.getValue( "24:00:00" ); 322 323 final StringTokenizer tokenizer = new StringTokenizer ( value, ":" ); 325 final int count = tokenizer.countTokens(); 326 long time = 0; 327 for( int i = count; i > 0; i-- ) 328 { 329 final long no = Long.parseLong( tokenizer.nextToken() ); 330 if( 4 == i ) 331 { 332 time += no * DAY; 333 } 334 if( 3 == i ) 335 { 336 time += no * HOUR; 337 } 338 if( 2 == i ) 339 { 340 time += no * MINUTE; 341 } 342 if( 1 == i ) 343 { 344 time += no * SECOND; 345 } 346 } 347 348 return new RotateStrategyByTime( time ); 349 } 350 else { 352 final String value = conf.getValue( "24:00:00" ); 354 355 final StringTokenizer tokenizer = new StringTokenizer ( value, ":" ); 357 final int count = tokenizer.countTokens(); 358 long time = 0; 359 for( int i = count; i > 0; i-- ) 360 { 361 final long no = Long.parseLong( tokenizer.nextToken() ); 362 if( 3 == i ) 363 { 364 time += no * HOUR; 365 } 366 if( 2 == i ) 367 { 368 time += no * MINUTE; 369 } 370 if( 1 == i ) 371 { 372 time += no * SECOND; 373 } 374 } 375 376 return new RotateStrategyByTimeOfDay( time ); 377 } 378 } 379 380 protected FileStrategy getFileStrategy( final Configuration conf, final File file ) 381 { 382 final String type = conf.getAttribute( "type", "unique" ); 383 384 if( "revolving".equals( type ) ) 385 { 386 final int initialRotation = 387 conf.getAttributeAsInteger( "init", 5 ); 388 final int maxRotation = 389 conf.getAttributeAsInteger( "max", 10 ); 390 391 return new RevolvingFileStrategy( file, initialRotation, maxRotation ); 392 } 393 394 final String pattern = conf.getAttribute( "pattern", null ); 396 final String suffix = conf.getAttribute( "suffix", null ); 397 if( pattern == null ) 398 { 399 return new UniqueFileStrategy( file ); 400 } 401 else 402 { 403 if( suffix == null ) 404 { 405 return new UniqueFileStrategy( file, pattern ); 406 } 407 else 408 { 409 return new UniqueFileStrategy( file, pattern, suffix ); 410 } 411 } 412 } 413 } 414 415 | Popular Tags |