• Stars
    star
    334
  • Rank 126,264 (Top 3 %)
  • Language
    Go
  • License
    MIT License
  • Created about 5 years ago
  • Updated 6 months 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
67,974
star
2

coder

Provision remote development environments via Terraform
Go
8,129
star
3

sshcode

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

websocket

Minimal and idiomatic WebSocket library for Go
Go
3,892
star
5

deploy-code-server

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

wush

simplest & fastest way to transfer files between computers via WireGuard
Go
779
star
7

sail

Deprecated: Instant, pre-configured VS Code development environments.
Go
628
star
8

picopilot

GitHub Copilot in 70 lines of JavaScript
JavaScript
244
star
9

quartz

A Go time testing library for writing deterministic unit tests
Go
233
star
10

code-marketplace

Open source extension marketplace for VS Code.
Go
221
star
11

awesome-code-server

Projects, resources, and tutorials that take code-server to the next level
165
star
12

envbuilder

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

redjet

High-performance Redis library for Go
Go
141
star
14

awesome-coder

A curated list of awesome Coder resources.
130
star
15

aicommit

become the world's laziest committer
Go
125
star
16

nbin

Fast and robust node.js binary compiler.
TypeScript
107
star
17

hnsw

In-memory vector index for Go
Go
98
star
18

enterprise-images

Example Docker images for use with Coder
Shell
95
star
19

coder-v1-cli

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

vscode-coder

Open any Coder workspace in VS Code with a single click.
TypeScript
66
star
21

cli

A minimal Go CLI package.
Go
49
star
22

enterprise-helm

Operate Coder v1 on Kubernetes
Go
45
star
23

retry

A tiny retry package for Go.
Go
39
star
24

backstage-plugins

Official Coder plugins for the Backstage platform
TypeScript
38
star
25

hat

HTTP API testing for Go
Go
37
star
26

wgtunnel

HTTP tunnels over Wireguard
Go
37
star
27

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
34
star
28

modules

A collection of Terraform Modules to extend Coder templates.
HCL
32
star
29

terraform-provider-coder

Go
30
star
30

cursor-arm

Cursor built for ARM Linux and Windows
Nix
30
star
31

wsep

High performance command execution protocol
Go
27
star
32

node-browser

Use Node in the browser.
TypeScript
27
star
33

packages

Deploy Coder to your preferred cloud with a pre-built package.
HCL
27
star
34

exectrace

Simple eBPF-based exec snooping on Linux packaged as a Go library.
Go
25
star
35

labeler

A GitHub app that labels your issues for you
Go
24
star
36

jetbrains-coder

A JetBrains Plugin for Coder Workspaces
Kotlin
24
star
37

flog

Pretty formatted log for Go
Go
23
star
38

docs

Markdown content for Coder v1 Docs.
Shell
22
star
39

code-server-aur

code-server AUR package
Shell
15
star
40

coder-logstream-kube

Stream Kubernetes Pod events to the Coder startup logs
Go
13
star
41

pretty

TTY styles for Go
Go
13
star
42

ts-logger

TypeScript
13
star
43

requirefs

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

serpent

CLI framework for scale and configurability inspired by Cobra
Go
11
star
45

cloud-agent

The agent for Coder Cloud
Go
11
star
46

homebrew-coder

Coder Homebrew Tap
Ruby
11
star
47

coder.rs

[EXPERIMENTAL] Asynchronous Rust wrapper around the Coder Enterprise API
Rust
10
star
48

bigdur

A Go package for parsing larger durations.
Go
9
star
49

timer

Accurately measure how long a command takes to run
Go
9
star
50

terraform-provider-coderd

Manage a Coder deployment using Terraform
Go
9
star
51

webinars

HCL
8
star
52

presskit

press kit and brand assets for Coder.com
7
star
53

envbuilder-starter-devcontainer

A sample project for getting started with devcontainer.json in envbuilder
Dockerfile
7
star
54

coder-doctor

A preflight check tool for Coder
Go
6
star
55

observability

Go
6
star
56

blogs

Content for coder.com/blog
D2
6
star
57

terraform-provider-envbuilder

Go
6
star
58

nfy

EXPERIMENTAL: Pumped up install scripts
Go
5
star
59

ghlabels

A tool to synchronize labels on GitHub repositories sanely.
Go
5
star
60

codercord

A Discord bot for our community server
Dart
5
star
61

devcontainer-webinar

The Good, The Bad, And The Future of Dev Containers
Shell
5
star
62

jetbrains-backend-coder

Kotlin
5
star
63

coder-xray

JFrog XRay Integration
Go
4
star
64

gke-disk-cleanup

Go
4
star
65

cla

The Coder Contributor License Agreement (CLA)
4
star
66

starquery

Query in near-realtime if a user has starred a GitHub repository.
Go
4
star
67

setup-action

Downloads and Configures Coder.
4
star
68

support-scripts

Things for Coder Customer Success.
Shell
3
star
69

.github

3
star
70

enterprise-terraform

Terraform modules and examples for deploying Coder
HCL
3
star
71

sail-aur

sail AUR package
Shell
3
star
72

styleguide

2
star
73

grip

extensible logging and messaging framework for go processes.
Go
2
star
74

presentations

Talks and presentations related to Coder released under CC0 which permits remixing and reuse!
2
star
75

coder-docs-generator

Generates off-line docs for Coder Docs
TypeScript
2
star
76

community-templates

Unofficial templates for Coder for various platforms and cloud providers
HCL
2
star
77

gott

go test timer
Go
2
star
78

wxnm

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

internal

Non-community issues related to coder/coder
2
star
80

synology

a work in progress prototype
Shell
2
star
81

feeltty

Quantify the typing experience of a TTY
Go
1
star
82

merge-queue-test

Shell
1
star
83

containers-test

Container images compatible with Coder
Dockerfile
1
star
84

sshcode-aur

sshcode AUR package
Shell
1
star
85

sandbox-for-github

a sandpit for playing around with GitHub configuration stuff such as GitHub actions or issue templates
1
star