Skip to content

Commit 79a30e2

Browse files
ernadoclaude
andcommitted
docs: shared example logger (debug) and pl logging guide
Extract examples.NewLogger: zap NewProductionConfig at Debug level, so MTProto RPC traces and the business peer diagnostics are visible. Add examples/README.md documenting how to pretty-print the JSONL with github.com/go-faster/pl. Use the shared logger in the business example and drop the peer diagnostic to Debug. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
1 parent fda2a70 commit 79a30e2

4 files changed

Lines changed: 82 additions & 2 deletions

File tree

business_updates.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ func (b *Bot) logBusinessPeerDiag(ctx context.Context, peer tg.PeerClass, e tg.E
9898
}
9999
}
100100

101-
b.logger().Info(ctx, "Business peer diagnostic", fields...)
101+
b.logger().Debug(ctx, "Business peer diagnostic", fields...)
102102
}
103103

104104
// peerKindString describes a peer for diagnostics.

examples/README.md

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
# Examples
2+
3+
Runnable bots showing the library in practice. Each is its own `main` package:
4+
5+
```bash
6+
APP_ID=12345 APP_HASH=abcdef BOT_TOKEN=123:abc go run ./examples/echo
7+
```
8+
9+
Bots need an MTProto app identity (`APP_ID`/`APP_HASH`, from
10+
<https://my.telegram.org>) plus a BotFather token (`BOT_TOKEN`).
11+
12+
## Logging
13+
14+
The bots log structured **JSONL** via zap. The shared
15+
[`examples.NewLogger`](./logger.go) uses `zap.NewProductionConfig` but lowers the
16+
level to **Debug**, so MTProto RPC traces and the business peer diagnostics show
17+
up — handy when verifying behavior against the live API.
18+
19+
Raw JSON is hard to read in a terminal. Pipe it through
20+
[`github.com/go-faster/pl`](https://github.com/go-faster/pl), which tails and
21+
pretty-prints exactly this `zap.NewProductionConfig` JSONL.
22+
23+
### Install pl
24+
25+
```bash
26+
go install github.com/go-faster/pl/cmd/pl@latest
27+
```
28+
29+
### Use it
30+
31+
zap writes to **stderr**, so redirect it into `pl` with `2>&1`:
32+
33+
```bash
34+
APP_ID=12345 APP_HASH=abcdef BOT_TOKEN=123:abc go run ./examples/business 2>&1 | pl
35+
```
36+
37+
Useful flags:
38+
39+
- `--level info` — hide debug lines (keep info and above)
40+
- `--no-time` — drop timestamps
41+
- `--no-color` — disable colors (or set `NO_COLOR`)
42+
- `-f service.log` — follow a file like `tail -f`
43+
44+
Non-JSON lines pass through untouched, so mixed output (e.g. a panic stack
45+
trace) stays readable.
46+
47+
To capture a session and read it back later:
48+
49+
```bash
50+
go run ./examples/business 2>session.log
51+
pl session.log # read once
52+
pl -f session.log # follow while the bot runs
53+
```

examples/business/main.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,11 +49,16 @@ import (
4949
"go.uber.org/zap"
5050

5151
"github.com/gotd/botapi"
52+
"github.com/gotd/botapi/examples"
5253
"github.com/gotd/botapi/storage"
5354
)
5455

5556
func main() {
56-
log, _ := zap.NewProduction()
57+
log, err := examples.NewLogger()
58+
if err != nil {
59+
panic(err)
60+
}
61+
5762
defer func() { _ = log.Sync() }()
5863

5964
appID, err := atoi(os.Getenv("APP_ID"))

examples/logger.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// Package examples holds helpers shared by the runnable example bots.
2+
package examples
3+
4+
import (
5+
"go.uber.org/zap"
6+
"go.uber.org/zap/zapcore"
7+
)
8+
9+
// NewLogger builds the logger shared by the example bots: the standard zap
10+
// production configuration (structured JSONL on stderr) with the level lowered
11+
// to Debug, so the library's debug output — MTProto RPC traces and the business
12+
// peer diagnostics — is visible while debugging.
13+
//
14+
// The output is JSONL in the shape github.com/go-faster/pl expects; pipe it
15+
// through pl for readable, colorized logs (see the examples README).
16+
func NewLogger() (*zap.Logger, error) {
17+
cfg := zap.NewProductionConfig()
18+
19+
cfg.Level = zap.NewAtomicLevelAt(zapcore.DebugLevel)
20+
21+
return cfg.Build()
22+
}

0 commit comments

Comments
 (0)