1 package org.apache.velocity.runtime.log; 2 3 18 19 import java.util.Enumeration ; 20 21 import org.apache.log4j.Category; 22 import org.apache.log4j.RollingFileAppender; 23 import org.apache.log4j.PatternLayout; 24 import org.apache.log4j.Priority; 25 import org.apache.log4j.Appender; 26 27 import org.apache.velocity.runtime.RuntimeConstants; 28 import org.apache.velocity.runtime.RuntimeServices; 29 30 39 public class SimpleLog4JLogSystem implements LogSystem 40 { 41 private RuntimeServices rsvc = null; 42 43 44 protected Category logger = null; 45 46 public SimpleLog4JLogSystem() 47 { 48 } 49 50 public void init( RuntimeServices rs ) 51 { 52 rsvc = rs; 53 54 58 59 String categoryname = (String ) rsvc.getProperty("runtime.log.logsystem.log4j.category"); 60 61 if ( categoryname != null ) 62 { 63 logger = Category.getInstance( categoryname ); 64 65 logVelocityMessage( 0, 66 "SimpleLog4JLogSystem using category '" + categoryname + "'"); 67 68 return; 69 } 70 71 74 75 String logfile = rsvc.getString( RuntimeConstants.RUNTIME_LOG ); 76 77 80 try 81 { 82 internalInit( logfile ); 83 84 logVelocityMessage( 0, 85 "SimpleLog4JLogSystem initialized using logfile '" + logfile + "'" ); 86 } 87 catch( Exception e ) 88 { 89 System.out.println( 90 "PANIC : error configuring SimpleLog4JLogSystem : " + e ); 91 } 92 } 93 94 97 private void internalInit( String logfile ) 98 throws Exception 99 { 100 104 105 logger = Category.getInstance(this.getClass().getName()); 106 logger.setAdditivity(false); 107 108 112 logger.setPriority(Priority.DEBUG); 113 114 RollingFileAppender appender = new RollingFileAppender( new PatternLayout( "%d - %m%n"), logfile, true); 115 116 appender.setMaxBackupIndex( 1 ); 117 118 appender.setMaximumFileSize( 100000 ); 119 120 logger.addAppender(appender); 121 } 122 123 129 public void logVelocityMessage(int level, String message) 130 { 131 switch (level) 132 { 133 case LogSystem.WARN_ID: 134 logger.warn( message ); 135 break; 136 case LogSystem.INFO_ID: 137 logger.info(message); 138 break; 139 case LogSystem.DEBUG_ID: 140 logger.debug(message); 141 break; 142 case LogSystem.ERROR_ID: 143 logger.error(message); 144 break; 145 default: 146 logger.debug(message); 147 break; 148 } 149 } 150 151 154 protected void finalize() throws Throwable 155 { 156 shutdown(); 157 } 158 159 160 public void shutdown() 161 { 162 Enumeration appenders = logger.getAllAppenders(); 163 while (appenders.hasMoreElements()) 164 { 165 Appender appender = (Appender)appenders.nextElement(); 166 appender.close(); 167 } 168 } 169 } 170 | Popular Tags |