Skip to content

Instantly share code, notes, and snippets.

@claytantor
Last active October 12, 2016 18:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save claytantor/a86c237f9e7f5724f015cfb0ef220a9e to your computer and use it in GitHub Desktop.
Save claytantor/a86c237f9e7f5724f015cfb0ef220a9e to your computer and use it in GitHub Desktop.
using cloudwatch logs client
public class AwsLogQuery {
private final static Logger logger = LoggerFactory.getLogger(AwsLogQuery.class);
// use the role of the lambda to determine policy.
protected static final AWSCredentialsProvider CREDENTIALS_PROVIDER =
new DefaultAWSCredentialsProviderChain();
public static void main(String[] args){
long startTime = 1476119058044l;
long endTime = 1476119159726l;
String filterPattern = "99bf4094";
String logGroup = "/aws/lambda/DynamodbExampleLambda-dev";
AwsLogQuery q = new AwsLogQuery();
q.getLogEvents(logGroup,startTime,endTime,filterPattern);
}
public void getLogEvents(String logGroup, long startTime, long endTime, String filterPattern){
ClientConfiguration logsClientCfg = new ClientConfiguration();
Protocol logsProtocolType = Protocol.HTTPS;
logsClientCfg.setProtocol(logsProtocolType);
AWSLogsClient cloudWatchLogs = new AWSLogsClient(CREDENTIALS_PROVIDER, logsClientCfg)
.withEndpoint("https://logs.us-west-2.amazonaws.com")
.withRegion(Region.getRegion(Regions.US_WEST_2));
try {
FilterLogEventsRequest request = new FilterLogEventsRequest();
request.setStartTime(startTime);
request.setEndTime(endTime);
request.setLogGroupName(logGroup);
request.setFilterPattern(filterPattern);
logger.info(request.toString());
FilterLogEventsResult result = cloudWatchLogs.filterLogEvents(request);
result.getNextToken();
logger.info(result.toString());
logger.info("events found:"+result.getEvents().size());
result.getEvents().forEach(event->{
logger.info(event.toString());
});
} catch (Exception e) {
logger.error(e.getMessage(),e);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment