diff --git a/localstack/services/lambda_/invocation/logs.py b/localstack/services/lambda_/invocation/logs.py index 32abca4db9ed5..a63f1ab2d04f4 100644 --- a/localstack/services/lambda_/invocation/logs.py +++ b/localstack/services/lambda_/invocation/logs.py @@ -1,13 +1,13 @@ import dataclasses import logging import threading -import time from queue import Queue from typing import Optional, Union from localstack.aws.connect import connect_to from localstack.utils.aws.client_types import ServicePrincipal from localstack.utils.bootstrap import is_api_enabled +from localstack.utils.cloudwatch.cloudwatch_util import store_cloudwatch_logs from localstack.utils.threads import FuncThread LOG = logging.getLogger(__name__) @@ -50,32 +50,10 @@ def run_log_loop(self, *args, **kwargs) -> None: log_item = self.log_queue.get() if log_item is QUEUE_SHUTDOWN: return - logs = log_item.logs.splitlines() - # until we have a better way to have timestamps, log events have the same time for a single invocation - log_events = [ - {"timestamp": int(time.time() * 1000), "message": log_line} for log_line in logs - ] try: - try: - logs_client.put_log_events( - logGroupName=log_item.log_group, - logStreamName=log_item.log_stream, - logEvents=log_events, - ) - except logs_client.exceptions.ResourceNotFoundException: - # create new log group - try: - logs_client.create_log_group(logGroupName=log_item.log_group) - except logs_client.exceptions.ResourceAlreadyExistsException: - pass - logs_client.create_log_stream( - logGroupName=log_item.log_group, logStreamName=log_item.log_stream - ) - logs_client.put_log_events( - logGroupName=log_item.log_group, - logStreamName=log_item.log_stream, - logEvents=log_events, - ) + store_cloudwatch_logs( + logs_client, log_item.log_group, log_item.log_stream, log_item.logs + ) except Exception as e: LOG.warning( "Error saving logs to group %s in region %s: %s",