1 16 package org.apache.log4j.jdbc; 17 18 import org.apache.log4j.*; 19 import org.apache.log4j.spi.*; 20 import org.apache.log4j.PatternLayout; 21 22 import java.util.ArrayList ; 23 import java.util.Iterator ; 24 25 import java.sql.DriverManager ; 26 import java.sql.Connection ; 27 import java.sql.Statement ; 28 import java.sql.SQLException ; 29 30 31 79 public class JDBCAppender extends org.apache.log4j.AppenderSkeleton 80 implements org.apache.log4j.Appender { 81 82 85 protected String databaseURL = "jdbc:odbc:myDB"; 86 87 90 protected String databaseUser = "me"; 91 92 95 protected String databasePassword = "mypassword"; 96 97 104 protected Connection connection = null; 105 106 115 protected String sqlStatement = ""; 116 117 121 protected int bufferSize = 1; 122 123 126 protected ArrayList buffer; 127 128 131 protected ArrayList removes; 132 133 public JDBCAppender() { 134 super(); 135 buffer = new ArrayList (bufferSize); 136 removes = new ArrayList (bufferSize); 137 } 138 139 142 public void append(LoggingEvent event) { 143 buffer.add(event); 144 145 if (buffer.size() >= bufferSize) 146 flushBuffer(); 147 } 148 149 157 protected String getLogStatement(LoggingEvent event) { 158 return getLayout().format(event); 159 } 160 161 169 protected void execute(String sql) throws SQLException { 170 171 Connection con = null; 172 Statement stmt = null; 173 174 try { 175 con = getConnection(); 176 177 stmt = con.createStatement(); 178 stmt.executeUpdate(sql); 179 } catch (SQLException e) { 180 if (stmt != null) 181 stmt.close(); 182 throw e; 183 } 184 stmt.close(); 185 closeConnection(con); 186 187 } 189 190 191 198 protected void closeConnection(Connection con) { 199 } 200 201 207 protected Connection getConnection() throws SQLException { 208 if (!DriverManager.getDrivers().hasMoreElements()) 209 setDriver("sun.jdbc.odbc.JdbcOdbcDriver"); 210 211 if (connection == null) { 212 connection = DriverManager.getConnection(databaseURL, databaseUser, 213 databasePassword); 214 } 215 216 return connection; 217 } 218 219 223 public void close() 224 { 225 flushBuffer(); 226 227 try { 228 if (connection != null && !connection.isClosed()) 229 connection.close(); 230 } catch (SQLException e) { 231 errorHandler.error("Error closing connection", e, ErrorCode.GENERIC_FAILURE); 232 } 233 this.closed = true; 234 } 235 236 243 public void flushBuffer() { 244 removes.ensureCapacity(buffer.size()); 246 for (Iterator i = buffer.iterator(); i.hasNext();) { 247 try { 248 LoggingEvent logEvent = (LoggingEvent)i.next(); 249 String sql = getLogStatement(logEvent); 250 execute(sql); 251 removes.add(logEvent); 252 } 253 catch (SQLException e) { 254 errorHandler.error("Failed to excute sql", e, 255 ErrorCode.FLUSH_FAILURE); 256 } 257 } 258 259 buffer.removeAll(removes); 261 262 removes.clear(); 264 } 265 266 267 268 public void finalize() { 269 close(); 270 } 271 272 273 276 public boolean requiresLayout() { 277 return true; 278 } 279 280 281 284 public void setSql(String s) { 285 sqlStatement = s; 286 if (getLayout() == null) { 287 this.setLayout(new PatternLayout(s)); 288 } 289 else { 290 ((PatternLayout)getLayout()).setConversionPattern(s); 291 } 292 } 293 294 295 298 public String getSql() { 299 return sqlStatement; 300 } 301 302 303 public void setUser(String user) { 304 databaseUser = user; 305 } 306 307 308 public void setURL(String url) { 309 databaseURL = url; 310 } 311 312 313 public void setPassword(String password) { 314 databasePassword = password; 315 } 316 317 318 public void setBufferSize(int newBufferSize) { 319 bufferSize = newBufferSize; 320 buffer.ensureCapacity(bufferSize); 321 removes.ensureCapacity(bufferSize); 322 } 323 324 325 public String getUser() { 326 return databaseUser; 327 } 328 329 330 public String getURL() { 331 return databaseURL; 332 } 333 334 335 public String getPassword() { 336 return databasePassword; 337 } 338 339 340 public int getBufferSize() { 341 return bufferSize; 342 } 343 344 345 349 public void setDriver(String driverClass) { 350 try { 351 Class.forName(driverClass); 352 } catch (Exception e) { 353 errorHandler.error("Failed to load driver", e, 354 ErrorCode.GENERIC_FAILURE); 355 } 356 } 357 } 358 359 | Popular Tags |