-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
38 lines (32 loc) · 1.07 KB
/
Copy pathmain.go
File metadata and controls
38 lines (32 loc) · 1.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
// Example logging shows routing luart's structured events into log/slog via
// Config.Logger + NewSlogLogger. The default Logger is a no-op; this opts in so
// pool loads, drops, and load/compile failures land in your logs.
package main
import (
"bytes"
"context"
"fmt"
"log"
"log/slog"
luart "github.com/htcom-code/go-lua-perf"
)
// loggingDemo runs a script with an slog logger writing into buf, and returns
// nothing but the side effect: buf captures luart's structured log lines.
func loggingDemo(buf *bytes.Buffer) error {
loader := luart.NewMapLoader()
src := `function hi() return "hi" end`
loader.Set("hi", src, luart.HashVersion(src), "1.0.0")
logger := luart.NewSlogLogger(slog.New(slog.NewTextHandler(buf, nil)))
rt := luart.New(loader, luart.Config{MaxStates: 2, Logger: logger})
defer rt.Close()
_, err := rt.Run(context.Background(), "hi", "hi")
return err
}
func main() {
var buf bytes.Buffer
if err := loggingDemo(&buf); err != nil {
log.Fatal(err)
}
// e.g. level=INFO msg="luart: pool loaded" key=hi version=... display=1.0.0
fmt.Print(buf.String())
}