可以使用AWS CloudWatch来查看AWS Lambda Layer的日志。可以将层日志合并到函数日志中,通过查询CloudWatch日志流来查看日志。
以下是查询CloudWatch日志流的Python代码示例:
import boto3
client = boto3.client('logs')
# Replace with the name of your log group
log_group_name = '/aws/lambda/my-layer-function'
# Replace with the name of your log stream (can use list_logs() to get stream names)
log_stream_name = '2021/08/01/[23:59:59]a1b2c3d4e5'
# Query to retrieve logs
query = 'fields @timestamp, @message'
# Start query
response = client.start_query(
logGroupName=log_group_name,
startTime=int((time.time() - 900) * 1000), # 15 minutes ago
endTime=int(time.time() * 1000),
queryString=query,
)
# Get query ID
query_id = response['queryId']
# Check query status
status = 'Running'
while status == 'Running':
stats = client.get_query_results(queryId=query_id)
if stats['status'] == 'Complete':
status = 'Complete'
else:
time.sleep(1)
# Get query results
results = client.get_query_results(queryId=query_id)['results']
# Extract timestamp and message from results
for result in results:
timestamp = result[0]['value']
message = result[1]['value']
print(f'{timestamp}: {message}')
其中log_group_name和log_stream_name需要根据具体情况替换。