1 package org.apache.velocity.runtime.log; 2 3 18 19 import java.util.Enumeration ; 20 21 import org.apache.log4j.*; 22 import org.apache.log4j.net.*; 23 24 import org.apache.velocity.runtime.RuntimeConstants; 25 import org.apache.velocity.runtime.RuntimeServices; 26 27 36 public class Log4JLogSystem implements LogSystem 37 { 38 private RuntimeServices rsvc = null; 39 40 41 protected Category logger = null; 42 43 44 protected Layout layout = null; 45 46 47 private String logfile = ""; 48 49 53 public Log4JLogSystem() 54 { 55 } 56 57 public void init( RuntimeServices rs ) 58 { 59 rsvc = rs; 60 61 65 logfile = rsvc.getString( RuntimeConstants.RUNTIME_LOG ); 66 67 70 try 71 { 72 internalInit(); 73 74 logVelocityMessage( 0, 75 "Log4JLogSystem initialized using logfile " + logfile ); 76 } 77 catch( Exception e ) 78 { 79 System.out.println( 80 "PANIC : error configuring Log4JLogSystem : " + e ); 81 } 82 } 83 84 89 private void internalInit() 90 throws Exception 91 { 92 logger = Category.getInstance(""); 93 logger.setAdditivity(false); 94 95 99 logger.setPriority(Priority.DEBUG); 100 101 String pattern = rsvc.getString( RuntimeConstants.LOGSYSTEM_LOG4J_PATTERN ); 102 103 if (pattern == null || pattern.length() == 0) 104 { 105 pattern = "%d - %m%n"; 106 } 107 108 layout = new PatternLayout(pattern); 109 110 configureFile(); 111 configureRemote(); 112 configureSyslog(); 113 configureEmail(); 114 } 115 116 119 private void configureFile() 120 throws Exception 121 { 122 int backupFiles = 123 rsvc.getInt(RuntimeConstants.LOGSYSTEM_LOG4J_FILE_BACKUPS, 1); 124 int fileSize = 125 rsvc.getInt(RuntimeConstants.LOGSYSTEM_LOG4J_FILE_SIZE, 100000); 126 127 Appender appender = new RollingFileAppender(layout,logfile,true); 128 129 ((RollingFileAppender)appender).setMaxBackupIndex(backupFiles); 130 131 132 if (fileSize > -1) 133 { 134 ((RollingFileAppender)appender).setMaximumFileSize(fileSize); 135 } 136 logger.addAppender(appender); 137 } 138 139 142 private void configureRemote() 143 throws Exception 144 { 145 String remoteHost = 146 rsvc.getString(RuntimeConstants.LOGSYSTEM_LOG4J_REMOTE_HOST); 147 int remotePort = 148 rsvc.getInt(RuntimeConstants.LOGSYSTEM_LOG4J_REMOTE_PORT, 1099); 149 150 if (remoteHost == null || remoteHost.trim().equals("") || 151 remotePort <= 0) 152 { 153 return; 154 } 155 156 Appender appender=new SocketAppender(remoteHost,remotePort); 157 158 logger.addAppender(appender); 159 } 160 161 164 private void configureSyslog() 165 throws Exception 166 { 167 String syslogHost = 168 rsvc.getString(RuntimeConstants.LOGSYSTEM_LOG4J_SYSLOGD_HOST); 169 String syslogFacility = 170 rsvc.getString(RuntimeConstants.LOGSYSTEM_LOG4J_SYSLOGD_FACILITY); 171 172 if (syslogHost == null || syslogHost.trim().equals("") || 173 syslogFacility == null ) 174 { 175 return; 176 } 177 178 Appender appender = new SyslogAppender(); 179 180 ((SyslogAppender)appender).setLayout(layout); 181 ((SyslogAppender)appender).setSyslogHost(syslogHost); 182 ((SyslogAppender)appender).setFacility(syslogFacility); 183 184 logger.addAppender(appender); 185 } 186 187 190 private void configureEmail() 191 throws Exception 192 { 193 String smtpHost = 194 rsvc.getString(RuntimeConstants.LOGSYSTEM_LOG4J_EMAIL_SERVER); 195 String emailFrom = 196 rsvc.getString(RuntimeConstants.LOGSYSTEM_LOG4J_EMAIL_FROM); 197 String emailTo = 198 rsvc.getString(RuntimeConstants.LOGSYSTEM_LOG4J_EMAIL_TO); 199 String emailSubject = 200 rsvc.getString(RuntimeConstants.LOGSYSTEM_LOG4J_EMAIL_SUBJECT); 201 String bufferSize = 202 rsvc.getString(RuntimeConstants.LOGSYSTEM_LOG4J_EMAIL_BUFFER_SIZE); 203 204 if (smtpHost == null || smtpHost.trim().equals("") 205 || emailFrom == null || smtpHost.trim().equals("") 206 || emailTo == null || emailTo.trim().equals("") 207 || emailSubject == null || emailSubject.trim().equals("") 208 || bufferSize == null || bufferSize.trim().equals("") ) 209 { 210 return; 211 } 212 213 SMTPAppender appender = new SMTPAppender(); 214 215 appender.setSMTPHost( smtpHost ); 216 appender.setFrom( emailFrom ); 217 appender.setTo( emailTo ); 218 appender.setSubject( emailSubject ); 219 220 appender.setBufferSize( Integer.parseInt(bufferSize) ); 221 222 appender.setLayout(layout); 223 appender.activateOptions(); 224 logger.addAppender(appender); 225 } 226 227 233 public void logVelocityMessage(int level, String message) 234 { 235 switch (level) 236 { 237 case LogSystem.WARN_ID: 238 logger.warn( message ); 239 break; 240 case LogSystem.INFO_ID: 241 logger.info(message); 242 break; 243 case LogSystem.DEBUG_ID: 244 logger.debug(message); 245 break; 246 case LogSystem.ERROR_ID: 247 logger.error(message); 248 break; 249 default: 250 logger.debug(message); 251 break; 252 } 253 } 254 255 258 protected void finalize() throws Throwable 259 { 260 shutdown(); 261 } 262 263 264 public void shutdown() 265 { 266 Enumeration appenders = logger.getAllAppenders(); 267 while (appenders.hasMoreElements()) 268 { 269 Appender appender = (Appender)appenders.nextElement(); 270 appender.close(); 271 } 272 } 273 } 274 | Popular Tags |