Skip to content

Instantly share code, notes, and snippets.

@ken-itakura
Created March 6, 2018 21:23
Show Gist options
  • Save ken-itakura/01efee238235dd9a48c7cd9be6e3e5aa to your computer and use it in GitHub Desktop.
Save ken-itakura/01efee238235dd9a48c7cd9be6e3e5aa to your computer and use it in GitHub Desktop.
monitor cloud watch log continuously
import boto3
from boto3.session import Session
import time
from datetime import datetime
import calendar
def get_log_events(log_group):
"""Generate all the log events from a CloudWatch group.
thanks to https://alexwlchan.net/2017/11/fetching-cloudwatch-logs/
:param log_group: Name of the CloudWatch log group.
"""
use_default_profile = False
if use_default_profile:
client = boto3.client('logs')
else:
profile = 'PROFILE_NAME_HERE' # must define profile "PROFILE_NAME_HERE" in ~/.aws/credentials
session = Session(profile_name=profile)
client = session.client('logs')
minutes_ago = 60
utcNowSec = calendar.timegm(datetime.utcnow().timetuple())
startTime = (utcNowSec - 60 * minutes_ago) * 1000
kwargs = {
'logGroupName': log_group,
'startTime': startTime,
'limit': 10000,
}
while True:
resp = client.filter_log_events(**kwargs)
yield resp['events']
try:
kwargs['nextToken'] = resp['nextToken']
except KeyError:
try:
if len(resp['events']) > 0:
kwargs['startTime'] = resp[u'events'][len(resp['events'])-1]['timestamp'] + 1
kwargs.pop('nextToken', None)
time.sleep(5)
except KeyError:
break
if __name__ == '__main__':
for events in get_log_events('LOGGROUP_NAME_HERE'):
for event in events:
print(event['message'])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment