-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathlog_firehose.py
More file actions
32 lines (28 loc) · 1.02 KB
/
log_firehose.py
File metadata and controls
32 lines (28 loc) · 1.02 KB
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
import boto3
import logging
import json
import os
from botocore.config import Config
logging.basicConfig()
logger = logging.getLogger()
logger.setLevel("INFO")
class FirehoseLogger:
def __init__(
self,
stream_name: str = os.getenv("SPLUNK_FIREHOSE_NAME"),
boto_client=boto3.client("firehose", config=Config(region_name="eu-west-2")),
):
self.firehose_client = boto_client
self.delivery_stream_name = stream_name
def send_log(self, log_message):
try:
log_to_splunk = log_message
logger.info(f"Log sent to Firehose for save: {log_to_splunk}")
encoded_log_data = json.dumps(log_to_splunk).encode("utf-8")
response = self.firehose_client.put_record(
DeliveryStreamName=self.delivery_stream_name,
Record={"Data": encoded_log_data},
)
logger.info(f"Log sent to Firehose: {response}")
except Exception as e:
logger.exception(f"Error sending log to Firehose: {e}")