-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhandler.py
55 lines (49 loc) · 1.62 KB
/
handler.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
import logging
import boto3
from datetime import datetime
class DynamoDBFormatter(logging.Formatter):
def format(self, record):
data = record.__dict__.copy()
return data['msg']
class DynamoDBHandler(logging.StreamHandler):
def __init__(self, level=logging.WARNING):
logging.StreamHandler.__init__(self, level)
self.level = level
self.client = boto3.client(
'dynamodb',
region_name='eu-west-1'
)
self.table_name = 'AutomationLogging'
self.formatter = DynamoDBFormatter()
try:
self.client.create_table(
AttributeDefinitions=[
{
'AttributeName': 'Time',
'AttributeType': 'S',
}
],
KeySchema=[
{
'AttributeName': 'Time',
'KeyType': 'HASH',
}
],
BillingMode='PAY_PER_REQUEST',
TableName=self.table_name,
)
except self.client.exceptions.ResourceInUseException:
pass
def emit(self, record):
formatted_record = self.format(record)
self.client.put_item(
TableName=self.table_name,
Item={
'Time': {
'S': str(datetime.utcnow())
},
'Record': {
'S': str(formatted_record)
}
}
)