zap: write error: sync /dev/stdout: invalid argument
Thanks for opensourcing this library!
Everything is fine during program execution, but when the application shuts down (due to an error) zap logs to stderr: write error: sync /dev/stdout: invalid argument
Output:
2017-02-20T17:25:14.708+0100 info starting
2017-02-20T17:25:14.708+0100 info publishing {"port": 8080}
2017-02-20T17:45:54.522+0100 fatal Error publishing the API {"error": "listen tcp :8080: bind: address already in use"}
2017-02-20 17:25:14.708574765 +0100 CET write error: sync /dev/stdout: invalid argument
exit status 1
Relevant set-up code:
config.FileLoggingEnabled = false
config.EncodeLogsAsJson = false
...
encCfg := zapcore.EncoderConfig{
TimeKey: "@timestamp",
LevelKey: "level",
NameKey: "logger",
CallerKey: "caller",
MessageKey: "msg",
StacktraceKey: "stacktrace",
EncodeLevel: zapcore.LowercaseLevelEncoder,
EncodeTime: zapcore.ISO8601TimeEncoder,
EncodeDuration: zapcore.NanosDurationEncoder,
}
...
func Configure() {
writers := []zapcore.WriteSyncer{os.Stdout}
if config.FileLoggingEnabled {
writers = append(writers, newRollingFile(....))
}
GlobalLogger = newZapLogger(config.EncodeLogsAsJson, zapcore.NewMultiWriteSyncer(writers...))
}
func newZapLogger(encodeAsJSON bool, output zapcore.WriteSyncer) *zap.Logger {
encoder := zapcore.NewConsoleEncoder(encoderCfg)
if encodeAsJSON {
encoder = zapcore.NewJSONEncoder(encoderCfg)
}
return zap.New(zapcore.NewCore(encoder, output, zap.NewAtomicLevel()))
}
About this issue
- Original URL
- State: closed
- Created 10 years ago
- Comments: 23 (13 by maintainers)
Commits related to this issue
- Test `Sync()` call when calling AddSync If call to `Sync()` fails, wrap the WriteSyncer with a fake `Sync()`. Fixes #328 — committed to Ulexus/zap by Ulexus 10 years ago
- Test `Sync()` call when calling AddSync If call to `Sync()` fails, wrap the WriteSyncer with a fake `Sync()`. Fixes #328 — committed to Ulexus/zap by Ulexus 10 years ago
- Ignore Sync errors (#347) Take the simplest approach to fixing the cross-platform compatibility issues with syncing stderr and stdout (outlined in #328) and just ignore sync errors. If we come up wit... — committed to uber-go/zap by Ulexus 10 years ago
- Don't return errors on logger.Sync Currently logger.Sync fails often (or always), reading https://github.com/uber-go/zap/issues/328 seems like it's safe to ignore. — committed to status-im/status-protocol-go by cammellos 7 years ago
- Ignore zap sync error when existing According to https://github.com/uber-go/zap/issues/328, `Sync` can fail, but it is not a problem. Ignore it. — committed to bryanl/octant by bryanl 7 years ago
- Ignore zap logger sync error Prior to this commit, this is how all `tkn chains` output starts like: ``` $ tkn chain payload build-push-run-output-image-p229w sync /dev/stderr: invalid argument ... .... — committed to concaf/cli by concaf 4 years ago
- Ignore zap logger sync error Prior to this commit, this is how all `tkn chains` output starts like: ``` $ tkn chain payload build-push-run-output-image-p229w sync /dev/stderr: invalid argument ... .... — committed to tektoncd/cli by concaf 4 years ago
- chore: Ignore stdout/stderr sync errors in audit log Calling `Sync` on stdout/stderr on Linux raises an error that can be ignored. See https://github.com/uber-go/zap/issues/328. Fixes #1514 Signed-... — committed to charithe/cerbos by charithe 4 years ago
- chore: Ignore stdout/stderr sync errors in audit log (#1515) Calling `Sync` on stdout/stderr on Linux raises an error that can be ignored. See https://github.com/uber-go/zap/issues/328. Fixes #15... — committed to cerbos/cerbos by charithe 4 years ago
- fix(logging)_: revert buffering of `os.Stderr` The regression was introduced in 58ed6e5, where `os.Stderr` was wrapped with `bufio.NewWriter`. This approach doesn't work as expected because `bufio.N... — committed to status-im/status-go by osmaczko a year ago
- fix(logging)_: revert buffering of `os.Stderr` The regression was introduced in 58ed6e5, where `os.Stderr` was wrapped with `bufio.NewWriter`. This approach doesn't work as expected because `bufio.N... — committed to status-im/status-go by osmaczko a year ago
- ignore EINVAL when flushing logs Running the `scheduler` in EKS, every 5 mins an error is logging: ``` failed to flush logs: sync /dev/stderr: invalid argument ``` https://github.com/uber-go/zap/is... — committed to slaupster/KAI-Scheduler by slaupster a year ago
- ignore EINVAL when flushing logs (#238) * ignore EINVAL when flushing logs Running the `scheduler` in EKS, every 5 mins an error is logging: ``` failed to flush logs: sync /dev/stderr: invalid argu... — committed to NVIDIA/KAI-Scheduler by slaupster a year ago
If you want to check for this invalid argument you can do so with:
Thanks for the report @toefel18 and @Ulexus
The issue here is that
fsyncbehaves differently on OSX and Linux. When doing aFatal, zap callsSynconos.Stdout, which ends up callingfsync.The documentation for
fsyncspecifies thatEINVALmay be returned depending on the type of file passed in, but does not explicitly call out whether it will work for stdout. The Linux documentation says:The OSX documentation is similar:
This simple example reproduces the issue with
fsyncbehaving differently:OSX:
Linux:
@akshayjshah We have a couple of options here:
SyncEINVALerrors (harder to detect and more error prone)This is trivial to reproduce:
Thanks a lot @Ulexus, I forgot to come back to this
Sorry, what I meant to say is that
ErrInvalidis not the error that’s returned in this case. As the comment onErrInvalidsays, the error is only returned when the*Filemethods are called with*Fileis nil. This snippet shows that the returned error is notos.ErrInvalidin the case whenSyncfails:Output on Linux:
My intent was to let people know its
syscall.EINVALinstead ofos.ErrInvalidI saw listed above, but flagged didn’t work. I’ve confirmed this works.With regards to buffering, even if there isn’t buffering it’s good practice to flush your output to avoid loss. This can happen even if its at the OS layer, so beyond go’s control.
By default
os.Stdout/os.Stderris unbuffered in golang, to make it buffered you can use: