📄 jdbcappender.java
字号:
{ errorHandler.error("JDBCAppender::setOption(), Invalid COLUMN_OPTION LogType : " + arg + " !"); return; } } else if(j == 3) value = arg; } if(!setLogType(name, logtype, value)) return; } } else if(_option.equals(BUFFER_OPTION)) { try { buffer_size = Integer.parseInt(_value); } catch(Exception e) { errorHandler.error("JDBCAppender::setOption(), Invalid BUFFER_OPTION value : " + _value + " !"); return; } } else if(_option.equals(COMMIT_OPTION)) { docommit = _value.equals("Y"); } if(_option.equals(SQL_OPTION) || _option.equals(TABLE_OPTION)) { if(!configured) configure(); } } /** Internal method. Returns true, you may define your own layout... */ public boolean requiresLayout() { return true; } /** Internal method. Close the database connection & flush the buffer. */ public void close() { flush_buffer(); if(connection_class == null) { try{con.close();}catch(Exception e){errorHandler.error("JDBCAppender::close(), " + e);} } this.closed = true; } /** You have to call this function for all provided columns of your log-table ! */ public boolean setLogType(String _name, int _logtype, Object _value) { if(sql != null) return true; if(!configured) { if(!configure()) return false; } try { jlogger.setLogType(_name, _logtype, _value); } catch(Exception e) { errorHandler.error("JDBCAppender::setLogType(), " + e); return false; } return true; } /** Internal method. Appends the message to the database table. */ public void append(LoggingEvent event) { if(!ready) { if(!ready()) { errorHandler.error("JDBCAppender::append(), Not ready to append !"); return; } } buffer.add(event); if(buffer.size() >= buffer_size) flush_buffer(); } /** Internal method. Flushes the buffer. */ public void flush_buffer() { try { int size = buffer.size(); if(size < 1) return; for(int i=0; i<size; i++) { LoggingEvent event = (LoggingEvent)buffer.get(i); //Insert message into database jlogger.append(layout.format(event)); } buffer.clear(); if(docommit) con.commit(); } catch(Exception e) { errorHandler.error("JDBCAppender::flush_buffer(), " + e + " : " + jlogger.getErrorMsg()); try{con.rollback();} catch(Exception ex){} return; } } /** Internal method. Returns true, when the JDBCAppender is ready to append messages to the database, else false. */ public boolean ready() { if(ready) return true; if(!configured) return false; ready = jlogger.ready(); if(!ready){errorHandler.error(jlogger.getErrorMsg());} return ready; } /** Internal method. Connect to the database. */ protected void connect() throws Exception { if(connected) return; try { if(connection_class == null) { if(url == null) throw new Exception("JDBCAppender::connect(), No URL defined."); if(username == null) throw new Exception("JDBCAppender::connect(), No USERNAME defined."); if(password == null) throw new Exception("JDBCAppender::connect(), No PASSWORD defined."); connectionHandler = new DefaultConnectionHandler(); } else { connectionHandler = (JDBCConnectionHandler)(Class.forName(connection_class).newInstance()); } if(url != null && username != null && password != null) { con = connectionHandler.getConnection(url, username, password); } else { con = connectionHandler.getConnection(); } if(con.isClosed()) { throw new Exception("JDBCAppender::connect(), JDBCConnectionHandler returns no connected Connection !"); } } catch(Exception e) { throw new Exception("JDBCAppender::connect(), " + e); } connected = true; } /** Internal method. Configures for appending... */ protected boolean configure() { if(configured) return true; if(!connected) { if((connection_class == null) && (url == null || username == null || password == null)) { errorHandler.error("JDBCAppender::configure(), Missing database-options or connector-option !"); return false; } try { connect(); } catch(Exception e) { connection_class = null; url = null; errorHandler.error("JDBCAppender::configure(), " + e); return false; } } if(sql == null && table == null) { errorHandler.error("JDBCAppender::configure(), No SQL_OPTION or TABLE_OPTION given !"); return false; } if(!jlogger.isConfigured()) { try { jlogger.setConnection(con); if(sql == null) { jlogger.configureTable(table); } else jlogger.configureSQL(sql); } catch(Exception e) { errorHandler.error("JDBCAppender::configure(), " + e); return false; } } //Default Message-Layout if(layout == null) { layout = new PatternLayout("%m"); } configured = true; return true; }}/**This is a default JDBCConnectionHandler used by JDBCAppender*/class DefaultConnectionHandler implements JDBCConnectionHandler{ Connection con = null; public Connection getConnection() { return con; } public Connection getConnection(String _url, String _username, String _password) { try { if(con != null && !con.isClosed()) con.close(); con = DriverManager.getConnection(_url, _username, _password); con.setAutoCommit(false); } catch(Exception e){} return con; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -