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 反编译一款APP手游,解密出lua脚本。
    • ¥15 报错问题,只有两个错误,如图片所示,i多次初始化
    • ¥15 matlab runtime 如何收费
    • ¥20 用C语言绘制cs1.6方框透视 出现了点问题 绘制不上去 矩阵数据 和敌人坐标都是正确的
    • ¥15 Tpad api账户 api口令
    • ¥30 ppt进度条制作,vba语言
    • ¥15 生信simpleaffy包下载
    • ¥15 在二层网络中,掩码存在包含关系即可通信
    • ¥15 端口转发器解析失败不知道电脑设置了啥
    • ¥15 Latex算法流程图行号自定义