Skip to content

Commit

Permalink
cfg and cfg.New
Browse files Browse the repository at this point in the history
  • Loading branch information
trimble committed Nov 25, 2023
1 parent 8a5e577 commit 8183b0c
Show file tree
Hide file tree
Showing 3 changed files with 121 additions and 11 deletions.
67 changes: 67 additions & 0 deletions examples/logloglog/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
// Package main shows an example of logging with sabot.
package main

import (
"context"
"os"

"github.com/pkg/errors"

"github.com/clarktrimble/sabot"
)

var (
version string
)

type Config struct {
Version string `json:"version"`
Logger *sabot.Config `json:"logger"`
}

func main() {

// usually load config via launch(envconfig), but literal for demo

cfg := &Config{
Version: version,
Logger: &sabot.Config{
MaxLen: 99,
},
}

// create logger

lgr := cfg.Logger.New(os.Stderr)

// usually set run id with hondo(rand), but literal for demo

ctx := lgr.WithFields(context.Background(), "run_id", "123123123")

// log stuff, yay

lgr.Info(ctx, "logloglog starting", "config", cfg)
lgr.Error(ctx, "failed to, you know ..", errors.Errorf("oops"))
}

// output:

/*
$ bin/logloglog 2>&1 | jq --slurp
[
{
"config": "{\"version\":\"config.11.8a5e577\",\"logger\":{\"max_len\":99}}",
"level": "info",
"msg": "logloglog starting",
"run_id": "123123123",
"ts": "2023-11-25T21:20:54.758434441Z"
},
{
"error": "oops\nmain.main\n\t/home/trimble/proj/sabot/examples/logloglog/main.go:38\nruntime.main\n\t/--truncated--",
"level": "error",
"msg": "failed to, you know ..",
"run_id": "123123123",
"ts": "2023-11-25T21:20:54.758722223Z"
}
]
*/
27 changes: 22 additions & 5 deletions sabot.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,28 @@ import (
"github.com/pkg/errors"
)

const (
logErrorKey string = "logerror"
truncationNotice string = "--truncated--"
)

// Fields are key-value pairs.
type Fields map[string]any

// Config is the configurable fields of Sabot.
type Config struct {
MaxLen int `json:"max_len" desc:"maximum length that will be logged for any field"`
}

// New creates a Sabot from Config.
func (cfg *Config) New(writer io.Writer) *Sabot {

return &Sabot{
MaxLen: cfg.MaxLen,
Writer: writer,
}
}

// LogKey is a unique to this package key for use with context Value.
type LogKey struct{}

Expand Down Expand Up @@ -57,11 +76,6 @@ func (sabot *Sabot) GetFields(ctx context.Context) Fields {
// unexported
//

const (
logErrorKey string = "logerror"
truncationNotice string = "--truncated--"
)

func (sabot *Sabot) log(ctx context.Context, level, msg string, kv []any) {

now := time.Now().UTC()
Expand Down Expand Up @@ -193,6 +207,9 @@ func copyFields(ctx context.Context) Fields {

func (fields Fields) truncate(max int) {

// Todo: prolly don't want just add notice yeah?
// Todo: and while you're up, git rid of alt writer?

max -= len(truncationNotice)
if max < 1 {
return
Expand Down
38 changes: 32 additions & 6 deletions sabot_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"context"
"encoding/json"
"fmt"
"os"
"strings"
"testing"
"time"
Expand All @@ -22,16 +23,42 @@ func TestSabot(t *testing.T) {

var _ = Describe("Sabot", func() {

var (
ctx context.Context
lgr *Sabot
)

Describe("creating a logger from config", func() {
var (
cfg *Config
)

JustBeforeEach(func() {
lgr = cfg.New(os.Stderr)
})

When("all is well", func() {
BeforeEach(func() {
cfg = &Config{
MaxLen: 99,
}
})

It("should setup the logger", func() {
Expect(lgr.MaxLen).To(Equal(99))
Expect(lgr.Writer).To(Equal(os.Stderr))
})
})
})

Describe("getting and storing fields", func() {
var (
ctx context.Context
fields Fields
lgr Sabot
)

BeforeEach(func() {
ctx = context.Background()
lgr = Sabot{
lgr = &Sabot{
MaxLen: 0,
}
})
Expand Down Expand Up @@ -99,16 +126,14 @@ var _ = Describe("Sabot", func() {
Describe("logging an event", func() {
var (
buf *bytes.Buffer
ctx context.Context
msg string
err error
kv []any
lgr Sabot
)

BeforeEach(func() {
buf = &bytes.Buffer{}
lgr = Sabot{
lgr = &Sabot{
Writer: buf,
MaxLen: 0,
}
Expand Down Expand Up @@ -326,6 +351,7 @@ var _ = Describe("Sabot", func() {
func delog(buf *bytes.Buffer) (logged Fields) {

// Todo: combine efforts with other log testing stuffs

// marshal logged data to map

logged = Fields{}
Expand Down

0 comments on commit 8183b0c

Please sign in to comment.