zap icon indicating copy to clipboard operation
zap copied to clipboard

How to use on AWS Lambda

Open cloudlena opened this issue 5 years ago • 4 comments

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?

cloudlena avatar Sep 14 '18 07:09 cloudlena

This works for me:

func init() {
	Logger, LoggerErr = zap.NewProduction()
	if LoggerErr != nil {
		log.Fatalf("Cannot initialize zap logger: %v", LoggerErr)
	}
	defer Logger.Sync()
}

jacknagz avatar Oct 02 '18 23:10 jacknagz

Any updates on this? Facing a similar issue.

Ping : @akshayjshah

kaushikthedeveloper avatar Feb 28 '19 19:02 kaushikthedeveloper

Seems the issue is directly related to this one https://github.com/uber-go/zap/issues/328

dnozdrin avatar Jun 29 '20 14:06 dnozdrin

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.

prashantv avatar Jul 09 '20 04:07 prashantv