Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add zapio.Writer #971

Merged
merged 4 commits into from Jun 25, 2021
Merged

Add zapio.Writer #971

merged 4 commits into from Jun 25, 2021

Commits on Jun 24, 2021

  1. Add zapio.Writer

    This adds a new zapio package that provides a `Writer`. The writer
    implements `io.WriteCloser` and `zapcore.WriteSyncer`.
    
    It works by splitting the input on newlines, flushing to the logger as
    new lines are encountered, and buffering input otherwise.
    
    So for example, if write "foobar\n" is split across multiple Write calls
    "foo" and "bar\n", instead of emitting two separate logs for "foo"
    and "bar", the Writer will buffer the input until the newline is
    encountered and write a single log for "foobar".
    
    Resolves #929
    abhinav committed Jun 24, 2021
    Copy the full SHA
    0072bc4 View commit details
    Browse the repository at this point in the history
  2. Add a benchmark

    Add a benchmark that measures the performance of the writer when writes are
    split across multiple lines, as well as when it's one log per write.
    
    ```
    name             time/op
    Writer/single-4  422ns ±21%
    Writer/splits-4  433ns ± 4%
    
    name             alloc/op
    Writer/single-4  16.0B ± 0%
    Writer/splits-4  16.0B ± 0%
    
    name             allocs/op
    Writer/single-4   2.00 ± 0%
    Writer/splits-4   2.00 ± 0%
    ```
    abhinav committed Jun 24, 2021
    Copy the full SHA
    a17fef8 View commit details
    Browse the repository at this point in the history
  3. Fast path for empty buffer

    If we don't have a leftover partial write in the buffer, we can skip the
    buffer completely and write directly to the logger.
    
    This improves performance when log statements aren't split across too many
    writes.
    
    ```
    name             old time/op    new time/op    delta
    Writer/single-4     422ns ±21%     383ns ± 2%  -9.26%  (p=0.000 n=10+9)
    Writer/splits-4     433ns ± 4%     435ns ± 1%    ~     (p=0.236 n=9+8)
    
    name             old alloc/op   new alloc/op   delta
    Writer/single-4     16.0B ± 0%     16.0B ± 0%    ~     (all equal)
    Writer/splits-4     16.0B ± 0%     16.0B ± 0%    ~     (all equal)
    
    name             old allocs/op  new allocs/op  delta
    Writer/single-4      2.00 ± 0%      2.00 ± 0%    ~     (all equal)
    Writer/splits-4      2.00 ± 0%      2.00 ± 0%    ~     (all equal)
    ```
    abhinav committed Jun 24, 2021
    Copy the full SHA
    0c8d989 View commit details
    Browse the repository at this point in the history
  4. sync: Test for empty flush

    Add a test case to verify the behavior of Sync, especially with regard to
    not logging anything when the buffer is empty.
    abhinav committed Jun 24, 2021
    Copy the full SHA
    b6abcb8 View commit details
    Browse the repository at this point in the history