kkesley
kkesley

Reputation: 3406

AWS Lambda golang logging from external package

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.

Upvotes: 4

Views: 2902

Answers (1)

Brian McCall
Brian McCall

Reputation: 1907

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

Upvotes: 3

Related Questions