uber-go / zap

Blazing fast, structured, leveled logging in Go.
https://pkg.go.dev/go.uber.org/zap
MIT License
21.64k stars 1.42k forks source link

sync /dev/stderr: inappropriate ioctl for device #880

Open caevv opened 3 years ago

caevv commented 3 years ago

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()
prashantv commented 3 years ago

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:

caevv commented 3 years ago

Can we have errors.Is(err, zap.ErrConsoleSync) in the mean time?

prashantv commented 3 years ago

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.

hsblhsn commented 3 years ago

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")
        },
    })
loeffel-io commented 2 years ago

any update?

vigo commented 2 years ago

any update?

erhanakp commented 2 years ago

any update?

adriancarayol commented 2 years ago

I think you can use this approach for now: https://github.com/uber-go/zap/issues/991#issuecomment-962098428

brunotacca commented 2 months ago

Up, experienced the same behavior using v1.16.0, Apple Silicon, Sonoma 14.5.