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

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

报告相同问题?

悬赏问题

  • ¥15 io.jsonwebtoken.security.Keys
  • ¥15 急,ubuntu安装后no caching mode page found等
  • ¥15 联想交换机NE2580O/NE1064TO安装SONIC
  • ¥15 防火墙的混合模式配置
  • ¥15 Ubuntu不小心注销了要怎么恢复啊
  • ¥15 win10电脑安装完plcsim advanced4.0运行时为什么会提示找不到虚拟网卡
  • ¥15 安装powerbuilder10卡在安装程序正在运行这个页面 没有下一步任何指令
  • ¥15 关于mpi的问题:请问遇到这种情况需要怎么解决,出现这个问题后电脑不能进行mpi多核运行只能进行单核运行
  • ¥50 微信聊天记录备份到电脑提示成功了,但还是没同步到电脑微信
  • ¥15 python怎么在已有视频文件后添加新帧