zap
zap copied to clipboard
sync /dev/stderr: inappropriate ioctl for device
Both on mac and linux I get this error when doing zapLogger.Sync()
:
sync /dev/stderr: inappropriate ioctl for device
Code:
zapLogger, err := zap.NewProduction()
if err != nil {
log.Fatal(err.Error())
}
defer func() {
if err := zapLogger.Sync(); err != nil {
log.Print(err.Error())
}
}()
logger := zapLogger.Sugar()
Thanks for the report @caevv
This is a known issue, see previous threads related to this: #772 and #370.
The error itself is slightly different on OS X vs Linux.
OS X: inappropriate ioctl for device
Linux: sync /dev/stderr: invalid argument
The error only happens when stdout/stderr point to the console, but not if they are redirected to a file (since files support sync).
I think it's worth considering some workarounds for this issue it's a common scenario. A couple of possible ideas:
- Ignore these specific errors from
Sync
(for any file) - After opening a file, immediately perform a
Sync
, and if returns an error, then ignore futureSync
errors (if they are the same).
Can we have errors.Is(err, zap.ErrConsoleSync)
in the mean time?
This error comes from the underlying platform, so to achieve the above, we'd need to have platform-specific detection, and wrapping of the errors in zap. So it would require a similar amount of work in zap first.
Hey, it's been almost a year. Is there any update on this issue?
Using this as a hotfix. correct me if I have done anything wrong.
lc.Append(fx.Hook{
++ OnStart: func(c context.Context) error {
++ if logger.Sync() != nil {
++ ignoreSync = true
++ }
++ return nil
++ },
OnStop: func(ctx context.Context) error {
++ if ignoreSync {
++ return nil
++ }
err := logger.Sync()
return eris.Wrap(err, "log: could not sync before exit")
},
})
any update?
any update?
any update?
I think you can use this approach for now: https://github.com/uber-go/zap/issues/991#issuecomment-962098428