DVA-C02 Question 373
Select 2You are developing a serverless application using AWS Lambda and need to implement structured logging to make your logs easier to query and analyze in Amazon CloudWatch Logs. Which of the following approaches should you take?
- A
Use a logging library like 'winston' or 'pino' to generate JSON-formatted log messages.
- B
Manually format your log messages as plain text and include key-value pairs for important data.
- C
Leverage the AWS SDK's built-in logging functionality to automatically create structured logs.
- D
Use the
console.log()function in your Lambda function to send raw output to CloudWatch Logs. - E
Include a correlation ID in your structured logs to trace requests across distributed systems.
Show answer and explanation
Correct answers: A, E
Explanation
Structured logging involves formatting log data in a structured format like JSON, which enables easier querying and analysis in tools like Amazon CloudWatch Logs. Libraries like 'winston' or 'pino' simplify this process. Adding correlation IDs is another best practice to enhance traceability in distributed systems. Plain text or unstructured logs (e.g., from console.log()) do not provide the same level of functionality or ease of use.
- A. Correct.
This is correct. Using logging libraries like 'winston' or 'pino' allows you to generate JSON-formatted logs, which are structured and can be easily queried in CloudWatch Logs.
- B. Incorrect.
This is incorrect. Manually formatting logs as plain text might include key-value pairs, but plain text logs are not inherently structured and are harder to query in CloudWatch.
- C. Incorrect.
This is incorrect. The AWS SDK does not provide built-in functionality to automatically create structured logs. You need to implement this separately using a logging library or custom logic.
- D. Incorrect.
This is incorrect. Using
console.log()generates unstructured logs, which are harder to query and analyze effectively in CloudWatch Logs. - E. Correct.
This is correct. Including a correlation ID in your logs is a best practice for distributed systems and enhances the value of structured logs by making it easier to trace specific requests.