Skip to content

Commit

Permalink
chore(examples): add sublogger example
Browse files Browse the repository at this point in the history
  • Loading branch information
aymanbagabas committed Feb 22, 2023
1 parent e08a87d commit 6e4f0e1
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 3 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ time.Sleep(10 * time.Minute)
logger.Info("Finished baking")
```

<img width="700" src="https://vhs.charm.sh/vhs-483r6n6t37vTPG0w9TrFCY.gif" alt="Made with VHS">
<img width="700" src="https://vhs.charm.sh/vhs-6oSCJcQ5EmFKKELcskJhLo.gif" alt="Made with VHS">

Use `log.SetFormatter()` or `log.WithFormatter()` to change the output format.
Available options are:
Expand All @@ -150,7 +150,6 @@ Set the logger level and options.
logger.SetReportTimestamp(false)
logger.SetReportCaller(false)
logger.SetLevel(log.DebugLevel)
logger.Debug("Preparing batch 2...")
```

### Sub-logger
Expand All @@ -159,10 +158,11 @@ Create sub-loggers with their specific fields.

```go
batch2 := logger.With("batch", 2, "chocolateChips", true)
batch2.Debug("Preparing batch 2...")
batch2.Debug("Adding chocolate chips")
```

<img width="700" src="https://vhs.charm.sh/vhs-75gIsLW8dN7DOahsxsKG4v.gif" alt="Made with VHS">
<img width="700" src="https://vhs.charm.sh/vhs-650Sw3rnwplHzwdfYNUp8n.gif" alt="Made with VHS">

### Format Messages

Expand Down
17 changes: 17 additions & 0 deletions examples/batch2/batch2.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
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.SetReportTimestamp(false)
logger.SetReportCaller(false)
logger.SetLevel(log.DebugLevel)
logger.Debug("Preparing batch 2...")
logger.Debug("Adding chocolate chips")
}

0 comments on commit 6e4f0e1

Please sign in to comment.