Skip to content

Commit

Permalink
docs: add styles section and examples
Browse files Browse the repository at this point in the history
  • Loading branch information
aymanbagabas committed Feb 23, 2023
1 parent 169c012 commit a9f75d2
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 0 deletions.
19 changes: 19 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,25 @@ logger.SetReportCaller(false)
logger.SetLevel(log.DebugLevel)
```

### Styles

You can customize the logger styles using [Lipgloss][lipgloss]. The styles are
defined at a global level in [styles.go](./styles.go).

```go
// Override the default error level style.
log.ErrorLevelStyle = lipgloss.NewStyle().
SetString("ERROR!!").
Padding(0, 1, 0, 1).
Background(lipgloss.Color("204")).
Foreground(lipgloss.Color("0"))
// Add a custom style for key `err`
log.KeyStyles["err"] = lipgloss.NewStyle().Foreground(lipgloss.Color("204"))
log.Error("Whoops!", "err", "kitchen on fire")
```

<img width="400" src="https://vhs.charm.sh/vhs-1s1qma0OVFeWFGqtBAPpfW.gif" alt="Made with VHS">

### Sub-logger

Create sub-loggers with their specific fields.
Expand Down
20 changes: 20 additions & 0 deletions examples/styles/styles.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package main

import (
"time"

"github.com/charmbracelet/lipgloss"
"github.com/charmbracelet/log"
)

func main() {
log.ErrorLevelStyle = lipgloss.NewStyle().
SetString("ERROR!!").
Padding(0, 1, 0, 1).
Background(lipgloss.Color("204")).
Foreground(lipgloss.Color("0"))
log.KeyStyles["err"] = lipgloss.NewStyle().Foreground(lipgloss.Color("204"))
logger := log.New()
logger.Error("Whoops!", "err", "kitchen on fire")
time.Sleep(3 * time.Second)
}
8 changes: 8 additions & 0 deletions examples/styles/styles.tape
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
Output styles.gif

Set Height 250
Set Width 700

Type "./styles" Sleep 500ms Enter

Sleep 3s

0 comments on commit a9f75d2

Please sign in to comment.