Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
Logback: the reliable, generic, fast and flexible logging framework. Copyright (C) 1999-2011, QOS.ch. All rights reserved. This program and the accompanying materials are dual-licensed under either the terms of the Eclipse Public License v1.0 as published by the Eclipse Foundation or (per the licensee's choosing) under the terms of the GNU Lesser General Public License version 2.1 as published by the Free Software Foundation.
 
 package ch.qos.logback.classic.db;
 
 import java.util.Map;
 import java.util.Set;
 
The DBAppender inserts logging events into three database tables in a format independent of the Java programming language. For more information about this appender, please refer to the online manual at http://logback.qos.ch/manual/appenders.html#DBAppender

Author(s):
Ceki Gülcü
Ray DeCampo
Sébastien Pennec
 
 public class DBAppender extends DBAppenderBase<ILoggingEvent> {
   protected String insertPropertiesSQL;
   protected String insertExceptionSQL;
   protected String insertSQL;
   protected static final Method GET_GENERATED_KEYS_METHOD;
 
 
   static final int TIMESTMP_INDEX = 1;
   static final int  FORMATTED_MESSAGE_INDEX  = 2;
   static final int  LOGGER_NAME_INDEX = 3;
   static final int  LEVEL_STRING_INDEX = 4;
   static final int  THREAD_NAME_INDEX = 5;
   static final int  REFERENCE_FLAG_INDEX = 6;
   static final int  ARG0_INDEX = 7;
   static final int  ARG1_INDEX = 8;
   static final int  ARG2_INDEX = 9;
   static final int  ARG3_INDEX = 10;
   static final int  CALLER_FILENAME_INDEX = 11;
   static final int  CALLER_CLASS_INDEX = 12;
   static final int  CALLER_METHOD_INDEX = 13;
   static final int  CALLER_LINE_INDEX = 14;
   static final int  EVENT_ID_INDEX  = 15;
   
   static {
     // PreparedStatement.getGeneratedKeys() method was added in JDK 1.4
     Method getGeneratedKeysMethod;
     try {
       // the
       getGeneratedKeysMethod = PreparedStatement.class.getMethod(
           "getGeneratedKeys", (Class[]) null);
     } catch (Exception ex) {
       getGeneratedKeysMethod = null;
     }
      = getGeneratedKeysMethod;
   }
 
   public void setDbNameResolver(DBNameResolver dbNameResolver) {
     this. = dbNameResolver;
   }
 
   @Override
   public void start() {
     if ( == null)
        = new DefaultDBNameResolver();
      = SQLBuilder.buildInsertSQL();
     super.start();
   }
 
   @Override
   protected void subAppend(ILoggingEvent eventConnection connection,
       PreparedStatement insertStatementthrows Throwable {
 
    bindLoggingEventWithInsertStatement(insertStatementevent);
    
    // This is expensive... should we do it every time?
    bindCallerDataWithPreparedStatement(insertStatementevent.getCallerData());
    int updateCount = insertStatement.executeUpdate();
    if (updateCount != 1) {
      addWarn("Failed to insert loggingEvent");
    }
  }
  
  protected void secondarySubAppend(ILoggingEvent eventConnection connection,
      long eventIdthrows Throwable {
    Map<StringStringmergedMap = mergePropertyMaps(event);
    insertProperties(mergedMapconnectioneventId);
    if (event.getThrowableProxy() != null) {
      insertThrowable(event.getThrowableProxy(), connectioneventId);
    }
  }
      ILoggingEvent eventthrows SQLException {
    stmt.setLong(event.getTimeStamp());
    stmt.setShort(, DBHelper.computeReferenceMask(event));
  }
      Object[] argArraythrows SQLException {
    
    int arrayLen = argArray != null ? argArray.length : 0;
    
    for(int i = 0; i < arrayLen && i < 4; i++) {
      stmt.setString(+iasStringTruncatedTo254(argArray[i]));
    }
    if(arrayLen < 4) {
      for(int i = arrayLeni < 4; i++) {
        stmt.setString(+inull);
      }
    }
  }
     String s = null;
     if(o != null) {
         so.toString();
     }
    if(s == null) {
      return null;
    }
    if(s.length() <= 254) {
      return s;
    } else {
      return s.substring(0, 254);
    }
  }
  
      StackTraceElement[] callerDataArraythrows SQLException {
    StackTraceElement callerData = callerDataArray[0];
    if (callerData != null) {
      stmt.setString(callerData.getFileName());
      stmt.setString(callerData.getClassName());
      stmt.setString(callerData.getMethodName());
      stmt.setString(, Integer.toString(callerData.getLineNumber()));
    }
  }
    Map<StringStringmergedMap = new HashMap<StringString>();
    // we add the context properties first, then the event properties, since
    // we consider that event-specific properties should have priority over
    // context-wide properties.
    Map<StringStringloggerContextMap = event.getLoggerContextVO()
        .getPropertyMap();
    Map<StringStringmdcMap = event.getMDCPropertyMap();
    if (loggerContextMap != null) {
      mergedMap.putAll(loggerContextMap);
    }
    if (mdcMap != null) {
      mergedMap.putAll(mdcMap);
    }
    return mergedMap;
  }
  protected Method getGeneratedKeysMethod() {
  }
  protected String getInsertSQL() {
    return ;
  }
  protected void insertProperties(Map<StringStringmergedMap,
      Connection connectionlong eventIdthrows SQLException {
    Set propertiesKeys = mergedMap.keySet();
    if (propertiesKeys.size() > 0) {
      PreparedStatement insertPropertiesStatement = connection
      for (Iterator i = propertiesKeys.iterator(); i.hasNext();) {
        String key = (Stringi.next();
        String value = (StringmergedMap.get(key);
        insertPropertiesStatement.setLong(1, eventId);
        insertPropertiesStatement.setString(2, key);
        insertPropertiesStatement.setString(3, value);
        if () {
          insertPropertiesStatement.addBatch();
        } else {
          insertPropertiesStatement.execute();
        }
      }
      if () {
        insertPropertiesStatement.executeBatch();
      }
      insertPropertiesStatement.close();
    }
  }

  
Add an exception statement either as a batch or execute immediately if batch updates are not supported.
  void updateExceptionStatement(PreparedStatement exceptionStatement,
      String txtshort ilong eventIdthrows SQLException {
    exceptionStatement.setLong(1, eventId);
    exceptionStatement.setShort(2, i);
    exceptionStatement.setString(3, txt);
      exceptionStatement.addBatch();
    } else {
      exceptionStatement.execute();
    }
  }
  short buildExceptionStatement(IThrowableProxy tpshort baseIndex,
      PreparedStatement insertExceptionStatementlong eventId)
      throws SQLException {
    StringBuilder buf = new StringBuilder();
    ThrowableProxyUtil.subjoinFirstLine(buftp);
    updateExceptionStatement(insertExceptionStatementbuf.toString(),
        baseIndex++, eventId);
    int commonFrames = tp.getCommonFrames();
    for (int i = 0; i < stepArray.length - commonFramesi++) {
      StringBuilder sb = new StringBuilder();
      sb.append(.);
      ThrowableProxyUtil.subjoinSTEP(sbstepArray[i]);
      updateExceptionStatement(insertExceptionStatementsb.toString(),
          baseIndex++, eventId);
    }
    if (commonFrames > 0) {
      StringBuilder sb = new StringBuilder();
      sb.append(.).append("... ").append(commonFrames).append(
          " common frames omitted");
      updateExceptionStatement(insertExceptionStatementsb.toString(),
          baseIndex++, eventId);
    }
    return baseIndex;
  }
  protected void insertThrowable(IThrowableProxy tpConnection connection,
      long eventIdthrows SQLException {
    PreparedStatement exceptionStatement = connection
    short baseIndex = 0;
    while (tp != null) {
      baseIndex = buildExceptionStatement(tpbaseIndexexceptionStatement,
          eventId);
      tp = tp.getCause();
    }
      exceptionStatement.executeBatch();
    }
    exceptionStatement.close();
  }
New to GrepCode? Check out our FAQ X