Exemplo n.º 1
0
  /** 日志服务主方法 由日志服务线程调用 */
  public void process(Collection<LogEvent> list) {
    for (LogEvent e : list) {

      /*
       * 对每一个LogEvent检查是否被过滤
       */
      if (checkFilter(e) == false) {
        continue;
      }

      // 获取LogAppender并输出日志
      LogAppender appender = repository.getLogAppender(e);
      appender.append(e.getLogContent());
    }
  }
 public int deleteAllExceptLastNRecords(final int recordsAmountToKeepAlive)
     throws DataAccessException {
   Integer logRecordsCountTotal = logRepository.countAll();
   if (recordsAmountToKeepAlive >= logRecordsCountTotal) {
     return 0;
   }
   final String temporaryTableNameNew =
       tableName + "_" + UUID.randomUUID().toString().replace("-", "");
   final String temporaryTableNameOld =
       tableName + "_" + UUID.randomUUID().toString().replace("-", "");
   sharedTransactionTemplate.execute(
       new TransactionCallbackWithoutResult() {
         @Override
         protected void doInTransactionWithoutResult(TransactionStatus status) {
           Integer limit = recordsAmountToKeepAlive;
           String[] sql = {
             "CREATE TABLE IF NOT EXISTS " + temporaryTableNameNew + " LIKE " + tableName,
             "INSERT INTO "
                 + temporaryTableNameNew
                 + " SELECT * FROM "
                 + tableName
                 + " ORDER BY occurred_at DESC LIMIT "
                 + limit,
             "RENAME TABLE "
                 + tableName
                 + " TO "
                 + temporaryTableNameOld
                 + ", "
                 + temporaryTableNameNew
                 + " TO "
                 + tableName,
             "DROP TABLE " + temporaryTableNameOld
           };
           jdbcTemplate.batchUpdate(sql);
         }
       });
   return logRecordsCountTotal - recordsAmountToKeepAlive;
 }