logger.go raw
1 //go:build js && wasm
2
3 package wasmdb
4
5 import (
6 "fmt"
7 "syscall/js"
8 "time"
9
10 "next.orly.dev/pkg/lol"
11 )
12
13 // logger provides logging functionality for the wasmdb package
14 // It outputs to the browser console via console.log/warn/error
15 type logger struct {
16 level int
17 }
18
19 // NewLogger creates a new logger with the specified level
20 func NewLogger(level int) *logger {
21 return &logger{level: level}
22 }
23
24 // SetLogLevel changes the logging level
25 func (l *logger) SetLogLevel(level int) {
26 l.level = level
27 }
28
29 // formatMessage creates a formatted log message with timestamp
30 func (l *logger) formatMessage(level, format string, args ...interface{}) string {
31 msg := fmt.Sprintf(format, args...)
32 return fmt.Sprintf("[%s] [wasmdb] [%s] %s",
33 time.Now().Format("15:04:05.000"),
34 level,
35 msg,
36 )
37 }
38
39 // Debugf logs a debug message
40 func (l *logger) Debugf(format string, args ...interface{}) {
41 if l.level <= lol.Debug {
42 msg := l.formatMessage("DEBUG", format, args...)
43 js.Global().Get("console").Call("log", msg)
44 }
45 }
46
47 // Infof logs an info message
48 func (l *logger) Infof(format string, args ...interface{}) {
49 if l.level <= lol.Info {
50 msg := l.formatMessage("INFO", format, args...)
51 js.Global().Get("console").Call("log", msg)
52 }
53 }
54
55 // Warnf logs a warning message
56 func (l *logger) Warnf(format string, args ...interface{}) {
57 if l.level <= lol.Warn {
58 msg := l.formatMessage("WARN", format, args...)
59 js.Global().Get("console").Call("warn", msg)
60 }
61 }
62
63 // Errorf logs an error message
64 func (l *logger) Errorf(format string, args ...interface{}) {
65 if l.level <= lol.Error {
66 msg := l.formatMessage("ERROR", format, args...)
67 js.Global().Get("console").Call("error", msg)
68 }
69 }
70
71 // Fatalf logs a fatal message (does not exit in WASM)
72 func (l *logger) Fatalf(format string, args ...interface{}) {
73 msg := l.formatMessage("FATAL", format, args...)
74 js.Global().Get("console").Call("error", msg)
75 }
76