title: How to configure a custom zap logger that writes to output only at error level?
body: <p>I’m trying to build a customized logger that keeps log messages below Error level in a buffer and flushes the buffer only after encountering an Error.</p>
  <p>The problem is that I don’t know how to trigger the flushing of the logs to the output (Sync method) when encountering an Error.</p>
  <p>The below code is an attempt to do so:</p>
  <pre><code>func CustomLogger() {
     // First, define our level-handling logic.
     lowPriority := zap.LevelEnablerFunc(func(lvl zapcore.Level) bool {
        return lvl &lt; activationLevel
     })
     //define the output of the logs
     customWriteSyncer := Buffer(os.Stdout)
  
     consoleEncoder := zapcore.NewConsoleEncoder(zap.NewDevelopmentEncoderConfig())
     zapcore.NewJSONEncoder(zap.NewProductionEncoderConfig())
  
     //creates a Core that writes logs to a WriteSyncer
     core := zapcore.NewCore(consoleEncoder, customWriteSyncer, lowPriority)
  
     logger := zap.New(core)
  
     defer logger.Sync()
    
     Logger = logger
  }
  
  type BufferWriterSync struct {
     buf *bufio.Writer
  }
  
  func Buffer(ws zapcore.WriteSyncer) zapcore.WriteSyncer {
     bw := &amp;BufferWriterSync{
        buf: bufio.NewWriter(ws),
     }
     ws = zapcore.Lock(bw)
     return ws
  }
  // Sync syncs data to output
  func (w BufferWriterSync) Sync() error {
     return w.buf.Flush()
  }
  // Write writes data to buffer
  func (w BufferWriterSync) Write(p []byte) (int, error) {
     return w.buf.Write(p)
  } 
  
  </code></pre>
  <p>Example, when performing:</p>
  <ul>
  <li><p><code>logger.Info(&quot;some Info message&quot;)</code> this message ends up in the buffer of bufio.Writer and the Info message is not displayed</p>
  </li>
  <li><p><code>logger.Info(&quot;some Info message2&quot;)</code> this message ends up in the buffer of bufio.Writer and the Info message is not displayed</p>
  </li>
  <li><p><code>logger.Error(&quot;some Error message&quot;)</code> <strong>only when encountering logging of an error</strong> all the accumulated logs from the buffered must be flushed to the output, based on the above code example it should go to <code>os.Stdout</code></p>
  </li>
  </ul>
  <p>Expected output:</p>
  <pre><code>some Info message
  some Info message2
  some Error message
  </code></pre>
  <p>NOTE:
  The functionality that I am trying to achieve is similar to <strong>fingers_crossed</strong> feature that is present in <a href="https://symfony.com/doc/current/logging.html#handlers-that-modify-log-entries" rel="nofollow noreferrer">Php Symfony framework</a>.</p>