• Stars
    star
    328
  • Rank 123,582 (Top 3 %)
  • Language
    Go
  • License
    MIT License
  • Created over 4 years ago
  • Updated 15 days ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Minimal structured logging library for Go

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

    • 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

    • Entire encoding process is documented on godoc.
    • 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.

More Repositories

1

code-server

VS Code in the browser
TypeScript
65,191
star
2

coder

Provision remote development environments via Terraform
Go
6,912
star
3

sshcode

Run VS Code on any server over SSH.
Go
5,741
star
4

deploy-code-server

Deploy code-server to the cloud with a few clicks ☁️ 👨🏼‍💻
Shell
834
star
5

sail

Deprecated: Instant, pre-configured VS Code development environments.
Go
630
star
6

code-marketplace

Open source extension marketplace for VS Code.
Go
186
star
7

awesome-code-server

Projects, resources, and tutorials that take code-server to the next level
154
star
8

redjet

High-performance Redis library for Go
Go
137
star
9

awesome-coder

A curated list of awesome Coder resources.
113
star
10

nbin

Fast and robust node.js binary compiler.
TypeScript
106
star
11

enterprise-images

Example Docker images for use with Coder
Shell
88
star
12

envbuilder

Build development environments from a Dockerfile on Docker, Kubernetes, and OpenShift. Enable developers to modify their development environment quickly.
Go
83
star
13

coder-v1-cli

Command line for Coder v1. For Coder v2, go to https://github.com/coder/coder
72
star
14

cli

A minimal Go CLI package.
Go
48
star
15

enterprise-helm

Operate Coder v1 on Kubernetes
Go
44
star
16

vscode-coder

Open any Coder workspace in VS Code with a single click.
TypeScript
41
star
17

retry

A tiny retry package for Go.
Go
38
star
18

hat

HTTP API testing for Go
Go
36
star
19

wgtunnel

HTTP tunnels over Wireguard
Go
33
star
20

envbox

envbox is an image that enables creating non-privileged containers capable of running system-level software (e.g. dockerd, systemd, etc) in Kubernetes.
Go
27
star
21

node-browser

Use Node in the browser.
TypeScript
26
star
22

wsep

High performance command execution protocol
Go
24
star
23

terraform-provider-coder

Go
23
star
24

flog

Pretty formatted log for Go
Go
22
star
25

packages

Deploy Coder to your preferred cloud with a pre-built package.
HCL
22
star
26

backstage-plugins

Official Coder plugins for the Backstage platform
TypeScript
22
star
27

docs

Markdown content for Coder v1 Docs.
Shell
20
star
28

modules

A collection of Terraform Modules to work with Coder.
HCL
20
star
29

exectrace

Simple eBPF-based exec snooping on Linux packaged as a Go library.
Go
19
star
30

jetbrains-coder

A JetBrains Plugin for Coder Workspaces
Kotlin
17
star
31

code-server-aur

code-server AUR package
Shell
15
star
32

requirefs

Create a readable and requirable file system from tars, zips, or a custom provider.
TypeScript
11
star
33

labeler

A GitHub app that labels your issues for you
Go
10
star
34

cloud-agent

The agent for Coder Cloud
Go
10
star
35

ts-logger

TypeScript
10
star
36

coder.rs

[EXPERIMENTAL] Asynchronous Rust wrapper around the Coder Enterprise API
Rust
9
star
37

coder-logstream-kube

Stream Kubernetes Pod events to the Coder startup logs
Go
9
star
38

pretty

TTY styles for Go
Go
9
star
39

homebrew-coder

Coder Homebrew Tap
Ruby
8
star
40

bigdur

A Go package for parsing larger durations.
Go
8
star
41

timer

Accurately measure how long a command takes to run
Go
7
star
42

serpent

CLI framework for scale and configurability inspired by Cobra
Go
5
star
43

coder-doctor

A preflight check tool for Coder
Go
5
star
44

blogs

Content for coder.com/blog
D2
5
star
45

webinars

HCL
4
star
46

nfy

EXPERIMENTAL: Pumped up install scripts
Go
4
star
47

ghlabels

A tool to synchronize labels on GitHub repositories sanely.
Go
4
star
48

devcontainer-webinar

The Good, The Bad, And The Future of Dev Containers
Shell
4
star
49

envbuilder-starter-devcontainer

A sample project for getting started with devcontainer.json in envbuilder
Dockerfile
4
star
50

presskit

press kit and brand assets for Coder.com
3
star
51

gke-disk-cleanup

Go
3
star
52

cla

The Coder Contributor License Agreement (CLA)
3
star
53

styleguide

2
star
54

codercord

A Discord bot for our community server
Dart
2
star
55

support-scripts

Things for Coder Customer Success.
Shell
2
star
56

enterprise-terraform

Terraform modules and examples for deploying Coder
HCL
2
star
57

sail-aur

sail AUR package
Shell
2
star
58

grip

extensible logging and messaging framework for go processes.
Go
1
star
59

coder-xray

JFrog XRay Integration
Go
1
star
60

presentations

Talks and presentations related to Coder released under CC0 which permits remixing and reuse!
1
star
61

.github

1
star
62

community-templates

Unofficial templates for Coder for various platforms and cloud providers
HCL
1
star
63

coder-docs-generator

Generates off-line docs for Coder Docs
TypeScript
1
star
64

gott

go test timer
Go
1
star
65

wxnm

A library for providing TypeScript typed communication between your web extension and your native Node application using Native Messaging
TypeScript
1
star
66

v2-templates

HCL
1
star
67

jetbrains-backend-coder

Kotlin
1
star