Skip to content

loft-orbital/slogx

Repository files navigation

slogx

Godoc Release

Go library to enhances the slog package with additional handlers and context helpers.

Installation

To install use the following command:

go get github.com/loft-orbital/slogx

Features

Refer to the godoc for the full usage documentation.

Simple logger creation

package main

import "github.com/loft-orbital/slogx"

func main() {
  log := slogx.New(os.Stdout, slogx.JSON, false)
  log.Info("Hello world!")
}

Context Injection

package main

import (
  "context"
  "os"

  "github.com/loft-orbital/slogx"
)

func foo(ctx context.Context) {
  log := slogx.FromContext(ctx)
  log.Info("Hello world!")
}

func main() {
  log := slogx.New(os.Stdout, slogx.JSON, false)
  
  ctx := slogx.ContextWithLogger(context.Background(), log)
  foo(ctx)
}

gRPC interceptors

package main

import (
  "google.golang.org/grpc"
  "github.com/loft-orbital/slogx"
)

func main() {
  dopts := []grpc.DialOptions {
    grpc.WithStreamInterceptor(slogx.StreamClientInterceptor(slogx.Default)),
    // ... others gRPC dial options
  }

  client, err := grpc.NewClient("grpchost:443", dopts...)
  if err!=nil {
    panic(err)
  }

  // gRPC service created using this client will have the default slogx logger
  // available in each stream's context.
}

Contributing

Please refer to the CONTRIBUTING.md file for information on how to contribute to this project.