Skip to content

Commit

Permalink
Merge branch 'main' into slog
Browse files Browse the repository at this point in the history
  • Loading branch information
aymanbagabas authored May 11, 2023
2 parents 6391d1a + afe34d9 commit c860e25
Show file tree
Hide file tree
Showing 7 changed files with 23 additions and 28 deletions.
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
MIT License

Copyright (c) 2022 Charmbracelet, Inc
Copyright (c) 2022-2023 Charmbracelet, Inc

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,9 @@ log.Error("Whoops!", "err", "kitchen on fire")
Create sub-loggers with their specific fields.

```go
logger := log.NewWithOptions(os.Stderr, log.Options{
Prefix: "Baking 🍪 "
})
batch2 := logger.With("batch", 2, "chocolateChips", true)
batch2.Debug("Preparing batch 2...")
batch2.Debug("Adding chocolate chips")
Expand Down
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ go 1.17
require (
github.com/charmbracelet/lipgloss v0.7.1
github.com/go-logfmt/logfmt v0.6.0
github.com/mattn/go-isatty v0.0.17
github.com/muesli/termenv v0.15.1
github.com/stretchr/testify v1.8.2
golang.org/x/exp v0.0.0-20230315142452-642cacee5cc0
)
Expand All @@ -14,9 +14,9 @@ require (
github.com/aymanbagabas/go-osc52/v2 v2.0.1 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/lucasb-eyer/go-colorful v1.2.0 // indirect
github.com/mattn/go-isatty v0.0.18 // indirect
github.com/mattn/go-runewidth v0.0.14 // indirect
github.com/muesli/reflow v0.3.0 // indirect
github.com/muesli/termenv v0.15.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/rivo/uniseg v0.2.0 // indirect
golang.org/x/sys v0.6.0 // indirect
Expand Down
3 changes: 2 additions & 1 deletion go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,9 @@ github.com/go-logfmt/logfmt v0.6.0/go.mod h1:WYhtIu8zTZfxdn5+rREduYbwxfcBr/Vr6KE
github.com/google/go-cmp v0.5.8/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
github.com/lucasb-eyer/go-colorful v1.2.0 h1:1nnpGOrhyZZuNyfu1QjKiUICQ74+3FNCN69Aj6K7nkY=
github.com/lucasb-eyer/go-colorful v1.2.0/go.mod h1:R4dSotOR9KMtayYi1e77YzuveK+i7ruzyGqttikkLy0=
github.com/mattn/go-isatty v0.0.17 h1:BTarxUcIeDqL27Mc+vyvdWYSL28zpIhv3RoTdsLMPng=
github.com/mattn/go-isatty v0.0.17/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM=
github.com/mattn/go-isatty v0.0.18 h1:DOKFKCQ7FNG2L1rbrmstDN4QVRdS89Nkh85u68Uwp98=
github.com/mattn/go-isatty v0.0.18/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
github.com/mattn/go-runewidth v0.0.12/go.mod h1:RAqKPSqVFrSLVXbA8x7dzmKdmGzieGRCM46jaSJTDAk=
github.com/mattn/go-runewidth v0.0.14 h1:+xnbZSEeDbOIg5/mE6JF0w6n9duR1l3/WmbinWVwUuU=
github.com/mattn/go-runewidth v0.0.14/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w=
Expand Down
9 changes: 8 additions & 1 deletion logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (

"github.com/charmbracelet/lipgloss"
"golang.org/x/exp/slog"
"github.com/muesli/termenv"
)

var (
Expand Down Expand Up @@ -319,7 +320,13 @@ func (l *Logger) SetOutput(w io.Writer) {
isDiscard = 1
}
atomic.StoreUint32(&l.isDiscard, isDiscard)
l.re = lipgloss.NewRenderer(w)
// Reuse cached renderers
if v, ok := registry.Load(w); ok {
l.re = v.(*lipgloss.Renderer)
} else {
l.re = lipgloss.NewRenderer(w, termenv.WithColorCache(true))
registry.Store(w, l.re)
}
}

// SetFormatter sets the formatter.
Expand Down
8 changes: 7 additions & 1 deletion pkg.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,13 @@ import (
"time"
)

var defaultLogger = NewWithOptions(os.Stderr, Options{ReportTimestamp: true})
var (
// registry is a map of all registered lipgloss renderers.
registry = sync.Map{}

// defaultLogger is the default global logger instance.
defaultLogger = NewWithOptions(os.Stderr, Options{ReportTimestamp: true})
)

// Default returns the default logger. The default logger comes with timestamp enabled.
func Default() *Logger {
Expand Down
22 changes: 0 additions & 22 deletions terminal.go

This file was deleted.

0 comments on commit c860e25

Please sign in to comment.