• Stars
    star
    234
  • Rank 165,281 (Top 4 %)
  • Language
    Go
  • License
    MIT License
  • Created about 1 year ago
  • Updated about 1 month ago

Reviews

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

Repository Details

🚨 Design workflows of slog handlers: pipeline, middleware, fanout, routing, failover, load balancing...

slog: Handler chaining, fanout, routing, failover, load balancing...

tag Go Version GoDoc Build Status Go report Coverage Contributors License

Design workflows of slog handlers:

  • fanout: distribute log.Record to multiple slog.Handler in parallel
  • pipeline: rewrite log.Record on the fly (eg: for privacy reason)
  • routing: forward log.Record to all matching slog.Handler
  • failover: forward log.Record to the first available slog.Handler
  • load balancing: increase log bandwidth by sending log.Record to a pool of slog.Handler

Here a simple workflow with both pipeline and fanout:

workflow example with pipeline and fanout

See also:

HTTP middlewares:

Loggers:

Log sinks:

🚀 Install

go get github.com/samber/slog-multi

Compatibility: go >= 1.21

No breaking changes will be made to exported APIs before v2.0.0.

⚠️ Use this library carefully, log processing can be very costly (!)

💡 Usage

GoDoc: https://pkg.go.dev/github.com/samber/slog-multi

Broadcast: slogmulti.Fanout()

Distribute logs to multiple slog.Handler in parallel.

import (
    slogmulti "github.com/samber/slog-multi"
    "log/slog"
)

func main() {
    logstash, _ := net.Dial("tcp", "logstash.acme:4242")    // use github.com/netbrain/goautosocket for auto-reconnect
    stderr := os.Stderr

    logger := slog.New(
        slogmulti.Fanout(
            slog.NewJSONHandler(logstash, &slog.HandlerOptions{}),  // pass to first handler: logstash over tcp
            slog.NewTextHandler(stderr, &slog.HandlerOptions{}),    // then to second handler: stderr
            // ...
        ),
    )

    logger.
        With(
            slog.Group("user",
                slog.String("id", "user-123"),
                slog.Time("created_at", time.Now()),
            ),
        ).
        With("environment", "dev").
        With("error", fmt.Errorf("an error")).
        Error("A message")
}

Stderr output:

time=2023-04-10T14:00:0.000000+00:00 level=ERROR msg="A message" user.id=user-123 user.created_at=2023-04-10T14:00:0.000000+00:00 environment=dev error="an error"

Netcat output:

{
	"time":"2023-04-10T14:00:0.000000+00:00",
	"level":"ERROR",
	"msg":"A message",
	"user":{
		"id":"user-123",
		"created_at":"2023-04-10T14:00:0.000000+00:00"
	},
	"environment":"dev",
	"error":"an error"
}

Routing: slogmulti.Router()

Distribute logs to all matching slog.Handler in parallel.

import (
    slogmulti "github.com/samber/slog-multi"
	slogslack "github.com/samber/slog-slack"
    "log/slog"
)

func main() {
    slackChannelUS := slogslack.Option{Level: slog.LevelError, WebhookURL: "xxx", Channel: "supervision-us"}.NewSlackHandler()
	slackChannelEU := slogslack.Option{Level: slog.LevelError, WebhookURL: "xxx", Channel: "supervision-eu"}.NewSlackHandler()
	slackChannelAPAC := slogslack.Option{Level: slog.LevelError, WebhookURL: "xxx", Channel: "supervision-apac"}.NewSlackHandler()

	logger := slog.New(
		slogmulti.Router().
			Add(slackChannelUS, recordMatchRegion("us")).
			Add(slackChannelEU, recordMatchRegion("eu")).
			Add(slackChannelAPAC, recordMatchRegion("apac")).
			Handler(),
	)

	logger.
		With("region", "us").
		With("pool", "us-east-1").
		Error("Server desynchronized")
}

func recordMatchRegion(region string) func(ctx context.Context, r slog.Record) bool {
	return func(ctx context.Context, r slog.Record) bool {
		ok := false

		r.Attrs(func(attr slog.Attr) bool {
			if attr.Key == "region" && attr.Value.Kind() == slog.KindString && attr.Value.String() == region {
				ok = true
				return false
			}

			return true
		})

		return ok
	}
}

Failover: slogmulti.Failover()

List multiple targets for a slog.Record instead of retrying on the same unavailable log management system.

import (
	"net"
    slogmulti "github.com/samber/slog-multi"
    "log/slog"
)


func main() {
	// ncat -l 1000 -k
	// ncat -l 1001 -k
	// ncat -l 1002 -k

    // list AZs
    // use github.com/netbrain/goautosocket for auto-reconnect
	logstash1, _ := net.Dial("tcp", "logstash.eu-west-3a.internal:1000")
	logstash2, _ := net.Dial("tcp", "logstash.eu-west-3b.internal:1000")
	logstash3, _ := net.Dial("tcp", "logstash.eu-west-3c.internal:1000")

	logger := slog.New(
		slogmulti.Failover()(
			slog.HandlerOptions{}.NewJSONHandler(logstash1, nil),    // send to this instance first
			slog.HandlerOptions{}.NewJSONHandler(logstash2, nil),    // then this instance in case of failure
			slog.HandlerOptions{}.NewJSONHandler(logstash3, nil),    // and finally this instance in case of double failure
		),
	)

	logger.
		With(
			slog.Group("user",
				slog.String("id", "user-123"),
				slog.Time("created_at", time.Now()),
			),
		).
		With("environment", "dev").
		With("error", fmt.Errorf("an error")).
		Error("A message")
}

Load balancing: slogmulti.Pool()

Increase log bandwidth by sending log.Record to a pool of slog.Handler.

import (
	"net"
    slogmulti "github.com/samber/slog-multi"
    "log/slog"
)

func main() {
	// ncat -l 1000 -k
	// ncat -l 1001 -k
	// ncat -l 1002 -k

    // list AZs
    // use github.com/netbrain/goautosocket for auto-reconnect
	logstash1, _ := net.Dial("tcp", "logstash.eu-west-3a.internal:1000")
	logstash2, _ := net.Dial("tcp", "logstash.eu-west-3b.internal:1000")
	logstash3, _ := net.Dial("tcp", "logstash.eu-west-3c.internal:1000")

	logger := slog.New(
		slogmulti.Pool()(
            // a random handler will be picked
			slog.HandlerOptions{}.NewJSONHandler(logstash1, nil),
			slog.HandlerOptions{}.NewJSONHandler(logstash2, nil),
			slog.HandlerOptions{}.NewJSONHandler(logstash3, nil),
		),
	)

	logger.
		With(
			slog.Group("user",
				slog.String("id", "user-123"),
				slog.Time("created_at", time.Now()),
			),
		).
		With("environment", "dev").
		With("error", fmt.Errorf("an error")).
		Error("A message")
}

Chaining: slogmulti.Pipe()

Rewrite log.Record on the fly (eg: for privacy reason).

func main() {
    // first middleware: format go `error` type into an object {error: "*myCustomErrorType", message: "could not reach https://a.b/c"}
    errorFormattingMiddleware := slogmulti.NewHandleInlineMiddleware(errorFormattingMiddleware)

    // second middleware: remove PII
    gdprMiddleware := NewGDPRMiddleware()

    // final handler
    sink := slog.NewJSONHandler(os.Stderr, &slog.HandlerOptions{})

    logger := slog.New(
        slogmulti.
            Pipe(errorFormattingMiddleware).
            Pipe(gdprMiddleware).
            // ...
            Handler(sink),
    )

    logger.
        With(
            slog.Group("user",
                slog.String("id", "user-123"),
                slog.String("email", "user-123"),
                slog.Time("created_at", time.Now()),
            ),
        ).
        With("environment", "dev").
        Error("A message",
            slog.String("foo", "bar"),
            slog.Any("error", fmt.Errorf("an error")),
        )
}

Stderr output:

{
    "time":"2023-04-10T14:00:0.000000+00:00",
    "level":"ERROR",
    "msg":"A message",
    "user":{
        "id":"*******",
        "email":"*******",
        "created_at":"*******"
    },
    "environment":"dev",
    "foo":"bar",
    "error":{
        "type":"*myCustomErrorType",
        "message":"an error"
    }
}

Custom middleware

Middleware must match the following prototype:

type Middleware func(slog.Handler) slog.Handler

The example above uses:

Note: WithAttrs and WithGroup methods of custom middleware must return a new instance, instead of this.

Inline middleware

An "inline middleware" (aka. lambda), is a shortcut to middleware implementation, that hooks a single method and proxies others.

// hook `logger.Enabled` method
mdw := slogmulti.NewEnabledInlineMiddleware(func(ctx context.Context, level slog.Level, next func(context.Context, slog.Level) bool) bool{
    // [...]
    return next(ctx, level)
})
// hook `logger.Handle` method
mdw := slogmulti.NewHandleInlineMiddleware(func(ctx context.Context, record slog.Record, next func(context.Context, slog.Record) error) error {
    // [...]
    return next(ctx, record)
})
// hook `logger.WithAttrs` method
mdw := slogmulti.NewWithAttrsInlineMiddleware(func(attrs []slog.Attr, next func([]slog.Attr) slog.Handler) slog.Handler{
    // [...]
    return next(attrs)
})
// hook `logger.WithGroup` method
mdw := slogmulti.NewWithGroupInlineMiddleware(func(name string, next func(string) slog.Handler) slog.Handler{
    // [...]
    return next(name)
})

A super inline middleware that hooks all methods.

Warning: you would rather implement your own middleware.

mdw := slogmulti.NewInlineMiddleware(
    func(ctx context.Context, level slog.Level, next func(context.Context, slog.Level) bool) bool{
        // [...]
        return next(ctx, level)
    },
    func(ctx context.Context, record slog.Record, next func(context.Context, slog.Record) error) error{
        // [...]
        return next(ctx, record)
    },
    func(attrs []slog.Attr, next func([]slog.Attr) slog.Handler) slog.Handler{
        // [...]
        return next(attrs)
    },
    func(name string, next func(string) slog.Handler) slog.Handler{
        // [...]
        return next(name)
    },
)

🤝 Contributing

Don't hesitate ;)

# Install some dev dependencies
make tools

# Run tests
make test
# or
make watch-test

👤 Contributors

Contributors

💫 Show your support

Give a ⭐️ if this project helped you!

GitHub Sponsors

📝 License

Copyright © 2023 Samuel Berthe.

This project is MIT licensed.

More Repositories

1

lo

💥 A Lodash-style Go library based on Go 1.18+ Generics (map, filter, contains, find...)
Go
15,102
star
2

awesome-prometheus-alerts

🚨 Collection of Prometheus alerting rules
HTML
5,944
star
3

mo

🦄 Monads and popular FP abstractions, powered by Go 1.18+ Generics (Option, Result, Either...)
Go
2,207
star
4

do

⚙️ A dependency injection toolkit based on Go 1.18+ Generics.
Go
1,555
star
5

invoice-as-a-service

💰 Simple invoicing service (REST API): from JSON to PDF
PHP
181
star
6

oops

🔥 Error handling library with context, assertion, stack trace and source fragments
Go
164
star
7

sync-ssh-keys

🔐 Sync public ssh keys to ~/.ssh/authorized_keys, based on Github/Gitlab organization membership.
Go
134
star
8

chartjs-plugin-datasource-prometheus

📊 Chart.js plugin for Prometheus
TypeScript
95
star
9

slog-formatter

🚨 slog: Attribute formatting
Go
79
star
10

go-gpt-3-encoder

Go BPE tokenizer (Encoder+Decoder) for GPT2 and GPT3
Go
77
star
11

slog-echo

🚨 Echo middleware for slog logger
Go
72
star
12

slog-gin

🚨 Gin middleware for slog logger
Go
65
star
13

the-great-gpt-firewall

🤖 A curated list of websites that restrict access to AI Agents, AI crawlers and GPTs
Python
65
star
14

prometheus-query-js

📊 A Javascript client for Prometheus query API
TypeScript
60
star
15

github-actions-runner

✅ Docker images for starting self-hosted Github Actions runner(s).
Dockerfile
57
star
16

grafana-flamegraph-panel

📊 Flame graph panels for Grafana
JavaScript
37
star
17

slog-fiber

🚨 Fiber middleware for slog logger
Go
35
star
18

slog-sampling

🚨 slog sampling: drop repetitive log records
Go
35
star
19

workshop-prometheus-grafana

📊 Prometheus and Grafana 101
JavaScript
30
star
20

slog-sentry

🚨 slog: Sentry handler
Go
30
star
21

slog-chi

🚨 Chi middleware for slog logger
Go
22
star
22

awesome-olap

A curated list of awesome Online Analytical Processing databases, frameworks, ressources and other awesomeness.
16
star
23

go-amqp-pubsub

Fault tolerant Pub/Sub library for RabbitMQ
Go
16
star
24

pg_cron

⏰ PostgreSQL extension for running periodic jobs
C
15
star
25

slog-loki

🚨 slog: Loki handler
Go
14
star
26

arp-spoofing

💥 Simple implementation of arp poisoning attack ;)
C
14
star
27

slog-slack

🚨 slog: Slack handler
Go
14
star
28

slog-zap

🚨 slog: Zap handler
Go
12
star
29

slog-zerolog

🚨 slog: Zerolog handler
Go
12
star
30

go-tcp-pool

✨ Drop-in replacement to net.Conn with pooling and auto-reconnect
Go
11
star
31

refined-hn

JavaScript
11
star
32

slog-logrus

🚨 slog: Logrus handler
Go
11
star
33

slog-http

🚨 net/http middleware for slog logger
Go
10
star
34

free_proxy_list

Free proxy list [NOT MAINTAINED ANYMORE - please fork]
Shell
9
star
35

slog-syslog

🚨 slog: Syslog handler
Go
9
star
36

slog-parquet

🚨 slog: Parquet handler + Object Storage
Go
9
star
37

go-type-to-string

🕵️‍♂️ Extract a string representation of Go type
Go
8
star
38

git-contrib-graph

📊 Displays a github-like contribution graph, of every contributors of a repository
Go
8
star
39

powEUr

Python
7
star
40

node-promfiler

Expose a http endpoint for exporting node.js v8 profiling
JavaScript
7
star
41

slog-datadog

🚨 slog: Datadog handler
Go
7
star
42

slog-channel

🚨 slog: Go channel handler
Go
5
star
43

go-singleflightx

🧬 x/sync/singleflight but with generics, batching and nullable result
Go
5
star
44

slog-nats

🚨 slog: NATS handler
Go
5
star
45

slog-kafka

🚨 slog: Kafka handler
Go
4
star
46

remote-dev-environment

👨‍💻 My development environment is too slow, let's fix that !
4
star
47

GoogleCalendarNotifier-FitbitTracker

Google Calendar notifier for Fitbit Tracker
Gosu
4
star
48

ansible-role-airbyte

Ansible role for Airbyte
4
star
49

criterion-rpm-package

RPM package for Criterion (C unit testing)
Shell
3
star
50

dagobert

A simple Go client for the clip-as-service server
Go
3
star
51

rabbitmq-flooding

Cluster recovery testing. Floods RabbitMQ with random data.
Python
3
star
52

slog-graylog

🚨 slog: Graylog handler
Go
3
star
53

go-psi

🥵 Pressure Stall Informations (PSI) and starvation notifier
Go
3
star
54

slog-telegram

🚨 slog: Telegram handler
Go
3
star
55

hot

🌶️ In-memory caching library for Go
Go
3
star
56

llvm_dart_binding

Binding Dart/LLVM (using LLVM bytecode from Dart)
Dart
3
star
57

slog-webhook

🚨 slog: Webhook handler
Go
3
star
58

slog-common

Common toolchain for slog
Go
2
star
59

slog-logstash

🚨 slog: Logstash handler
Go
2
star
60

lab-langchain-getting-started

Python
2
star
61

BTCC_api

A basic API wrapper for the BTCC Trading and Market FIX API.
JavaScript
2
star
62

github-stackoverflow-email-scrapping

Scrape top Github and Stack-Overflow users to find email address
Go
2
star
63

ngx-domarrow

Declarative and template-driven DOMArrow integration for Angular2+
TypeScript
2
star
64

celery_demonstration

Async worker + scheduling
Python
2
star
65

go-metered-io

📐 A drop-in replacement to io.Reader and io.Writer with the total number of bytes transfered.
Go
2
star
66

dotfiles

@samber's dotfiles
JavaScript
1
star
67

grafana-dashboard-nomad

Grafana dashboards for Nomad (Docker orchestrator from Hashicorp)
1
star
68

slog-fluentd

🚨 slog: Fluentd handler
Go
1
star
69

go-clevercloud-api

Go library for Clever-Cloud api
Go
1
star
70

lab-langchain

Python
1
star
71

slog-mattermost

🚨 slog: Mattermost handler
Go
1
star
72

dockerfiles

Dockerfile
1
star
73

jitsi-virtual-background

JavaScript
1
star
74

SaaS-Cookbook-List

List of Cookbook about SaaS development (ENG/FR)
1
star
75

raw-ip-udp-sockets-chap

Simple implementation of CHAP protocol, with raw socket layers (3+4)
C
1
star
76

lab-parquet

Go
1
star
77

nft-http-api

🚦 NFT over HTTP API
Go
1
star
78

canvas-to-bmp

TypeScript
1
star
79

refined-cycle-app

JavaScript
1
star
80

azure-ad-oauth2-proxy

Dockerfile
1
star
81

packer-qemu-debian

Builds Debian 8 image for Qemu
Shell
1
star
82

poc-selenium-unit-test-css

Python
1
star
83

maxscale-experiments

Demonstration step-by-step of MaxScale for master/slave query spliting/routing #mysql #docker
Shell
1
star
84

messenger-bot-clock

Messenger bot replying with current time
JavaScript
1
star
85

hello-world-node-pg-redis

Simple health check with NodeJS + Redis + PostgreSQL
JavaScript
1
star
86

slog-microsoft-teams

🚨 slog: Microsoft Teams handler
Go
1
star
87

fb-messenger-bot-psychologist

🤖 A Messenger bot talking like a psychologist
Emacs Lisp
1
star