Code Monkey home page Code Monkey logo

slog's Introduction

slog

GitHub Release GoDoc Coveralls CI Status

slog is a minimal structured logging library for Go.

Install

go get cdr.dev/slog

Features

Example

Many more examples available at godoc.

log := sloghuman.Make(os.Stdout)

log.Info(context.Background(), "my message here",
    slog.F("field_name", "something or the other"),
    slog.F("some_map", slog.M(
        slog.F("nested_fields", "wowow"),
    )),
    slog.Error(
        xerrors.Errorf("wrap1: %w",
            xerrors.Errorf("wrap2: %w",
                io.EOF,
            ),
        ),
    ),
)

Example output screenshot

Why?

At Coder we’ve used Uber’s zap for several years. It is a fantastic library for performance. Thanks Uber!

However we felt the API and developer experience could be improved.

Here is a list of reasons how we improved on zap with slog.

  1. slog has a minimal API surface

    • Compare slog to zap and zapcore.
    • The sprawling API makes zap hard to understand, use and extend.
  2. slog has a concise semi typed API

    • We found zap's fully typed API cumbersome. It does offer a sugared API but it's too easy to pass an invalid fields list since there is no static type checking. Furthermore, it's harder to read as there is no syntax grouping for each key value pair.
    • We wanted an API that only accepted the equivalent of zap.Any for every field. This is slog.F.
  3. sloghuman uses a very human readable format

    • It colors distinct parts of each line to make it easier to scan logs. Even the JSON that represents the fields in each log is syntax highlighted so that is very easy to scan. See the screenshot above.
      • zap lacks appropriate colors for different levels and fields
    • slog automatically prints one multiline field after the log to make errors and such much easier to read.
      • zap logs multiline fields and errors stack traces as JSON strings which made them unreadable in a terminal.
    • When logging to JSON, slog automatically converts a golang.org/x/xerrors chain into an array with fields for the location and wrapping messages.
  4. Full context.Context support

    • slog lets you set fields in a context.Context such that any log with the context prints those fields.
    • We wanted to be able to pull up all relevant logs for a given trace, user or request. With zap, we were plugging these fields in for every relevant log or passing around a logger with the fields set. This became very verbose.
  5. Simple and easy to extend

    • A new backend only has to implement the simple Sink interface.
    • The Logger type provides a nice API around Sink but also implements Sink to allow for composition.
    • zap is hard and confusing to extend. There are too many structures and configuration options.
  6. Structured logging of Go structures with json.Marshal

    • All values will be logged with json.Marshal unless they implement fmt.Stringer or error.
    • One may implement slog.Value to override the representation completely.
    • With zap, We found ourselves often implementing zap's ObjectMarshaler to log Go structures. This was verbose and most of the time we ended up only implementing fmt.Stringer and using zap.Stringer instead.
  7. slog takes inspiration from Go's stdlib and implements slog.Helper which works just like t.Helper

    • It marks the calling function as a helper and skips it when reporting location info.
    • We had many helper functions for logging but we wanted the line reported to be of the parent function. zap has an API for this but it's verbose and requires passing the logger around explicitly.
  8. Tight integration with stdlib's testing package

    • You can configure slogtest to exit on any ERROR logs and it has a global stateless API that takes a testing.TB so you do not need to create a logger first.
    • Test assertion helpers are provided in slogtest/assert.
    • zap has zaptest but the API surface is large and doesn't integrate well. It does not support any of the features described above.

slog's People

Contributors

nhooyr avatar coadler avatar ammario avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.