zap
zap copied to clipboard
How to use on AWS Lambda
I'm trying to use zap
on AWS Lambda. With the following configuration:
logger, err := zap.NewProduction()
if err != nil {
log.Fatalf("error creating logger: %s", err)
}
...
I am getting the following error when running it on Lambda:
error syncing logger: sync /dev/stderr: invalid argument
when checking for an error on logger.Sync
:
...
defer func() {
err := logger.Sync()
if err != nil {
log.Fatalf("error syncing logger: %s", err)
}
}()
So I tried changing the config to the following:
logConfig := zap.NewProductionConfig()
logConfig.OutputPaths = []string{"stdout"}
logger, err := logConfig.Build()
if err != nil {
log.Fatalf("error creating logger: %s", err)
}
which now gives me another error (from the same call to logger.Sync
):
error syncing logger: sync /dev/stdout: invalid argument
As far as I know, Lambda supports and recommends logging to stdout and stderr so I don't understand why it's not working. Is there a recommended way of fixing this?
This works for me:
func init() {
Logger, LoggerErr = zap.NewProduction()
if LoggerErr != nil {
log.Fatalf("Cannot initialize zap logger: %v", LoggerErr)
}
defer Logger.Sync()
}
Any updates on this? Facing a similar issue.
Ping : @akshayjshah
Seems the issue is directly related to this one https://github.com/uber-go/zap/issues/328
If you are using the a standard WriteSyncer
, then there's no buffering done by zap, so the Flush
is not necessary.
The underlying issue is related to #328 -- unfortunately the Flush is not supported by os.Stdout
and os.Stderr
on Linux, and so it has to be ignored somewhere.