MCPcopy Index your code
hub / github.com/coder/slog

github.com/coder/slog @v3.1.0

Chat with this repo
repository ↗ · DeepWiki ↗ · release v3.1.0 ↗ · + Follow
201 symbols 783 edges 30 files 63 documented · 31%
What it actually does AI analysis from the code graph — generated when you open this
loading…
README

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 := 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?

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

  2. Compare slog to zap and zapcore.

  3. The sprawling API makes zap hard to understand, use and extend.

  4. slog has a concise semi typed API

  5. 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.

  6. We wanted an API that only accepted the equivalent of zap.Any for every field. This is slog.F.

  7. sloghuman uses a very human readable format

  8. 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.
  9. 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.
  10. When logging to JSON, slog automatically converts a golang.org/x/xerrors chain into an array with fields for the location and wrapping messages.

  11. Full context.Context support

  12. slog lets you set fields in a context.Context such that any log with the context prints those fields.

  13. 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.

  14. Simple and easy to extend

  15. A new backend only has to implement the simple Sink interface.

  16. The Logger type provides a nice API around Sink but also implements Sink to allow for composition.
  17. zap is hard and confusing to extend. There are too many structures and configuration options.

  18. Structured logging of Go structures with json.Marshal

  19. Entire encoding process is documented on godoc.

  20. 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.

  21. slog takes inspiration from Go's stdlib and implements slog.Helper which works just like t.Helper

  22. It marks the calling function as a helper and skips it when reporting location info.

  23. 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.

  24. Tight integration with stdlib's testing package

  25. 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.
  26. Test assertion helpers are provided in slogtest/assert.
  27. zap has zaptest but the API surface is large and doesn't integrate well. It does not support any of the features described above.

Extension points exported contracts — how you extend this code

Sink (Interface)
Sink is the destination of a Logger. All sinks must be safe for concurrent use. [5 implementers]
slog.go

Core symbols most depended-on inside this repo

Equal
called by 55
internal/assert/assert.go
String
called by 23
slog.go
Info
called by 18
slog.go
Sink
called by 14
sloggers/sloghuman/sloghuman.go
Error
called by 12
slog.go
render
called by 9
internal/entryhuman/entry.go
True
called by 9
internal/assert/assert.go
Named
called by 8
slog.go

Shape

Function 112
Method 58
Struct 26
TypeAlias 3
Interface 2

Languages

Go100%

Modules by API surface

slog.go35 symbols
sloggers/slogtest/t_test.go18 symbols
internal/entryhuman/entry.go16 symbols
map.go14 symbols
sloggers/slogtest/t.go13 symbols
sloggers/slogtest/assert/assert_test.go12 symbols
example_test.go9 symbols
sloggers/slogtest/assert/assert.go7 symbols
internal/syncwriter/syncwriter_test.go7 symbols
internal/syncwriter/syncwriter.go7 symbols
internal/assert/assert.go7 symbols
sloggers/slogstackdriver/slogstackdriver.go6 symbols

For agents

$ claude mcp add slog \
  -- python -m otcore.mcp_server <graph>

⬇ download graph artifact