1 17 package org.apache.log.output.test; 18 19 import java.io.File ; 20 import org.apache.log.Hierarchy; 21 import org.apache.log.LogTarget; 22 import org.apache.log.Logger; 23 import org.apache.log.format.RawFormatter; 24 import org.apache.log.output.io.rotate.FileStrategy; 25 import org.apache.log.output.io.rotate.RevolvingFileStrategy; 26 import org.apache.log.output.io.rotate.RotateStrategy; 27 import org.apache.log.output.io.rotate.RotateStrategyBySize; 28 import org.apache.log.output.io.rotate.RotateStrategyByTime; 29 import org.apache.log.output.io.rotate.RotatingFileTarget; 30 import org.apache.log.output.io.rotate.UniqueFileStrategy; 31 32 36 public class TestRotatingFileOutputLogTarget 37 { 38 private RawFormatter m_formatter = new RawFormatter(); 39 40 42 public void testSizeUnique() 43 throws Exception 44 { 45 final File file = new File ( "test/size-unique.log" ); 46 final FileStrategy fileStrategy = new UniqueFileStrategy( file ); 47 final RotateStrategy rotateStrategy = new RotateStrategyBySize( 128 * 1024 ); 48 final Logger logger = getLogger( fileStrategy, rotateStrategy ); 49 50 doTest( logger ); 51 } 52 53 55 public void testSizeRevoling() 56 throws Exception 57 { 58 final File file = new File ( "test/size-revolve.log" ); 59 final FileStrategy fileStrategy = new RevolvingFileStrategy( file, 20 ); 60 final RotateStrategy rotateStrategy = new RotateStrategyBySize( 128 * 1024 ); 61 final Logger logger = getLogger( fileStrategy, rotateStrategy ); 62 63 doTest( logger ); 64 } 65 66 68 public void testTimeUnique() 69 throws Exception 70 { 71 final File file = new File ( "test/time-unique.log" ); 72 final FileStrategy fileStrategy = new UniqueFileStrategy( file ); 73 final RotateStrategy rotateStrategy = new RotateStrategyByTime( 3 * 1000 ); 74 final Logger logger = getLogger( fileStrategy, rotateStrategy ); 75 76 doTest( logger ); 77 } 78 79 81 public void testTimeRevolving() 82 throws Exception 83 { 84 final File file = new File ( "test/time-revolve.log" ); 85 final FileStrategy fileStrategy = new RevolvingFileStrategy( file, 5 ); 86 final RotateStrategy rotateStrategy = new RotateStrategyByTime( 3 * 1000 ); 87 final Logger logger = getLogger( fileStrategy, rotateStrategy ); 88 89 doTest( logger ); 90 } 91 92 private void doTest( final Logger logger ) 93 { 94 final long startTime = System.currentTimeMillis(); 95 final long diffTime = 10 * 1000; 96 long endTime = startTime; 97 98 int size = 0; 99 for( int i = 0; ( endTime - startTime ) < diffTime; i++ ) 100 { 101 size += generateMessages( logger, i, size, ( endTime - startTime ) ); 102 endTime = System.currentTimeMillis(); 103 } 104 } 105 106 108 private int generateMessages( final Logger logger, 109 final int i, 110 final long totalSize, 111 final long diffTime ) 112 { 113 final String message = 114 "Message " + i + ": total size " + totalSize + " diff time " + diffTime; 115 logger.debug( message ); 116 logger.info( message ); 117 logger.warn( message ); 118 logger.error( message ); 119 logger.fatalError( message ); 120 121 return message.length(); 122 } 123 124 private Logger getLogger( final FileStrategy fileStrategy, 125 final RotateStrategy rotateStrategy ) 126 throws Exception 127 { 128 final RotatingFileTarget target = 129 new RotatingFileTarget( m_formatter, rotateStrategy, fileStrategy ); 130 final Hierarchy hierarchy = new Hierarchy(); 131 final Logger logger = hierarchy.getLoggerFor( "myCat" ); 132 133 logger.setLogTargets( new LogTarget[]{target} ); 134 135 return logger; 136 } 137 138 public static void main( final String args[] ) 139 throws Exception 140 { 141 TestRotatingFileOutputLogTarget trfolt = new TestRotatingFileOutputLogTarget(); 142 trfolt.testSizeUnique(); 143 trfolt.testSizeRevoling(); 144 trfolt.testTimeUnique(); 145 trfolt.testTimeRevolving(); 146 } 147 } 148 | Popular Tags |