com.aliyun.openservices.log.common.LogItem Java Examples

The following examples show how to use com.aliyun.openservices.log.common.LogItem. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: LogProducer.java    From aliyun-log-java-producer with Apache License 2.0 6 votes vote down vote up
/**
 * Send a log asynchronously. It will convert the log to a log list which only contains one log,
 * and then invoke <code>send(project, logStore, topic, source, logItems, logItem,
 * callback)</code>. See {@link #send(String, String, String, String, String, List, Callback)} for
 * details.
 */
@Override
public ListenableFuture<Result> send(
    String project,
    String logStore,
    String topic,
    String source,
    String shardHash,
    LogItem logItem,
    Callback callback)
    throws InterruptedException, ProducerException {
  Utils.assertArgumentNotNull(logItem, "logItem");
  List<LogItem> logItems = new ArrayList<LogItem>();
  logItems.add(logItem);
  return send(project, logStore, topic, source, shardHash, logItems, callback);
}
 
Example #2
Source File: PackageManagerTest.java    From aliyun-log-producer-java with Apache License 2.0 6 votes vote down vote up
@Test
public void testSplitLogItems_10_21() {
    ProducerConfig producerConfig = new ProducerConfig();
    producerConfig.logsCountPerPackage = 10;
    PackageManager packageManager = new PackageManager(producerConfig, new ClientPool(producerConfig));
    List<LogItem> srcLogItems = new ArrayList<LogItem>();
    for (int i = 0; i < 21; ++i) {
        LogItem logItem = new LogItem();
        srcLogItems.add(logItem);
    }
    List<List<LogItem>> sepLogItems = packageManager.splitLogItems(srcLogItems);
    assertEquals(3, sepLogItems.size());
    assertEquals(producerConfig.logsCountPerPackage, sepLogItems.get(0).size());
    assertEquals(producerConfig.logsCountPerPackage, sepLogItems.get(1).size());
    assertEquals(1, sepLogItems.get(2).size());
}
 
Example #3
Source File: Log.java    From wangmarket with Apache License 2.0 6 votes vote down vote up
public static void requestLog(HttpServletRequest request, RequestInfo requestInfo, SImpleSiteVO simpleSiteVO){
	//未开启日志记录
	if(aliyunLogUtil == null){
		return;
	}
	if(simpleSiteVO.getResult() - SImpleSiteVO.FAILURE == 0){
		//失败的,没有访问正规站点的,可能是ip直接访问的,或者访问的未绑定上的域名过来的。这样的不与记录。
		return;
	}
	
	LogItem logItem = new LogItem(DateUtil.timeForUnix10());
	logItem.PushBack("ip", requestInfo.getIp());
	logItem.PushBack("referer", requestInfo.getReferer());
	logItem.PushBack("userAgent", requestInfo.getUserAgent());
	logItem.PushBack("htmlFile", requestInfo.getHtmlFile());
	logItem.PushBack("siteid", simpleSiteVO.getSimpleSite().getId()+"");
	logItem.PushBack("serverName", requestInfo.getServerName());
	
	try {
		aliyunLogUtil.cacheLog(logItem);
	} catch (LogException e1) {
		e1.printStackTrace();
	}
}
 
Example #4
Source File: ProducerBatchTest.java    From aliyun-log-java-producer with Apache License 2.0 6 votes vote down vote up
@Test
public void testAppendAttempt() {
  GroupKey groupKey = new GroupKey("project", "logStore", "topic", "source", "shardHash");
  ProducerBatch batch =
      new ProducerBatch(groupKey, "id", 100, 100, 3, System.currentTimeMillis());
  List<LogItem> logItems = new ArrayList<LogItem>();
  logItems.add(new LogItem());
  logItems.add(new LogItem());
  logItems.add(new LogItem());
  logItems.add(new LogItem());
  logItems.add(new LogItem());
  int sizeInBytes = LogSizeCalculator.calculate(logItems);
  ListenableFuture<Result> f = batch.tryAppend(logItems, sizeInBytes, null);
  Assert.assertNotNull(f);
  batch.appendAttempt(new Attempt(true, "xxx", "", "", System.currentTimeMillis()));
  batch.fireCallbacksAndSetFutures();
}
 
Example #5
Source File: ProducerInvalidTest.java    From aliyun-log-java-producer with Apache License 2.0 6 votes vote down vote up
@Test
public void testSendLogsThrownLogSizeTooLargeException()
    throws InterruptedException, ProducerException {
  ProducerConfig producerConfig = new ProducerConfig();
  producerConfig.setTotalSizeInBytes(30);
  Producer producer = new LogProducer(producerConfig);
  producer.putProjectConfig(buildProjectConfig());
  thrown.expect(LogSizeTooLargeException.class);
  thrown.expectMessage(
      "the logs is 36 bytes which is larger than the totalSizeInBytes you specified");
  List<LogItem> logItems = new ArrayList<LogItem>();
  logItems.add(ProducerTest.buildLogItem());
  logItems.add(ProducerTest.buildLogItem());
  logItems.add(ProducerTest.buildLogItem());
  producer.send("project", "logStore", logItems);
  producer.close();
  ProducerTest.assertProducerFinalState(producer);
}
 
Example #6
Source File: ProducerInvalidTest.java    From aliyun-log-java-producer with Apache License 2.0 6 votes vote down vote up
@Test
public void testSendLogsThrownMaxBatchCountExceedException()
    throws InterruptedException, ProducerException {
  ProducerConfig producerConfig = new ProducerConfig();
  Producer producer = new LogProducer(producerConfig);
  producer.putProjectConfig(buildProjectConfig());
  thrown.expect(MaxBatchCountExceedException.class);
  thrown.expectMessage(
      "the log list size is 40961 which exceeds the MAX_BATCH_COUNT "
          + ProducerConfig.MAX_BATCH_COUNT);
  List<LogItem> logItems = new ArrayList<LogItem>();
  for (int i = 0; i < ProducerConfig.MAX_BATCH_COUNT + 1; ++i) {
    logItems.add(ProducerTest.buildLogItem());
  }
  producer.send("project", "logStore", logItems);
}
 
Example #7
Source File: LogAccumulator.java    From aliyun-log-java-producer with Apache License 2.0 6 votes vote down vote up
public ListenableFuture<Result> append(
    String project,
    String logStore,
    String topic,
    String source,
    String shardHash,
    List<LogItem> logItems,
    Callback callback)
    throws InterruptedException, ProducerException {
  appendsInProgress.incrementAndGet();
  try {
    return doAppend(project, logStore, topic, source, shardHash, logItems, callback);
  } finally {
    appendsInProgress.decrementAndGet();
  }
}
 
Example #8
Source File: ProducerInvalidTest.java    From aliyun-log-java-producer with Apache License 2.0 6 votes vote down vote up
@Test
public void testSendWithNotExistProject() throws InterruptedException, ProducerException {
  ProducerConfig producerConfig = new ProducerConfig();
  Producer producer = new LogProducer(producerConfig);
  producer.putProjectConfig(buildProjectConfig());
  ListenableFuture<Result> f = producer.send("projectNotExist", "logStore", new LogItem(), null);
  try {
    f.get();
  } catch (ExecutionException e) {
    ResultFailedException resultFailedException = (ResultFailedException) e.getCause();
    Result result = resultFailedException.getResult();
    Assert.assertFalse(result.isSuccessful());
    Assert.assertEquals(Errors.PROJECT_CONFIG_NOT_EXIST, result.getErrorCode());
    Assert.assertEquals(
        "Cannot get the projectConfig for project projectNotExist", result.getErrorMessage());
  }
  producer.close();
  ProducerTest.assertProducerFinalState(producer);
}
 
Example #9
Source File: LogSizeCalculator.java    From aliyun-log-java-producer with Apache License 2.0 5 votes vote down vote up
public static int calculate(LogItem logItem) {
  int sizeInBytes = 4;
  for (LogContent content : logItem.GetLogContents()) {
    if (content.mKey != null) {
      sizeInBytes += content.mKey.length();
    }
    if (content.mValue != null) {
      sizeInBytes += content.mValue.length();
    }
  }
  return sizeInBytes;
}
 
Example #10
Source File: ActionLogCache.java    From wangmarket with Apache License 2.0 5 votes vote down vote up
/**
 * 插入一条日志
 * @param logItem 传入要保存的logItem,若为空,则会创建一个新的。此项主要为扩展使用,可自行增加其他信息记录入日志
 * @param request HttpServletRequest
 * @param goalid 操作的目标的id,若无,可为0,也可为空
 * @param action 动作的名字,如:用户登录、更改密码
 * @param remark 动作的描述,如用户将名字张三改为李四
 */
public static synchronized void insert(LogItem logItem, HttpServletRequest request, int goalid, String action, String remark){
	if(aliyunLogUtil == null){
		//不使用日志服务,终止即可
		return;
	}
	if(logItem == null){
		logItem = aliyunLogUtil.newLogItem();
	}
	
	/*用户相关信息,只有用户登录后,才会记录用户信息*/
	User user = ShiroFunc.getUser();
	if(user != null){
		logItem.PushBack("userid", user.getId()+"");
		logItem.PushBack("username", user.getUsername());
	}
	
	/* 动作相关 */
	logItem.PushBack("goalid", goalid+"");
	logItem.PushBack("action", action);
	logItem.PushBack("remark", remark);
	
	/*浏览器自动获取的一些信息*/
	if(request != null){
		logItem.PushBack("ip", IpUtil.getIpAddress(request));
		logItem.PushBack("param", request.getQueryString());
		logItem.PushBack("url", request.getRequestURL().toString());
		logItem.PushBack("referer", request.getHeader("referer"));
		logItem.PushBack("userAgent", request.getHeader("User-Agent"));
	}
	try {
		aliyunLogUtil.cacheLog(logItem);
	} catch (LogException e) {
		e.printStackTrace();
	}
}
 
Example #11
Source File: LogProducer.java    From aliyun-log-java-producer with Apache License 2.0 5 votes vote down vote up
/**
 * Send a log asynchronously. Equivalent to <code>send(project, logStore, topic, source, "",
 * logItem, callback)</code>. See {@link #send(String, String, String, String, String, LogItem,
 * Callback)} for details.
 */
@Override
public ListenableFuture<Result> send(
    String project,
    String logStore,
    String topic,
    String source,
    LogItem logItem,
    Callback callback)
    throws InterruptedException, ProducerException {
  return send(project, logStore, topic, source, null, logItem, callback);
}
 
Example #12
Source File: LogProducer.java    From aliyun-log-java-producer with Apache License 2.0 5 votes vote down vote up
/**
 * Send a list of logs asynchronously. Equivalent to <code>send(project, logStore, "", "", "",
 * logItems, callback)</code>. See {@link #send(String, String, String, String, String, List,
 * Callback)} for details.
 */
@Override
public ListenableFuture<Result> send(
    String project, String logStore, List<LogItem> logItems, Callback callback)
    throws InterruptedException, ProducerException {
  return send(project, logStore, "", "", null, logItems, callback);
}
 
Example #13
Source File: LogProducer.java    From aliyun-log-java-producer with Apache License 2.0 5 votes vote down vote up
/**
 * Send a list of logs asynchronously. Equivalent to <code>send(project, logStore, topic, source,
 * shardHash, logItems, null)</code>. See {@link #send(String, String, String, String, String,
 * List, Callback)} for details.
 */
@Override
public ListenableFuture<Result> send(
    String project,
    String logStore,
    String topic,
    String source,
    String shardHash,
    List<LogItem> logItems)
    throws InterruptedException, ProducerException {
  return send(project, logStore, topic, source, shardHash, logItems, null);
}
 
Example #14
Source File: ProducerInvalidTest.java    From aliyun-log-java-producer with Apache License 2.0 5 votes vote down vote up
@Test
public void testSendWithEmptyLogItems() throws InterruptedException, ProducerException {
  ProducerConfig producerConfig = new ProducerConfig();
  Producer producer = new LogProducer(producerConfig);
  producer.putProjectConfig(buildProjectConfig());
  thrown.expect(IllegalArgumentException.class);
  thrown.expectMessage("logItems cannot be empty");
  List<LogItem> logItems = new ArrayList<LogItem>();
  producer.send("project", "logStore", logItems);
  producer.close();
  ProducerTest.assertProducerFinalState(producer);
}
 
Example #15
Source File: ProducerInvalidTest.java    From aliyun-log-java-producer with Apache License 2.0 5 votes vote down vote up
@Test
public void testSendWithEmptyProject() throws InterruptedException, ProducerException {
  ProducerConfig producerConfig = new ProducerConfig();
  Producer producer = new LogProducer(producerConfig);
  producer.putProjectConfig(buildProjectConfig());
  thrown.expect(IllegalArgumentException.class);
  thrown.expectMessage("project cannot be empty");
  producer.send("", "logStore", new LogItem());
  producer.close();
  ProducerTest.assertProducerFinalState(producer);
}
 
Example #16
Source File: ProducerTest.java    From aliyun-log-java-producer with Apache License 2.0 5 votes vote down vote up
public static List<LogItem> buildLogItems(int n) {
  List<LogItem> logItems = new ArrayList<LogItem>();
  for (int i = 0; i < n; ++i) {
    logItems.add(buildLogItem());
  }
  return logItems;
}
 
Example #17
Source File: Producer.java    From aliyun-log-java-producer with Apache License 2.0 5 votes vote down vote up
/** See {@link LogProducer#send(String, String, String, String, LogItem, Callback)} */
ListenableFuture<Result> send(
    String project,
    String logStore,
    String topic,
    String source,
    LogItem logItem,
    Callback callback)
    throws InterruptedException, ProducerException;
 
Example #18
Source File: LogProducer.java    From aliyun-log-java-producer with Apache License 2.0 5 votes vote down vote up
/**
 * Send a log asynchronously. Equivalent to <code>send(project, logStore, topic, source, "",
 * logItem, null)</code>. See {@link #send(String, String, String, String, String, LogItem,
 * Callback)} for details.
 */
@Override
public ListenableFuture<Result> send(
    String project, String logStore, String topic, String source, LogItem logItem)
    throws InterruptedException, ProducerException {
  return send(project, logStore, topic, source, null, logItem, null);
}
 
Example #19
Source File: LogProducer.java    From aliyun-log-java-producer with Apache License 2.0 5 votes vote down vote up
/**
 * Send a list of logs asynchronously. Equivalent to <code>send(project, logStore, topic, source,
 * "", logItems, null)</code>. See {@link #send(String, String, String, String, String, List,
 * Callback)} for details.
 */
@Override
public ListenableFuture<Result> send(
    String project, String logStore, String topic, String source, List<LogItem> logItems)
    throws InterruptedException, ProducerException {
  return send(project, logStore, topic, source, null, logItems, null);
}
 
Example #20
Source File: Producer.java    From aliyun-log-java-producer with Apache License 2.0 5 votes vote down vote up
/** See {@link LogProducer#send(String, String, String, String, String, LogItem)} */
ListenableFuture<Result> send(
    String project,
    String logStore,
    String topic,
    String source,
    String shardHash,
    LogItem logItem)
    throws InterruptedException, ProducerException;
 
Example #21
Source File: Producer.java    From aliyun-log-java-producer with Apache License 2.0 5 votes vote down vote up
/** See {@link LogProducer#send(String, String, String, String, String, List)} */
ListenableFuture<Result> send(
    String project,
    String logStore,
    String topic,
    String source,
    String shardHash,
    List<LogItem> logItems)
    throws InterruptedException, ProducerException;
 
Example #22
Source File: ProducerBatch.java    From aliyun-log-java-producer with Apache License 2.0 5 votes vote down vote up
public ListenableFuture<Result> tryAppend(
    List<LogItem> items, int sizeInBytes, Callback callback) {
  if (!hasRoomFor(sizeInBytes, items.size())) {
    return null;
  } else {
    SettableFuture<Result> future = SettableFuture.create();
    logItems.addAll(items);
    thunks.add(new Thunk(callback, future));
    curBatchCount += items.size();
    curBatchSizeInBytes += sizeInBytes;
    return future;
  }
}
 
Example #23
Source File: ProducerInvalidTest.java    From aliyun-log-java-producer with Apache License 2.0 5 votes vote down vote up
@Test
public void testSendWithEmptyLogStore() throws InterruptedException, ProducerException {
  ProducerConfig producerConfig = new ProducerConfig();
  Producer producer = new LogProducer(producerConfig);
  producer.putProjectConfig(buildProjectConfig());
  thrown.expect(IllegalArgumentException.class);
  thrown.expectMessage("logStore cannot be empty");
  producer.send("project", "", new LogItem());
  producer.close();
  ProducerTest.assertProducerFinalState(producer);
}
 
Example #24
Source File: Producer.java    From aliyun-log-java-producer with Apache License 2.0 5 votes vote down vote up
/** See {@link LogProducer#send(String, String, String, String, List, Callback)} */
ListenableFuture<Result> send(
    String project,
    String logStore,
    String topic,
    String source,
    List<LogItem> logItems,
    Callback callback)
    throws InterruptedException, ProducerException;
 
Example #25
Source File: LoghubAppenderCallback.java    From aliyun-log-logback-appender with Apache License 2.0 5 votes vote down vote up
public LoghubAppenderCallback(LoghubAppender<E> loghubAppender, String project, String logstore, String topic,
                              String source, List<LogItem> logItems) {
    super();
    this.loghubAppender = loghubAppender;
    this.project = project;
    this.logstore = logstore;
    this.topic = topic;
    this.source = source;
    this.logItems = logItems;
}
 
Example #26
Source File: Producer.java    From aliyun-log-java-producer with Apache License 2.0 5 votes vote down vote up
/** See {@link LogProducer#send(String, String, String, String, String, LogItem, Callback)} */
ListenableFuture<Result> send(
    String project,
    String logStore,
    String topic,
    String source,
    String shardHash,
    LogItem logItem,
    Callback callback)
    throws InterruptedException, ProducerException;
 
Example #27
Source File: Producer.java    From aliyun-log-java-producer with Apache License 2.0 5 votes vote down vote up
/** See {@link LogProducer#send(String, String, String, String, String, List, Callback)} */
ListenableFuture<Result> send(
    String project,
    String logStore,
    String topic,
    String source,
    String shardHash,
    List<LogItem> logItems,
    Callback callback)
    throws InterruptedException, ProducerException;
 
Example #28
Source File: LogProducerTest.java    From aliyun-log-producer-java with Apache License 2.0 5 votes vote down vote up
private List<LogItem> getLogItems() {
    List<LogItem> logItems = new ArrayList<LogItem>();

    LogItem logItem1 = new LogItem((int) (new Date().getTime() / 1000));
    logItem1.PushBack("key1", "val1");
    LogItem logItem2 = new LogItem((int) (new Date().getTime() / 1000));
    logItem2.PushBack("key2", "val2");

    logItems.add(logItem1);
    logItems.add(logItem2);
    return logItems;
}
 
Example #29
Source File: PackageManagerTest.java    From aliyun-log-producer-java with Apache License 2.0 5 votes vote down vote up
@Test
public void testSplitLogItems_default_4096() {
    ProducerConfig producerConfig = new ProducerConfig();
    PackageManager packageManager = new PackageManager(producerConfig, new ClientPool(producerConfig));
    List<LogItem> srcLogItems = new ArrayList<LogItem>();
    for (int i = 0; i < 4096; ++i) {
        LogItem logItem = new LogItem();
        srcLogItems.add(logItem);
    }
    List<List<LogItem>> sepLogItems = packageManager.splitLogItems(srcLogItems);
    assertEquals(1, sepLogItems.size());
    assertEquals(producerConfig.logsCountPerPackage, sepLogItems.get(0).size());
}
 
Example #30
Source File: PackageManagerTest.java    From aliyun-log-producer-java with Apache License 2.0 5 votes vote down vote up
@Test
public void testSplitLogItems_10_0() {
    ProducerConfig producerConfig = new ProducerConfig();
    producerConfig.logsCountPerPackage = 10;
    PackageManager packageManager = new PackageManager(producerConfig, new ClientPool(producerConfig));
    List<LogItem> srcLogItems = new ArrayList<LogItem>();
    List<List<LogItem>> sepLogItems = packageManager.splitLogItems(srcLogItems);
    assertEquals(0, sepLogItems.size());
}