Commit efaa4d0d authored by Quxl's avatar Quxl

x

parent 9aaecc07
...@@ -4,10 +4,10 @@ import java.util.ArrayList; ...@@ -4,10 +4,10 @@ import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.concurrent.ExecutorService; import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors; import java.util.concurrent.Executors;
import java.util.concurrent.atomic.AtomicLong;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import com.aliyun.openservices.aliyun.log.producer.Producer; import com.aliyun.openservices.aliyun.log.producer.Producer;
import com.aliyun.openservices.aliyun.log.producer.Result; import com.aliyun.openservices.aliyun.log.producer.Result;
...@@ -17,6 +17,7 @@ import com.google.common.util.concurrent.FutureCallback; ...@@ -17,6 +17,7 @@ import com.google.common.util.concurrent.FutureCallback;
import com.google.common.util.concurrent.Futures; import com.google.common.util.concurrent.Futures;
import com.google.common.util.concurrent.ListenableFuture; import com.google.common.util.concurrent.ListenableFuture;
@Component
public class AliyunLogProducer { public class AliyunLogProducer {
private static final ExecutorService EXECUTOR_SERVICE = Executors.newCachedThreadPool(); private static final ExecutorService EXECUTOR_SERVICE = Executors.newCachedThreadPool();
...@@ -24,12 +25,11 @@ public class AliyunLogProducer { ...@@ -24,12 +25,11 @@ public class AliyunLogProducer {
private static final String project = System.getenv("PROJECT"); private static final String project = System.getenv("PROJECT");
private static final String logStore = System.getenv("LOG_STORE"); private static final String logStore = System.getenv("LOG_STORE");
public static void sendLog(String logText) { public void sendLog(String logText) {
try { try {
final AtomicLong completed = new AtomicLong(0);
List<LogItem> logItems = new ArrayList<LogItem>(); List<LogItem> logItems = new ArrayList<LogItem>();
ListenableFuture<Result> future = producer.send(project, logStore, logItems); ListenableFuture<Result> future = producer.send(project, logStore, logItems);
Futures.addCallback(future, new SampleFutureCallback(project, logStore, completed), EXECUTOR_SERVICE); Futures.addCallback(future, new SampleFutureCallback(project, logStore, logText), EXECUTOR_SERVICE);
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
} }
...@@ -43,29 +43,27 @@ public class AliyunLogProducer { ...@@ -43,29 +43,27 @@ public class AliyunLogProducer {
private final String logStore; private final String logStore;
private final AtomicLong completed; private final String logText;
SampleFutureCallback(String project, String logStore, AtomicLong completed) { SampleFutureCallback(String project, String logStore, String logText) {
this.project = project; this.project = project;
this.logStore = logStore; this.logStore = logStore;
this.completed = completed; this.logText = logText;
} }
@Override @Override
public void onSuccess(Result result) { public void onSuccess(Result result) {
LOGGER.info("Send logs successfully."); LOGGER.info("Send logs successfully.");
completed.getAndIncrement();
} }
@Override @Override
public void onFailure(Throwable t) { public void onFailure(Throwable errors) {
if (t instanceof ResultFailedException) { if (errors instanceof ResultFailedException) {
Result result = ((ResultFailedException) t).getResult(); Result result = ((ResultFailedException) errors).getResult();
LOGGER.error("Failed to send logs, project={}, logStore={}, result={}", project, logStore, result); LOGGER.error("Failed to send logs, project={}, logStore={}, result={}, log={}", project, logStore, result, logText);
} else { } else {
LOGGER.error("Failed to send log, e=", t); LOGGER.error("Failed to send logs, project={}, logStore={}, errors={}, log={}", project, logStore, errors, logText);
} }
completed.getAndIncrement();
} }
} }
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment