Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings
/slogPublic

Minimal structured logging library for Go

License

NotificationsYou must be signed in to change notification settings

coder/slog

Repository files navigation

GitHub ReleaseGoDocCoverallsCI Status

slog is a minimal structured logging library for Go.

Install

go get cdr.dev/slog

Features

Example

Many more examples available atgodoc.

log:=slog.Make(sloghuman.Sink(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",time.Date(2000,time.February,5,4,4,4,0,time.UTC)),    )),slog.Error(xerrors.Errorf("wrap1: %w",xerrors.Errorf("wrap2: %w",io.EOF,            ),        ),    ),)

Example output screenshot

Why?

AtCoder we’ve used Uber’szap 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

    • Compareslog tozap andzapcore.
    • 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 asugared APIbut 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 ofzap.Anyfor every field. This isslog.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 representsthe 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 agolang.org/x/xerrors chaininto an array with fields for the location and wrapping messages.
  4. Fullcontext.Context support

    • slog lets you set fields in acontext.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 pluggingthese 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 implementsSink 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 withjson.Marshal

    • Entire encoding process is documented ongodoc.
    • With zap, We found ourselves often implementing zap'sObjectMarshaler to log Go structures. This wasverbose and most of the time we ended up only implementingfmt.Stringer and usingzap.Stringer instead.
  7. slog takes inspiration from Go's stdlib and implementsslog.Helperwhich works just liket.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 anAPI for this but it's verbose and requirespassing the logger around explicitly.
  8. Tight integration with stdlib'stesting package

    • You can configureslogtest to exit on any ERROR logsand it has a global stateless API that takes atesting.TB so you do not need to create a logger first.
    • Test assertion helpers are provided inslogtest/assert.
    • zap haszaptest but the API surface is large and doesn'tintegrate well. It does not support any of the features described above.

About

Minimal structured logging library for Go

Topics

Resources

License

Code of conduct

Security policy

Stars

Watchers

Forks

Packages

No packages published

Contributors11


[8]ページ先頭

©2009-2025 Movatter.jp