Skip to content

Commit

Permalink
ref(log): use log options to initialize new loggers
Browse files Browse the repository at this point in the history
- remove interface
- remove functional options
- add options struct
  • Loading branch information
aymanbagabas committed Mar 8, 2023
1 parent 65ede51 commit 010ab90
Show file tree
Hide file tree
Showing 21 changed files with 469 additions and 523 deletions.
9 changes: 3 additions & 6 deletions context.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,15 @@ package log
import "context"

// WithContext wraps the given logger in context.
func WithContext(ctx context.Context, logger Logger, keyvals ...interface{}) context.Context {
if len(keyvals) > 0 {
logger = logger.With(keyvals...)
}
func WithContext(ctx context.Context, logger *Logger) context.Context {
return context.WithValue(ctx, loggerContextKey, logger)
}

// FromContext returns the logger from the given context.
// This will return the default package logger if no logger
// found in context.
func FromContext(ctx context.Context) Logger {
if logger, ok := ctx.Value(loggerContextKey).(Logger); ok {
func FromContext(ctx context.Context) *Logger {
if logger, ok := ctx.Value(loggerContextKey).(*Logger); ok {
return logger
}
return defaultLogger
Expand Down
8 changes: 5 additions & 3 deletions context_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package log
import (
"bytes"
"context"
"io/ioutil"
"testing"

"github.com/stretchr/testify/require"
Expand All @@ -13,15 +14,16 @@ func TestLogContext_empty(t *testing.T) {
}

func TestLogContext_simple(t *testing.T) {
l := New()
l := New(ioutil.Discard)
ctx := WithContext(context.Background(), l)
require.Equal(t, l, FromContext(ctx))
}

func TestLogContext_fields(t *testing.T) {
var buf bytes.Buffer
l := New(WithOutput(&buf), WithLevel(DebugLevel))
ctx := WithContext(context.Background(), l, "foo", "bar")
l := New(&buf)
l.SetLevel(DebugLevel)
ctx := WithContext(context.Background(), l.With("foo", "bar"))
l = FromContext(ctx)
require.NotNil(t, l)
l.Debug("test")
Expand Down
6 changes: 2 additions & 4 deletions examples/batch2/batch2.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
package main

import (
"time"

"github.com/charmbracelet/log"
)

func main() {
logger := log.New(log.WithTimestamp(), log.WithTimeFormat(time.Kitchen),
log.WithCaller(), log.WithPrefix("baking 🍪 ")).With("batch", 2, "chocolateChips", true)
logger := log.Default().With("batch", 2, "chocolateChips", true)
logger.SetPrefix("baking 🍪 ")
logger.SetReportTimestamp(false)
logger.SetReportCaller(false)
logger.SetLevel(log.DebugLevel)
Expand Down
6 changes: 2 additions & 4 deletions examples/chocolate-chips/chocolate-chips.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
package main

import (
"time"

"github.com/charmbracelet/log"
)

func main() {
logger := log.New(log.WithTimestamp(), log.WithTimeFormat(time.Kitchen),
log.WithCaller(), log.WithPrefix("Baking 🍪 "))
logger := log.Default().With()

logger.SetPrefix("Baking 🍪 ")
logger.SetReportTimestamp(false)
logger.SetReportCaller(false)
logger.SetLevel(log.DebugLevel)
Expand Down
8 changes: 6 additions & 2 deletions examples/new/new.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
package main

import "github.com/charmbracelet/log"
import (
"os"

"github.com/charmbracelet/log"
)

func main() {
logger := log.New()
logger := log.New(os.Stderr)
logger.Warn("chewy!", "butter", true)
}
8 changes: 6 additions & 2 deletions examples/options/options.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
package main

import (
"os"
"time"

"github.com/charmbracelet/log"
)

func main() {
logger := log.New(log.WithTimestamp(), log.WithTimeFormat(time.Kitchen),
log.WithCaller(), log.WithPrefix("Baking 🍪 "))
logger := log.New(os.Stderr)
logger.SetPrefix("Baking 🍪 ")
logger.SetTimeFormat(time.Kitchen)
logger.SetReportTimestamp(true)
logger.SetReportCaller(true)
logger.Info("Starting oven!", "degree", 375)
time.Sleep(3 * time.Second)
logger.Info("Finished baking")
Expand Down
3 changes: 2 additions & 1 deletion examples/styles/styles.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package main

import (
"os"
"time"

"github.com/charmbracelet/lipgloss"
Expand All @@ -18,7 +19,7 @@ func main() {
Foreground(lipgloss.Color("0"))
log.KeyStyles["err"] = lipgloss.NewStyle().Foreground(lipgloss.Color("204"))
log.ValueStyles["err"] = lipgloss.NewStyle().Bold(true)
logger := log.New()
logger := log.New(os.Stderr)
logger.Error("Whoops!", "err", "kitchen on fire")
time.Sleep(3 * time.Second)
}
2 changes: 1 addition & 1 deletion json.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"time"
)

func (l *logger) jsonFormatter(keyvals ...interface{}) {
func (l *Logger) jsonFormatter(keyvals ...interface{}) {
m := make(map[string]interface{}, len(keyvals)/2)
for i := 0; i < len(keyvals); i += 2 {
switch keyvals[i] {
Expand Down
15 changes: 10 additions & 5 deletions json_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ import (

func TestJson(t *testing.T) {
var buf bytes.Buffer
l := New(WithOutput(&buf), WithFormatter(JSONFormatter))
l := New(&buf)
l.SetFormatter(JSONFormatter)
cases := []struct {
name string
expected string
Expand Down Expand Up @@ -124,8 +125,10 @@ func TestJson(t *testing.T) {

func TestJsonCaller(t *testing.T) {
var buf bytes.Buffer
l := New(WithOutput(&buf), WithLevel(DebugLevel),
WithFormatter(JSONFormatter), WithCaller())
l := New(&buf)
l.SetFormatter(JSONFormatter)
l.SetReportCaller(true)
l.SetLevel(DebugLevel)
_, file, line, _ := runtime.Caller(0)
cases := []struct {
name string
Expand Down Expand Up @@ -169,8 +172,10 @@ func TestJsonCustomKey(t *testing.T) {
TimestampKey = oldTsKey
}()
TimestampKey = "time"
logger := New(WithOutput(&buf), WithTimestamp(),
WithTimeFunction(_zeroTime), WithFormatter(JSONFormatter))
logger := New(&buf)
logger.SetTimeFunction(_zeroTime)
logger.SetFormatter(JSONFormatter)
logger.SetReportTimestamp(true)
logger.Info("info")
require.Equal(t, "{\"lvl\":\"info\",\"msg\":\"info\",\"time\":\"0001/01/01 00:00:00\"}\n", buf.String())
}
Loading

0 comments on commit 010ab90

Please sign in to comment.