DVA-C02 Question 374
Single answerA developer is building a serverless application using AWS Lambda. To simplify debugging and enable better analysis of logs, the developer wants to implement structured logging. Which approach should the developer follow to achieve this?
- A
Use JSON format for log messages and include key-value pairs for relevant details.
- B
Write plain text log messages and rely on CloudWatch Logs Insights to parse them.
- C
Set up a custom Lambda function to process and structure logs before sending them to CloudWatch.
- D
Use AWS X-Ray to enable structured logging automatically for Lambda functions.
Show answer and explanation
Correct answer: A
Explanation
Structured logging refers to the practice of formatting log messages in a consistent, machine-readable way, often using JSON. This approach allows developers to include key-value pairs that can be easily parsed and analyzed using services like AWS CloudWatch Logs Insights. Writing logs in JSON format directly within the Lambda function is the most efficient and recommended approach for implementing structured logging in AWS environments.
- A. Correct.
Using JSON format with key-value pairs is a common approach for structured logging. It allows logs to be easily parsed and queried in tools like CloudWatch Logs Insights, making it the correct choice.
- B. Incorrect.
Plain text log messages are not structured and require additional effort to parse. While CloudWatch Logs Insights can offer some parsing capabilities, it is not an optimal approach for structured logging.
- C. Incorrect.
Setting up a custom Lambda function to process logs adds unnecessary complexity. AWS services like CloudWatch Logs are already capable of handling structured logs without needing an additional processing layer.
- D. Incorrect.
AWS X-Ray is used for distributed tracing and does not automatically enable structured logging. It is not relevant in this context.