我正在尝试在调用 lambda 函数后检索特定于 lambda 函数的日志。调用函数后是否有合适的 aws java sdk 方法来检索 lambda 执行的日志?
最佳答案
AWS Lambda - Logging (Java)文档提出了三个建议:
- Find logs in CloudWatch Logs. The context object (in the aws-lambda-java-core library) provides the getLogStreamName() and the getLogGroupName() methods. Using these methods, you can find the specific log stream where logs are written.
这个案例最符合你的问题。从您的 context object 获取 CloudWatch 日志组和日志流, 然后将它们传递给 SDK 的 AWSLogsClient getLogEvents()检索日志的方法。
- If you invoke a Lambda function via the console, the invocation type is always RequestResponse (that is, synchronous execution) and the console displays the logs that the Lambda function writes using the LambdaLogger object. AWS Lambda also returns logs from System.out and System.err methods.
此案例对于从控制台进行手动测试/调试非常有用。
- If you invoke a Lambda function programmatically, you can add the LogType parameter to retrieve the last 4 KB of log data that is written to CloudWatch Logs. For more information, see Invoke. AWS Lambda returns this log information in the x-amz-log-results header in the response. If you use the AWS Command Line Interface to invoke the function, you can specify the --log-type parameter with value Tail.
这种情况可能对您在开发环境中特别有用,并且更容易获得,因为您只需要 setLogType在 Java SDK 上 InvokeRequest .然后,在响应中,只需检查 getLogResult .
根据您的用途,在生产中使用此案例之前请仔细考虑。例如,这会将您的日志泄露给您的客户吗?即使没有,您的音量是否足以使这不切实际?
关于java - 从 Java 客户端调用 lambda 函数后如何访问 lambda 函数调用的云监视日志,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43023759/