-
Notifications
You must be signed in to change notification settings - Fork 1
/
glog.go
52 lines (46 loc) · 1.25 KB
/
glog.go
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
package goscope2
import (
"fmt"
"github.com/go-playground/validator/v10"
"github.com/golang/glog"
)
const (
SEVERITY_INFO = "INFO"
SEVERITY_WARNING = "WARNING"
SEVERITY_ERROR = "ERROR"
SEVERITY_FATAL = "FATAL"
)
var validate = validator.New()
func notify(gs *GoScope2, severity string, format string, args ...any) {
if err := validate.Var(severity, "required,oneof=INFO WARNING ERROR FATAL"); err != nil {
notify(gs, SEVERITY_FATAL, "invalid severity on error %w", fmt.Errorf(format, args...))
return
}
message := fmt.Sprintf(format, args...)
log := &Goscope2Log{
Type: TYPE_LOG,
Severity: severity,
Message: message,
}
go func() {
log.GenerateHash()
gs.DB.Create(log)
}()
}
func (gs *GoScope2) Infof(format string, args ...any) {
go notify(gs, SEVERITY_INFO, format, args...)
glog.Infof(format, args...)
}
func (gs *GoScope2) Warningf(format string, args ...any) {
go notify(gs, SEVERITY_WARNING, format, args...)
glog.Warningf(format, args...)
}
func (gs *GoScope2) Errorf(format string, args ...any) {
go notify(gs, SEVERITY_ERROR, format, args...)
glog.Errorf(format, args...)
}
func (gs *GoScope2) Fatalf(format string, args ...any) {
notify(gs, SEVERITY_FATAL, format, args...)
glog.Flush()
glog.Fatalf(format, args...)
}