ai_old/js/log.go

41 lines
1.1 KiB
Go
Raw Permalink Normal View History

package js
import (
"apigo.cc/ai/ai/goja"
"github.com/ssgo/log"
)
func RequireLog() map[string]any {
return map[string]any{
"info": func(argsIn goja.FunctionCall, vm *goja.Runtime) goja.Value {
args := MakeArgs(&argsIn, vm).Check(1)
getLogger(vm).Info(args.Str(0), args.Map2Arr(1)...)
return nil
},
"warn": func(argsIn goja.FunctionCall, vm *goja.Runtime) goja.Value {
args := MakeArgs(&argsIn, vm).Check(1)
getLogger(vm).Warning(args.Str(0), args.Map2Arr(1)...)
return nil
},
"error": func(argsIn goja.FunctionCall, vm *goja.Runtime) goja.Value {
args := MakeArgs(&argsIn, vm).Check(1)
getLogger(vm).Error(args.Str(0), args.Map2Arr(1)...)
return nil
},
"debug": func(argsIn goja.FunctionCall, vm *goja.Runtime) goja.Value {
args := MakeArgs(&argsIn, vm).Check(1)
getLogger(vm).Debug(args.Str(0), args.Map2Arr(1)...)
return nil
},
}
}
func getLogger(vm *goja.Runtime) *log.Logger {
if vm.GoData["logger"] != nil {
if logger, ok := vm.GoData["logger"].(*log.Logger); ok {
return logger
}
}
return log.DefaultLogger
}