|
1 | 1 | import dataclasses
|
2 | 2 | import logging
|
3 | 3 | import threading
|
| 4 | +import time |
4 | 5 | from queue import Queue
|
5 | 6 | from typing import Optional, Union
|
6 | 7 |
|
7 | 8 | from localstack.aws.connect import connect_to
|
8 | 9 | from localstack.utils.aws.client_types import ServicePrincipal
|
9 | 10 | from localstack.utils.bootstrap import is_api_enabled
|
10 |
| -from localstack.utils.cloudwatch.cloudwatch_util import store_cloudwatch_logs |
11 | 11 | from localstack.utils.threads import FuncThread
|
12 | 12 |
|
13 | 13 | LOG = logging.getLogger(__name__)
|
@@ -50,10 +50,32 @@ def run_log_loop(self, *args, **kwargs) -> None:
|
50 | 50 | log_item = self.log_queue.get()
|
51 | 51 | if log_item is QUEUE_SHUTDOWN:
|
52 | 52 | return
|
| 53 | + logs = log_item.logs.splitlines() |
| 54 | + # until we have a better way to have timestamps, log events have the same time for a single invocation |
| 55 | + log_events = [ |
| 56 | + {"timestamp": int(time.time() * 1000), "message": log_line} for log_line in logs |
| 57 | + ] |
53 | 58 | try:
|
54 |
| - store_cloudwatch_logs( |
55 |
| - logs_client, log_item.log_group, log_item.log_stream, log_item.logs |
56 |
| - ) |
| 59 | + try: |
| 60 | + logs_client.put_log_events( |
| 61 | + logGroupName=log_item.log_group, |
| 62 | + logStreamName=log_item.log_stream, |
| 63 | + logEvents=log_events, |
| 64 | + ) |
| 65 | + except logs_client.exceptions.ResourceNotFoundException: |
| 66 | + # create new log group |
| 67 | + try: |
| 68 | + logs_client.create_log_group(logGroupName=log_item.log_group) |
| 69 | + except logs_client.exceptions.ResourceAlreadyExistsException: |
| 70 | + pass |
| 71 | + logs_client.create_log_stream( |
| 72 | + logGroupName=log_item.log_group, logStreamName=log_item.log_stream |
| 73 | + ) |
| 74 | + logs_client.put_log_events( |
| 75 | + logGroupName=log_item.log_group, |
| 76 | + logStreamName=log_item.log_stream, |
| 77 | + logEvents=log_events, |
| 78 | + ) |
57 | 79 | except Exception as e:
|
58 | 80 | LOG.warning(
|
59 | 81 | "Error saving logs to group %s in region %s: %s",
|
|
0 commit comments