1 8 package org.apache.avalon.excalibur.logger.factory; 9 10 import org.apache.avalon.excalibur.logger.LogTargetFactory; 11 12 import org.apache.avalon.framework.configuration.Configuration; 13 import org.apache.avalon.framework.configuration.Configurable; 14 import org.apache.avalon.framework.configuration.ConfigurationException; 15 import org.apache.avalon.framework.context.Contextualizable; 16 import org.apache.avalon.framework.context.Context; 17 import org.apache.avalon.framework.context.ContextException; 18 19 import org.apache.log.LogTarget; 20 import org.apache.log.format.ExtendedPatternFormatter; 21 import org.apache.log.format.Formatter; 22 import org.apache.log.format.PatternFormatter; 23 import org.apache.log.format.RawFormatter; 24 import org.apache.log.output.io.FileTarget; 25 import org.apache.log.output.io.rotate.RotatingFileTarget; 26 import org.apache.log.output.io.rotate.RotateStrategy; 27 import org.apache.log.output.io.rotate.OrRotateStrategy; 28 import org.apache.log.output.io.rotate.RotateStrategyBySize; 29 import org.apache.log.output.io.rotate.RotateStrategyByTime; 30 import org.apache.log.output.io.rotate.FileStrategy; 31 import org.apache.log.output.io.rotate.RevolvingFileStrategy; 32 import org.apache.log.output.io.rotate.UniqueFileStrategy; 33 34 import java.io.File ; 35 import java.io.IOException ; 36 import java.util.StringTokenizer ; 37 38 117 public class FileTargetFactory 118 extends AbstractTargetFactory 119 { 120 121 124 public final LogTarget createTarget( final Configuration configuration ) 125 throws ConfigurationException 126 { 127 final Configuration conf_filename = configuration.getChild( "filename" ); 128 final String filename = getFilename( conf_filename.getValue() ); 129 130 final Configuration conf_rotation = configuration.getChild( "rotation", false ); 131 132 final Configuration conf_format = configuration.getChild( "format" ); 133 134 final Configuration conf_append = configuration.getChild( "append" ); 135 final boolean append = conf_append.getValueAsBoolean( false ); 136 137 final LogTarget logtarget; 138 139 final File file = new File ( filename ); 140 final Formatter formatter = getFormatter( conf_format ); 141 142 try 143 { 144 if( null == conf_rotation ) 145 { 146 logtarget = new FileTarget( file, append, formatter ); 147 } 148 else 149 { 150 final Configuration conf_strategy = conf_rotation.getChildren()[0]; 151 final RotateStrategy rotateStrategy = getRotateStrategy( conf_strategy ); 152 final FileStrategy fileStrategy = getFileStrategy( conf_rotation, file ); 153 154 logtarget = 155 new RotatingFileTarget( formatter, rotateStrategy, fileStrategy ); 156 } 157 } 158 catch( final IOException ioe ) 159 { 160 throw new ConfigurationException( "cannot create FileTarget", ioe ); 161 } 162 163 return logtarget; 164 } 165 166 private final static long SECOND = 1000; 167 private final static long MINUTE = 60*SECOND; 168 private final static long HOUR = 60*MINUTE; 169 private final static long DAY = 24*HOUR; 170 171 private final static long KILOBYTE = 1000; 172 private final static long MEGABYTE = 1000*KILOBYTE; 173 174 private RotateStrategy getRotateStrategy( final Configuration conf ) 175 { 176 final String type = conf.getName(); 177 178 if( "or".equals( type ) ) 179 { 180 final Configuration[] configurations = conf.getChildren(); 181 final int size = configurations.length; 182 183 final RotateStrategy[] strategies = new RotateStrategy[size]; 184 for( int i = 0; i < size; i++ ) 185 { 186 strategies[i] = getRotateStrategy( configurations[i] ); 187 } 188 189 return new OrRotateStrategy( strategies ); 190 } 191 192 if( "size".equals( type ) ) 193 { 194 final String value = conf.getValue( "2m" ); 195 196 final int count = value.length(); 197 final char end = value.charAt( count-1 ); 198 final long no; 199 final long size; 200 201 switch( end ) 202 { 203 case 'm':no = Long.parseLong( value.substring( 0, count-1 ) ); 204 size = no * MEGABYTE; 205 break; 206 case 'k':no = Long.parseLong( value.substring( 0, count-1 ) ); 207 size = no * KILOBYTE; 208 break; 209 default: size = Long.parseLong( value ); 210 } 211 212 return new RotateStrategyBySize( size ); 213 } 214 215 final String value = conf.getValue( "24:00:00" ); 217 218 final StringTokenizer tokenizer = new StringTokenizer ( value, ":" ); 220 final int count = tokenizer.countTokens(); 221 long time = 0; 222 for( int i = count; i > 0; i-- ) 223 { 224 final long no = Long.parseLong( tokenizer.nextToken() ); 225 if( 4 == i ) 226 { 227 time += no * DAY; 228 } 229 if( 3 == i ) 230 { 231 time += no * HOUR; 232 } 233 if( 2 == i ) 234 { 235 time += no * MINUTE; 236 } 237 if( 1 == i ) 238 { 239 time += no * SECOND; 240 } 241 } 242 243 return new RotateStrategyByTime( time ); 244 } 245 246 protected FileStrategy getFileStrategy( final Configuration conf, final File file ) 247 { 248 final String type = conf.getAttribute( "type", "unique" ); 249 250 if( "revolving".equals( type ) ) 251 { 252 final int initialRotation = 253 conf.getAttributeAsInteger( "init", 5 ); 254 final int maxRotation = 255 conf.getAttributeAsInteger( "max", 10 ); 256 257 return new RevolvingFileStrategy( file, initialRotation, maxRotation ); 258 } 259 260 final String pattern = conf.getAttribute( "pattern", null ); 262 final String suffix = conf.getAttribute( "suffix", null ); 263 if( pattern == null) 264 { 265 return new UniqueFileStrategy( file ); 266 } 267 else 268 { 269 if( suffix == null ) 270 { 271 return new UniqueFileStrategy( file, pattern ); 272 } 273 else 274 { 275 return new UniqueFileStrategy( file, pattern, suffix ); 276 } 277 } 278 } 279 280 protected Formatter getFormatter( final Configuration conf ) 281 { 282 Formatter formatter = null; 283 284 if ( null != conf ) 285 { 286 final FormatterFactory formatterFactory = new FormatterFactory(); 287 formatter = formatterFactory.createFormatter( conf ); 288 } 289 290 return formatter; 291 } 292 293 305 protected final String getFilename( String rawFilename ) 306 throws ConfigurationException 307 { 308 StringBuffer sb = new StringBuffer (); 309 int i = 0; 310 int j = -1; 311 while( ( j = rawFilename.indexOf( "${", i ) ) > -1 ) 312 { 313 if( i < j ) 314 { 315 sb.append( rawFilename.substring( i, j ) ); 316 } 317 int k = rawFilename.indexOf( '}', j ); 318 final String ctx_name = rawFilename.substring( j + 2, k ); 319 final Object ctx; 320 try 321 { 322 ctx = m_context.get( ctx_name ); 323 } 324 catch( final ContextException ce ) 325 { 326 throw new ConfigurationException( "missing entry '" + ctx_name + "' in Context" ); 327 } 328 sb.append( ctx.toString() ); 329 i = k + 1; 330 } 331 if( i < rawFilename.length() ) 332 { 333 sb.append( rawFilename.substring( i, rawFilename.length() ) ); 334 } 335 return sb.toString(); 336 } 337 } 338 339 | Popular Tags |