duangang1991 2018-07-26 06:37
浏览 85
已采纳

来自外部软件包的AWS Lambda golang日志记录

I'm starting to use external packages for my lambda function which is written in Golang.

I use serverless framework if that makes any difference.

I want to centralize the error logging every time there's an error in the DB.

but I notice that the log doesn't show on the cloudwatch. Only the logs in the main package shows.

here's my code

package response
func ServerError(err error) (events.APIGatewayProxyResponse, error) {
    log.Print(fmt.Errorf("ERROR: %v", err))
    return Custom(500, "Internal Server Error", nil)
}

I also tried

package response
func ServerError(err error) (events.APIGatewayProxyResponse, error) {
    fmt.Println(fmt.Errorf("ERROR: %v", err))
    return Custom(500, "Internal Server Error", nil)
}

My question is how can I enable logging in lambda outside of main package?

Thanks!

EDIT

It turns out that log.Print(fmt.Errorf("ERROR: %v", err)) works as well. I must've missed it the last time.

  • 写回答

1条回答 默认 最新

  • dpgu5074 2018-08-27 17:30
    关注

    In the serverless framework anything printed to std.out/std err will be written to the cloudwatch logs. Therefore all you have to do is write your errors to std.err

    fmt.Fprintf(os.Stderr, "log message: %s", str)

    This post touches on writing to std.err https://stackoverflow.com/a/40694000/2840591

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 双层网络上信息-疾病传播
  • ¥50 paddlepaddle pinn
  • ¥20 idea运行测试代码报错问题
  • ¥15 网络监控:网络故障告警通知
  • ¥15 django项目运行报编码错误
  • ¥15 请问这个是什么意思?
  • ¥15 STM32驱动继电器
  • ¥15 Windows server update services
  • ¥15 关于#c语言#的问题:我现在在做一个墨水屏设计,2.9英寸的小屏怎么换4.2英寸大屏
  • ¥15 模糊pid与pid仿真结果几乎一样