1 package org.apache.velocity.runtime.log; 2 3 18 19 import java.io.File ; 20 21 import org.apache.log.Priority; 22 import org.apache.log.Logger; 23 import org.apache.log.Hierarchy; 24 import org.apache.log.LogTarget; 25 import org.apache.log.output.io.FileTarget; 26 27 import org.apache.velocity.runtime.RuntimeServices; 28 import org.apache.velocity.runtime.RuntimeConstants; 29 30 37 public class AvalonLogSystem implements LogSystem 38 { 39 private Logger logger = null; 40 41 private RuntimeServices rsvc = null; 42 43 47 48 public AvalonLogSystem() 49 { 50 } 51 52 public void init( RuntimeServices rs ) 53 throws Exception 54 { 55 this.rsvc = rs; 56 57 61 String loggerName = (String ) rsvc.getProperty("runtime.log.logsystem.avalon.logger"); 62 63 if (loggerName != null) 64 { 65 this.logger = Hierarchy.getDefaultHierarchy().getLoggerFor(loggerName); 66 } 67 else 68 { 69 73 String logfile = (String ) rsvc.getProperty( RuntimeConstants.RUNTIME_LOG ); 74 75 78 try 79 { 80 init( logfile ); 81 82 logVelocityMessage( 0, 83 "AvalonLogSystem initialized using logfile '" + logfile + "'" ); 84 } 85 catch( Exception e ) 86 { 87 System.out.println( 88 "PANIC : Error configuring AvalonLogSystem : " + e ); 89 System.err.println( 90 "PANIC : Error configuring AvalonLogSystem : " + e ); 91 92 throw new Exception ("Unable to configure AvalonLogSystem : " + e ); 93 } 94 } 95 } 96 97 102 public void init(String logFile) 103 throws Exception 104 { 105 106 110 FileTarget target = new FileTarget( new File ( logFile), 111 false, 112 new VelocityFormatter("%{time} %{message}\\n%{throwable}" ) ); 113 114 117 118 logger = Hierarchy.getDefaultHierarchy().getLoggerFor( rsvc.toString() ); 119 logger.setPriority( Priority.DEBUG ); 120 logger.setLogTargets( new LogTarget[] { target } ); 121 } 122 123 129 public void logVelocityMessage(int level, String message) 130 { 131 135 136 switch (level) 137 { 138 case LogSystem.WARN_ID: 139 logger.warn( RuntimeConstants.WARN_PREFIX + message ); 140 break; 141 case LogSystem.INFO_ID: 142 logger.info( RuntimeConstants.INFO_PREFIX + message); 143 break; 144 case LogSystem.DEBUG_ID: 145 logger.debug( RuntimeConstants.DEBUG_PREFIX + message); 146 break; 147 case LogSystem.ERROR_ID: 148 logger.error(RuntimeConstants.ERROR_PREFIX + message); 149 break; 150 default: 151 logger.info( message); 152 break; 153 } 154 } 155 } 156 | Popular Tags |